The challenge
Write staircase(n) that prints n lines. Line 1 has one #, line 2 has two, and so on. Then call staircase(4).
#
##
###
####
Try it
Write your answer below, then press Check my answer. Your code runs right here in your browser, and the tests tell you what works.
It prints
What the tests check
- It prints:
# ## ### ####
Hints
Stuck? Open one hint at a time. Try again after each one.
Hint 1
An outer loop for each line, and an inner loop (or "#".repeat(i)) to build that line.
A solution
There’s always more than one right way. Read it, make sure you understand it, then try writing it yourself without looking.
Show one possible solution
function staircase(n) {
for (let i = 1; i <= n; i++) {
let line = "";
for (let j = 0; j < i; j++) {
line += "#";
}
console.log(line);
}
}
staircase(4);
Learn what you need
This exercise uses what Grade 5: Loops: Doing Things Over and Over teaches. Read those notes first if any of it is new.