Guest User

Untitled

a guest
Jun 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. /*
  2. i/p = 5;
  3. o/p = nested array of length 5
  4. cons- none
  5. edge cases- none
  6.  
  7.  
  8. just- create arrays that increases in length as it iterates
  9. from 0 to n. For each iteration add up the prev elements to
  10. create new element
  11.  
  12. visulization:
  13. as above
  14.  
  15. pseudocode:
  16. // create a place holder array = main arr
  17. //place first elem = [1] in main arr
  18. // use for loop for numrows iterations
  19. //for iteration 2,
  20. //crete temp arr = [];
  21. // push temparr [1,1]
  22. //arr [0] = 1,
  23. //arr [1] = temparr[0]+[1];
  24. //arr.length-1 = [1;]
  25.  
  26. transformation steps
  27.  
  28. 1 | [1,1] | [1,1]
  29. 2 | [1,1] | [1,2,1]
  30. 3 | [1,2,1] | [1,3,3,1]
  31. 4 | [1,3,3,1] | [1,4,6,4,1]
  32.  
  33. */
  34. var generate = function(numRows) {
  35. var results = [];
  36. var firstArr = [];
  37. firstArr[0] = 1;
  38. results.push(firstArr);
  39. var count = 0;
  40. var tempArr;
  41. for (var i = 0; i < numRows; i++) {
  42. tempArr = [1];
  43. for (var j = 1; j < results[i].length; j++) {
  44. tempArr.push(results[i][j] + results[i][j-1])
  45. }
  46. tempArr.push(1);
  47. results.push(tempArr);
  48. }
  49.  
  50. return results;
  51. };
  52.  
  53.  
  54. // for (var i = 2; i <= numRows; i++) {
  55. // var tempArr = [];
  56. // tempArr[0] = 1;
  57. // tempArr[i-1] = 1;
  58. // if(i === 2) {
  59. // results.push(tempArr);
  60. // }
  61. // var x = i-2;
  62. // var holder = [];
  63. // for (var j=3; j < numRows; j++) {
  64. // holder[x] = tempArr[j-2] + tempArr[j-1];
  65. // holder.concat(tempArr)
  66. // }
  67. // }
  68. // results.push(tempArr);
  69.  
  70. // }
Add Comment
Please, Sign In to add comment