#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0

    Triangle numbers


    I was looking for assistance with this program. The goal is take user input in the form x,y where x is the amount of numbers in the first row and y is the amount of numbers in the last row and all while applying euler's triangle equation like so:

    2,5 yields
    1-3-
    1-3-6-
    1-3-6-10-
    1-3-6-10-15-

    here is where i'm at:

    Code:
    <script language="javascript">        
    var str = prompt('Input beginning number, ending number', '3,4'); 	   
    var size = new Array(); 	  
     dimension = str.split(','); 	  
     var x = parseInt(dimension[0]);  	   
    var y = parseInt(dimension[1]); 	  
     var z = 1; 	
    for(i=0; i<x; i++) 	{ 	 		document.write(z+'&nbsp-&nbsp<br>'); 			z++ 			 		 		
    for(j=0; j<y; j++) 		 			 			document.write(z+'&nbsp-&nbsp'); 
    z =(z*(z+1)/2); 			 		 	} 
    </script>
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,123
    Rep Power
    119
    It took me a minute to put it all together, but the code below should produce what your looking for and I also added a "if" condition; to remove the last trailing hyphen (comment that out / remove that, if you want to).

    Code:
    <script type="text/javascript">        
    var str = prompt('Input beginning number, ending number', '3,4'); 	   
    var size = new Array(); 	  
    var dimension = str.split(','); 	  
    var x = parseInt(dimension[0]);  	   
    var y = parseInt(dimension[1]); 	  
    var z = 0;
    var max = 1;
    for(i=0;i<y;i++) {
    z++;
    size.push((z*(z+1)/2));
    }
    for (iii=1;iii<y;iii++) {
    max++;
    for (ii=0;ii<max;ii++) {
    document.write(size[ii]);
     if (ii<(max-1)) {
      document.write(" - ");
     }
    }
    document.write("<br/>");
    }
    </script>
    Last edited by web_loone08; November 12th, 2012 at 09:30 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    Oh alright that makes sense I wasn't familiar with push thank you very much.

IMN logo majestic logo threadwatch logo seochat tools logo