The following is my solution to the Staircase problem. I’m using String.prototype.padStart
to add the appropriate number of spaces in order to build the rises and making sure to right-align the steps. I also use String.repeat()
to build the steps that make up the staircase.
Problem
Write a program that prints a staircase of size n
. Where n
is an integer.
Constraints
- 0 <
n
≤ 100
Output Format
Print a staircase of size n
using #
symbols and spaces.
NOTE The last line must have 0
spaces in it.
Solution
function staircase (n) {
for (let i = 1; i <= n; i++) {
const step = '#'.repeat(i); // create a step
const rise = String.prototype.padStart.call(step, n, ' '); // Add necessary spaces
console.log(rise);
}
}