Here's the problem statement:
Given an integer value, print out Pascal's Triangle to the corresponding depth in row-major format:
Input Sample:
6
Output Sample:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
I need some practice with recursion, so I tried that approach:
var pascalDepth = parseInt(line, 10); //The input integer is provided on a single line as a text value, so this handles it.
var testTriangle = pascalTriangle(pascalDepth);
var output = [];
for(var i = 0; i < testTriangle.length; i++){
for(var j = 0; j < testTriangle[i].length; j++){
output.push(testTriangle[i][j]);
}
}
console.log(output.join(" "));
function pascalTriangle(totalLines, triangle) {
if (typeof triangle === 'undefined') {
triangle = [];
}
if (triangle.length === totalLines) {
return triangle;
}
if (triangle.length > 0) {
var triangleLine = [];
for (var i = 0; i < triangle.length; i++) {
if (typeof triangle[triangle.length - 1][i - 1] === 'undefined') {
triangleLine.push(1);
} else {
triangleLine.push((triangle[triangle.length - 1][i - 1] + triangle[triangle.length - 1][i]));
}
}
triangleLine.push(1);
triangle.push(triangleLine);
} else {
triangle.push([1]);
}
return pascalTriangle(totalLines, triangle);
}
triangle.length
andtriangle[triangle.length - 1]
. \$\endgroup\$