Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Oluwasetemi
Last active July 30, 2018 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oluwasetemi/7e05b1316b0a9aa49a3f30c27151edbd to your computer and use it in GitHub Desktop.
Save Oluwasetemi/7e05b1316b0a9aa49a3f30c27151edbd to your computer and use it in GitHub Desktop.
Pascal triangle
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