Can someone help me understand what's wrong with my implementation of Pascal's Triangle in JavaScript? I came across a similar thread discussing recursion, but I'm having trouble figuring out the errors in my code. I would appreciate fresh eyes to pinpoint any mistakes or suggest improvements. Thank you!
var pascalTriangle = [[1]];
function generatePascal(num, pascalTriangle) {
if (num < 2) {
return pascalTriangle;
}
var newRow = [1];
var lastRow = pascalTriangle[pascalTriangle.length - 1];
console.log(lastRow);
for (var i = 1; i < lastRow.length; i++) {
var sum = lastRow[i] + lastRow[i - 1];
newRow.push(sum);
}
pascalTriangle.push(newRow);
return generatePascal(num - 1, pascalTriangle);
}
generatePascal(2, pascalTriangle);