Created
November 16, 2015 14:55
-
-
Save jbsouvestre/ea0295ff0762aaedbca2 to your computer and use it in GitHub Desktop.
Pascal 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
| function getNextLine(previousLine){ | |
| let line = []; | |
| for(let i = 0; i < previousLine.length + 1; i++){ | |
| let prevValue = previousLine[i-1], | |
| value = previousLine[i]; | |
| line.push(prevValue + value || 1); | |
| } | |
| } | |
| function triangle(n){ | |
| let firstline = [1]; | |
| let lines = [firstline]; | |
| let templine = firstline; | |
| while(n--){ | |
| templine = getNextLine(templine); | |
| lines.push(templine); | |
| } | |
| return lines; | |
| } | |
| function displayTriangle(pascalTriangle){ | |
| let spacing = pascalTriangle.length - 1; | |
| for(let el of pascalTriangle){ | |
| console.log(new Array(spacing+1).join(' ') + el.join(' ') ); | |
| spacing -- ; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment