up one level
---
2014-02-12 20:22:24
Here’s the code for the pascal’s triangle javascript challenge that I did.
The part near the top is the actual script.
Then, commented out are nine (9) iterations that i made leading up to that final script. (i.e. “showing my work”)
At the very bottom is the “plan” that I wrote out in the first 30-45 minutes of the challenge, describing in words what I was going to code.
link to this page in action, displaying 10 rows of pascal’s triangle:
http://w̶i̶e̶l̶d̶l̶i̶n̶u̶x̶.̶c̶o̶m̶/uploads/pascals.html
<html>
<head>
</head>
<body>
<script>
// start with two "1"s.
var pascalSeed=[1,1];
// make a loop that loops through 10 iterations, each time calling the function on itself with the previous row as input while outputting the next row.
var h=0;
while (h<10)
{
pascalSeed = computeNextPascalRow(pascalSeed);
h++;
}
// the function. it contains a loop that takes input: an array containing a series of integers representing a row of a pascals triangle, and outputs the corresponding series of integers calculated as the next row of the pascals triangle.
function computeNextPascalRow(pascalInputRow)
{
// for the first one have a special case: it is always "1" so write "1" to the output array.
var pascalResult=[1];
// for each iteration, calculate how many integers in this series.
var seriesLength = pascalInputRow.length;
// write the first special case to the screen.
document.write( pascalResult[0] + "-");
// step through the second through "n"th integers in the input series, have a loop to calculate this:
for (var i=1; i < seriesLength ; i++)
{
//add its value plus the value of the previous integer in the series, and output the result onto the output array.
pascalResult.push( pascalInputRow[i-1] + pascalInputRow[i] );
document.write( pascalResult[i] + "-");
}
// for the last one have a special case: it is always "1" so write "1" to the output array.
pascalResult.push(1);
document.write( pascalResult[pascalInputRow.length] + "<br>");
return pascalResult;
//end loop that inputs series of integers and outputs series of integers
}
/*
var pascalSeed=[1,3,3,1];
for (var h=0; h<10; h++)
{
computeNextPascalRow(pascalSeed);
}
function computeNextPascalRow(pascalInputRow)
{
var pascalResult=[1]; // for the first one have a special case: it is always "1" so write "1" to the output array.
// calculate the length of this series
var seriesLength = pascalInputRow.length;
// write the first special case to the screen.
document.write( pascalResult[0] + "-");
for (var i=1; i < seriesLength ; i++)
{
pascalResult.push( pascalInputRow[i-1] + pascalInputRow[i] );
document.write( pascalResult[i] + "-");
}
pascalResult.push(1);
document.write( pascalResult[pascalInputRow.length] + "<br>");
return pascalResult;
}
*/
/*
var pascalSeed=[1,3,3,1];
for (var h=0; h<10; h++)
{
computeNextPascalRow();
}
function computeNextPascalRow()
{
var pascalResult=[1]; // for the first one have a special case: it
is always "1" so write "1" to the output array.
// calculate the length of this series
var seriesLength = pascalSeed.length;
// write the first special case to the screen.
document.write( pascalResult[0] + "-");
for (var i=1; i < seriesLength ; i++)
{
pascalResult.push( pascalSeed[i-1] + pascalSeed[i] );
document.write( pascalResult[i] + "-");
}
pascalResult.push(1);
document.write( pascalResult[pascalSeed.length] + "<br>");
return pascalResult;
}
*/
/*
var pascalSeed=[1,3,3,1];
for (var h=0; h<10; h++)
{
// calculate the length of this series
var seriesLength = pascalSeed.length;
var pascalResult=[1];
document.write("series length: " + seriesLength + "<br>");
document.write( pascalResult[0] + " <br>");
for (var i=1; i < seriesLength ; i++)
{
pascalResult.push( pascalSeed[i-1] + pascalSeed[i] );
document.write( pascalResult[i] + " <br>");
}
pascalResult.push(1);
document.write( pascalResult[pascalSeed.length] + " <br>");
}
*/
/*
var pascalSeed=[1,3,3,1];
for (var h=0; h<10; h++)
{
// calculate the length of this series
var seriesLength = pascalSeed.length;
var pascalResult=[1];
document.write("series length: " + seriesLength + "<br>");
document.write(pascalResult[0] +" <br>");
for (var i=1; i < seriesLength ; i++)
{
pascalResult.push(pascalSeed[i-1] + pascalSeed[i]);
document.write( i + " " + pascalResult[i] +"<br>");
}
pascalResult.push(1);
document.write( pascalResult[pascalSeed.length] +" <br>");
}
*/
/*
for (var h=0; h<10; h++)
{
var pascalSeed=[1,1];
var pascalResult=[1];
// calculate the length of this series
var seriesLength = pascalSeed.length;
document.write(seriesLength + " <br>");
for (var i=0; i<seriesLength; i++)
{
pascalResult.push(777);
document.write( i + " " + pascalSeed[i] +"hi!<br>");
document.write( i + " " + pascalResult[i] +"hi!<br>");
}
}
*/
/* working...
var pascalSeed=[1,1,1,1,1,1,1,1,1,1];
for (var h=0; h<10; h++)
{
// calculate the length of this series
var seriesLength = pascalSeed.length;
document.write(seriesLength + " <br>");
for (var i=0; i<seriesLength; i++)
{
document.write( i + " " + pascalSeed[i] +"
hi!<br>");
}
document.write( h + " " + pascalSeed[h] +"
hi!<br>");
}
*/
/* working...
var pascalSeed=[1,1,1,1,1,1,1,1,1,1];
for (var h=0; h<10; h++)
{
for (var i=0; i<2; i++)
{
document.write( i + " " + pascalSeed[i] +"hi!<br>");
}
document.write( h + " " + pascalSeed[h] +"hi!<br>");
}
*/
/* working...
for (var i=0;i<10;i++)
{
document.write( i + "hi!<br>");
}
*/
/* from w3c -- thank you :) ...
for (var i=0;i<cars.length;i++)
{
document.write(cars[i] + "<br>");
}
*/
/*
1
11
121
1331
14641
15
okay so here's my plan:
------
start with two "1"s.
make a loop that loops through 10 (to start...) iterations.
the loop inputs a series of integers and outputs the corresponding series of integers.
for each iteration:
calculate how many integers in this series.
assign each integer in the series an ordinal number from 1 to n (n being the last one)
for the first one have a special case:
it is always "1" so write "1" to the output array.
have a cursor.
step through the input series.
for the second through "n"th integers in the input series, have a loop to calculate this:
add its value plus the value of the previous integer in the series, and output the result onto the output array.
for the last one have a special case:
it is always "1" so write "1" to the output array.
//end loop that inputs series of integers and outputs series of integ
------
*/
</script>
</body>
</html>
*2014-12-05 edit: previously published at http://w̶i̶e̶l̶d̶l̶i̶n̶u̶x̶.̶c̶o̶m̶/?p=73
[2020 edit: Moved to: https://i̶n̶v̶e̶s̶t̶o̶r̶w̶o̶r̶k̶e̶r̶.̶c̶o̶m̶/2014/... .html.]