Also can solve using only the repeat
you already have in your code. The idea is to first write a certain amount of spaces and then join with a number of #
that makes sense for the line on which it goes.
In a scenario where the final size is 5 characters, each line would have the following characters::
- 4 spaces, 1 #
- 3 spaces, 2 #
- 2 spaces, 3 #
- 1 space, 4 #
- 0 spaces, 5 #
Implementation:
let array = []
function exibeNumero(entrada) {
for (let i = 0; i < entrada; i++) {
array.push(" ".repeat(entrada - i - 1) + "#".repeat(i + 1))
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^
}
for (const entrada of array) {
console.log(entrada)
}
}
exibeNumero(5)
The only instruction changed was the one that puts the element in the array. Note that it was necessary to change the repeat of the #
so that the first time already write a character instead of zero.