Guest User

Untitled

a guest
Jun 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. var generate = function(numRows) {
  2. // helper function that takes in the last level and then constructs the next level
  3. // takes in an array, spits out an array
  4. // sums i and i+1 , careful with the end length - 2
  5. var helper = function (arr) {
  6. var newArr = [1];
  7. for (var i = 0; i < arr.length - 1; i++) {
  8. newArr.push(arr[i] + arr[i+1]);
  9. }
  10. newArr.push(1);
  11. return newArr;
  12. }
  13.  
  14. //initialize tree
  15. var result = [];
  16. var currentArr = [1];
  17.  
  18. //for loop that loops through all levels
  19. for (var j = 0; j < numRows; j++) {
  20. result.push(currentArr);
  21. currentArr = helper(currentArr);
  22. }
  23. return result;
  24. };
Add Comment
Please, Sign In to add comment