Created
September 12, 2019 16:56
-
-
Save averylizette/a4782fef3e974ffa362f319f817c748f to your computer and use it in GitHub Desktop.
Pascals Triangle
This file contains hidden or 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
/* | |
an array of arrays | |
n number of arrays | |
if its over | |
/* | |
Will want to create an array of n array's | |
Inside each array is n number of '1' so that it mimics the triangle formation | |
then I will want to loop through the inner arrays (triangle rows) | |
If there is 3 or more elements within the array { | |
I want to then look the the array at the previous index, and add together its element[0] and element[1]; | |
I will make that sum the value of the initial row[1] and then increment to the next row element. | |
I will then continue to add together element[0 + 1] and element[1 + 1] of the preceding row and repeat the process... | |
until whatever element element[i+1] elvauates to undefined | |
If it is undefinded then I will not change the row's last element. | |
I will repreat this process until I have gone through the eni | |
*/ | |
var pascalsTriangle = function(n) { | |
var start = [1]; | |
var triangle = [[1], [1, 1]]; | |
for (var i = 0; i < n-2; i++) { | |
triangle.push(start); | |
} | |
triangle[1].push(1); | |
//how to i access information in one triangle element, and then alter the information in element + 1; | |
//loop through each inner array of the triangle array | |
for (var row = 0; row < triangle.length; row++) { | |
// row is an array | |
var count = 0; | |
if ([row+1].length < 1){ | |
for (var block = 0; block <= row.length; block++){ | |
var count = 0; | |
count = row[block] + row[block +1]; | |
if (count){ | |
triangle[row + 1].push(count); | |
} else if (!count) { | |
triangle[row + 1].push(1) | |
} | |
} | |
//add together together arr[0] + 1 and then store that variable | |
} | |
//triangle[row].push(1); | |
} | |
console.log(triangle) | |
} | |
pascalsTriangle(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment