const rows = 3;
for (let i = rows - 1; i > 0; i--) {
let space = '';
for (let j = 1; j <= i; j++) {
space += ' ';
}
for (let k = 0; k < rows; k++) {
let str = '';
for (let l = 0; l <= k; l++) {
str += '*';
}
}
console.log(space + str);
}
Question posted in Javascript
A very good W3school tutorial can be found here.
A very good W3school tutorial can be found here.
4
Answers
Move your variable within the proper scope.
Your ‘str’ variable isn’t defined in the context where your console.log is. Quickest way would be to move ‘let str = "" ‘ up to where you declare ‘space’
You’re defining
str
inside the second innerfor
loop, so it will only be accessible within that loop. You can define it within the outer loop and then reset it within the inner loop, then your print statement will be able to access it.I’m guessing this is what you’re looking for.
In your case, the log function did not ‘see’ the
str
variable because it was used outside the third for-loop (for (let k = 0;...
).