JavaScript question. If you were to arrange 15 dots in the shape of a triangle,
ID: 3845133 • Letter: J
Question
JavaScript question. If you were to arrange 15 dots in the shape of a triangle, you end up with an arrangement that might look something like this:
0
0 0
0 0 0
0 0 0 0
0 0 0 0 0
The first row of the triangle contains one dot, the second row contains two dots then three and so on. the number of dots it takes to form a triangle containing n rows is the sum of the integers from 1 through n. This sum is known as a triangular number. If you start at 1, the fourth triangular number is the sum of the consecutive integers 1 through 4 ( 1 + 2 + 3 + 4), or 10. A triangular number can also be generated by the formula:
triangularNumber = n * ( n + 1) / 2
for any integer value of n. For example, the 10th triangular number, 55, can be generated by substituting 10 as the value for n in the preceding formula. create a program that generates a table of triangular numbers using the preceding formula. Have the program generate every fifth triangular number between 5 and 50 (that is, 5, 10, 15, ..., 50)
Explanation / Answer
<html>
<body>
<script>
function triangularNum()
{
var i,j,sum;
for(i=5;i<=50;i=i+5)
{
sum = 0;
sum = i * (i+1) / 2;
document.write( i + " = " + sum + "<br>");
}
}
</script>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.