Learn Introductory JavaScript by Building a Pyramid Generator - Step 94

Tell us what’s happening:

My code:

let done = 0;
while (done <= count) {
  done++;
  rows.push(padRow(done, count));
}

The error:


// running tests

  1. Your while loop should check if done is less than or equal to count.
    // tests completed

I started with !== instead of <=. When I look up the step here and elsewhere it keeps saying to drop the while loop and use a for loop, but the error specifically states to use the while loop.
Even if I replace it all with a for loop it then fails completely.

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));
}*/


// User Editable Region

let done = 0;

while (done !== count) {
  done++;
  rows.push(padRow(done, count));
}

// User Editable Region


let result = ""

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

console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0

Challenge Information:

Learn Introductory JavaScript by Building a Pyramid Generator - Step 94

I can’t figure out what your issue is, can you be a bit clearer? The first code is correct and will pass all tests, the second one is the default, where you’re required to change the operator, furthermore, I did not see an error asking you to use a for loop.

1 Like

The first code fails with the error right below it.

When I change the code to show:

let done = 0;

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

Before I run the check the terminal shows:

RangeError: repeat count must be non-negative

After I run the check I get:
Sorry, your code does not pass. Keep trying.

Your while loop should check if done is less than or equal to count.

No error says to use a for loop, but two places here in the forum says to use a for loop. When I started looking elsewhere for an answer I found a github walkthrough that shows a for loop as well.

I have also tried putting multiple conditions in the while including using one for = the other with <= or -=, everything shows the range error before the check and the ‘less than or equal’ after the check. After research I found you can’t do that, then after trying the for loops and failing I came here.

I do not know what is going on… I just reset the exercise again, made the above comment while copy/pasting the errors as I was getting them. Right after posting the comment I went back and clicked the submit button a few times just for the heck of it and now it says it passed.

I did not change the code from when it failed just a few minutes ago. I have reset this probably 10 times or more over the last week and tried it the way it looks now several times and it always failed, but now it is fine.

Sorry to bug and thank you for looking at it!

Your code worked for me. I got a range error when I input it but it passed the test anyway. Perhaps do a hard refresh or clear your browser and any extensions.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.