5
I need some help with that...
We want to represent a ladder with variable height, using an array of strings.
For example, a ladder with height 3, we will represent with the following array:
var escada3 = [
" #",
" ##",
"###"
]
And a ladder with height 5, as follows:
var escada5 = [
" #",
" ##",
" ###",
" ####",
"#####"
]
Write a ladder function that uses a height (a number) and return an array that represents the corresponding ladder.
TIP
In Javascript you can repeat a text using repeat
as follows:
var degrau = "#".repeat(2); // agora degrau = "##";
This will serve to mount our steps;
But how do I insert the number of steps according to the number indicated by my result array? How to insert an element into an array?
I was able to solve part of the question by doing the repetitions, but he wants the white spaces, I don’t know how to add them, since I’ve tried " ".repeat
, but javascript does not repeat spaces this way.
function escada(numeroDegraus) {
var degrausEscada = [];
var comparacao = numeroDegraus;
for (let i = 1; i <= numeroDegraus; i++) {
var degraus = "#".repeat(i);
degrausEscada.push(degraus);
}
return degrausEscada;
}
console.log(escada(5));
"since I already tried " ". repeat, but javascript does not repeat spaces like this", No? I tested it here and it worked perfectly.
– Woss
It repeats only one space, unlike the one requested in the question, that when passed a value 5, would have to
– Douglas Morais