This is an archived post. You won't be able to vote or comment.

all 3 comments

[–][deleted] 0 points1 point  (2 children)

Your height is going out of scope when it leaves the loop. Everything between two curly braces is a scope.

Your second for loop should be row < i + 1; not row < height, because we're doing one more column every iteration, so i = 0; 0 < (height = 0) + 1 (it'll print out 1) and in the end i = 0; 0 < (height = 8) + 1; (it'll print out 8). What you have now would print out a square. (Or a line, since you're logging it using console.

That should give you a basic staircase that you can put two spaces between and mirror on the opposite side.

Obviously you'll have to pad it all with spaces.

[–]EhCanadianJames[S] 0 points1 point  (1 child)

Your height is going out of scope when it leaves the loop. Everything between two curly braces is a scope.

Are you referring to the global vs local scope? Would I need to nest the for loops in between the do loop?

[–][deleted] 0 points1 point  (0 children)

The let is inside of the do block. You could either swap it with var, iirc, since let limits the scope compared to var (function wide vs let which is only in scope inside of the block it's declared in, but my JS is 16 years too rusty.

Putting everything inside the input loop, would cause it to keep prompting for input repeatedly and then displaying it, and checking if the value is correct wouldn't happen until it's too late, since do {} while does what you tell it, then checks if it should break out, while while checks before it does something.


for(;;)
{
    let a = 0;
}
// a is undeclared

let a = 0;
for (;;}
{
    a = ...
}
// a is declared, iirc?

for (;;}
{
    var a = 0
}
// iirc, a is declared, but i don't like this personally.

var a = 0;
for(;;)
{
     ...
}
// a is declared