Skip to content

Instantly share code, notes, and snippets.

@jbsouvestre
Created November 16, 2015 14:55
Show Gist options
  • Select an option

  • Save jbsouvestre/ea0295ff0762aaedbca2 to your computer and use it in GitHub Desktop.

Select an option

Save jbsouvestre/ea0295ff0762aaedbca2 to your computer and use it in GitHub Desktop.
Pascal Triangle
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