I faced this problem: I need to write a program that will display a staircase in the console using repeat. And the staircase should be displayed in both directions.
The question is, can I add a method repeat to the function, so that it outputs to the console in steps of characters, like here, for example:
' #'
' ###'
' #####'
' #######'
' #########'
' ###########'
Please advise me, or just give me a hint in what direction to dig. Below is the cycle that I use.
let i = 0;
let max = 6;
while (i < max) {
let space = "";
let tree = "";
for (let j = 0; j < max - i; j ) space = " ";
for (let j = 0; j < 2 * i 1; j ) tree = "#";
console.log(space tree)
i
}
CodePudding user response:
I am not very clear about your question. The following codes provide the same result as yours by the means of repeat function (String.prototype.repeat
).
let layer = 6;
for (let i = 0; i < layer; i) {
console.log(' '.repeat(layer - i) '#'.repeat(1 i * 2));
}