I prefer use functional and prototypical inheritance over native loops because it's cleaner. And I use MAP because ES6 feature is far more short and useful, though MAP has no side effects.
const arrreversedArr = (n) => Array.from(Array(n).keys()).reverse()
const staircase = (n) => arrreversedArr(n).map((value, index) => {
return console.log(' '.repeat(value)+'#'.repeat(index+1)+'\n')
})
staircase(50)
func is a output of a logic: Array.from(Array(n).keys()).reverse()
that receives n:integer
parameter.
Array.from is a new syntax from ES6, it means that you can fill an array from "here" to "there" ie: Array.from(0, 6)
returns [0,1,2,3,4,5,6]
so all the staircase(n)
return is a output of map of this array. Such array that receive Array(n).keys()
and reverses it with prototype reverse()
so that the array could be: [6,5,4,3,2,1,0]
instead.
the map repeat iterate over the array created from Array.from(Array(n).keys()).reverse()
and return a log for each item such as every item repeats ' '
(blank space) + "#" at the end of this string.