Skip to content

Instantly share code, notes, and snippets.

@rebz135
Created August 9, 2018 16:39
Show Gist options
  • Select an option

  • Save rebz135/efd620f684c3e9b513b91b5f75309522 to your computer and use it in GitHub Desktop.

Select an option

Save rebz135/efd620f684c3e9b513b91b5f75309522 to your computer and use it in GitHub Desktop.
Pascal's triangle solution
var generate = function(numRows) {
let result = [];
let recursiveFunc = function (roundsLeft = numRows, oldArr = []) {
let newArr = [];
newArr.push(1);
if (!roundsLeft) {
return;
}
for (let i=0; i<oldArr.length; i++) {
if (oldArr[i+1]) {
newArr.push(oldArr[i] + oldArr[i+1])
} else {
newArr.push(1);
}
}
result.push(newArr);
recursiveFunc(roundsLeft-1, newArr);
}
recursiveFunc();
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment