Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
November 12th, 2012, 02:23 PM
 fronter
Registered User

Join Date: Nov 2012
Posts: 16
Time spent in forums: 8 h 25 m 41 sec
Reputation 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
November 12th, 2012, 09:24 PM
 web_loone08
Contributing User

Join Date: Aug 2008
Posts: 599
Time spent in forums: 1 Week 1 Day 12 h 40 m
Reputation Power: 69
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.

#3
November 13th, 2012, 01:45 AM
 fronter
Registered User

Join Date: Nov 2012
Posts: 16
Time spent in forums: 8 h 25 m 41 sec
Reputation Power: 0
Oh alright that makes sense I wasn't familiar with push thank you very much.

 Viewing: Dev Shed Forums > Web Design > JavaScript Development > Triangle numbers