Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var generate = function(numRows) {
- // helper function that takes in the last level and then constructs the next level
- // takes in an array, spits out an array
- // sums i and i+1 , careful with the end length - 2
- var helper = function (arr) {
- var newArr = [1];
- for (var i = 0; i < arr.length - 1; i++) {
- newArr.push(arr[i] + arr[i+1]);
- }
- newArr.push(1);
- return newArr;
- }
- //initialize tree
- var result = [];
- var currentArr = [1];
- //for loop that loops through all levels
- for (var j = 0; j < numRows; j++) {
- result.push(currentArr);
- currentArr = helper(currentArr);
- }
- return result;
- };
Add Comment
Please, Sign In to add comment