JSFiddle - React, Tailwind, and code Playground
by johnny000
JavaScript
function pascal(col,row) {
console.log("CALC("+col+","+row+")");
// check if it's one value on the outside of
// the pyramid, if yes return 1
if (col == 0 || col == row + 1) {
return 1;
} else {
// previous row
var prevRow = [];
for (var pS = 0;pS < row; pS++) {
var currentRow = [];
for (var cols = 0; cols < prevRow.length+1; cols++) {
console.log("ROW:" + pS + " - COL:" + cols);
if (cols == 0 || cols == prevRow.length+1) {
currentRow.push(1);
} else {
currentRow.push(0);
}
}
prevRow = currentRow;
}
return prevRow[col];
}
}
console.log("pascal(0,2)=" + pascal(0,2));
console.log("pascal(1,2)=" + pascal(1,2));
console.log("pascal(1,3)=" + pascal(1,3));
console.log("pascal(3,7)=" + pascal(3,7));