Learn Introductory JavaScript by Building a Pyramid Generator - Step 90

Tell us what’s happening:

Greetings! Please explain how the while loop body is executed if the continueLoop condition is false.

P.S. In the console, the pyramid is displayed correctly with this code. I checked it using AI, and similarly noted this error. I followed steps 86-90, but at the same time I was perplexed how it worked, contrary to theory.

Your code so far

const character = "#";
const count = 8;
const rows = [];

function padRow(rowNumber, rowCount) {
  return " ".repeat(rowCount - rowNumber) + character.repeat(2 * rowNumber - 1) + " ".repeat(rowCount - rowNumber);
}

// TODO: use a different type of loop
/*for (let i = 1; i <= count; i++) {
  rows.push(padRow(i, count));
}*/

let continueLoop = false;
let done = 0;


// User Editable Region

while (done < count) {
  done++;
  rows.push(padRow(done, count));

// User Editable Region

  if (done === count) {
    continueLoop = false;
  } 
}

let result = ""

for (const row of rows) {
  result = result + row + "\n";
}
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0

Challenge Information:

Learn Introductory JavaScript by Building a Pyramid Generator - Step 90

Hi. Welcome to the forum.

It works while continueLoop is set to false because that is what you have set it to do. If the condition is set to a value of false the code runs. If you set the condition to something else it will work if that condition is met.

There may be other ways to write the code but to pass the step you only enter what they have asked, don’t change any other code.

1 Like

continueLoop is not the condition of the loop, the condition is done < count, so when done < count is true the loop runs, when it is false it stops

1 Like