Last active
July 30, 2018 21:48
-
-
Save Oluwasetemi/7e05b1316b0a9aa49a3f30c27151edbd to your computer and use it in GitHub Desktop.
Pascal triangle
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
const pyramid = n => { | |
const oddNumberArray = Array.from({ length: n * 2 }) | |
.map((v, i) => i + 1) | |
.filter(v => { | |
if (v % 2 !== 0) return v | |
}) | |
let oddStringArray = numberToString(oddNumberArray) | |
oddStringArray.map(v => console.log(pad(v, (n * 2) - 1))) | |
} | |
const pad = (str, length, char = ' ') => { | |
return str.padStart((str.length + length) / 2, char) | |
.padEnd(length, char) | |
} | |
const numberToString = (arr, char = '#') => { | |
let str, result = []; | |
arr.forEach(element => { | |
for (let index = 1; index <= element; index++) | |
str = char.repeat(index) | |
result.push(str) | |
}) | |
return result | |
} | |
pyramid(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment