Created
March 15, 2018 16:43
-
-
Save zacharyhill/8135e5840222b0a4a25724e011ccf87b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var generate = function(numRows) { | |
var rows = []; | |
for (var row = 1; row <= numRows; row++) { | |
rows.push([]); | |
var rowIndex = row - 1; | |
var numOfItems = row; | |
for (var itemNum = 1; itemNum <= numOfItems; itemNum++) { | |
if (itemNum === 1 || itemNum === numOfItems) { | |
rows[rowIndex].push(1); | |
} else { | |
var lastRow = rowIndex - 1; | |
var currentNum = rows[lastRow][itemNum - 2] + rows[lastRow][itemNum - 1]; | |
rows[rowIndex].push(currentNum); | |
} | |
} | |
} | |
return rows; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment