Iterate with JavaScript Do...While Loops (need help)

Tell us what’s happening:
Change the while loop in the code to a do...while loop so that the loop will push the number 10 to myArray , and i will be equal to 11 when your code finishes running. My code will not pass the test. Please help.

Your code so far


// Setup
var myArray = [];
var i = 10;

// Only change code below this line.
do {
  myArr.push(i);
  i++;
} while (1 < 10);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-do---while-loops/

well in your case integer 1 is always less than integer 10 so that will always result to true.

change integer 1 to i and the code should pass

You have an infinite loop, this is always true

Do not use var, use const and let instead :slight_smile:

@ilenia I changed the 1 to i and it still doesn’t work.

@Nabi - the var declarations are part of the code i can’t change. did you mean the “myArr”?

Check your variables… they don’t match

// Setup
var myArr = []; //check that variable names are spelled properly
var i = 10;

// Only change code below this line.
do {
  myArr.push(i);
  i++; // changed 1 to 11
} while (i < 10); // since i started at 10 the loop will end before it does anything
// while (i < 11); // runs as long as i is less than 11
// while (i <= 10); // also works

I changed it to this and it says that myArr is not defined. I feel really stupid about this.

// Setup

var myArray = [];

var i = 10;

// Only change code below this line.

do {

myArr.push(i);

i++;

} while (i < 11);

it should be var myArr = []

thats in the section of code that i can’t change.

then this should be myArray.push(i)

also dont feel stupid. its actually a really common mistake

sorry. just caught that. now it’s saying:

myArray should equal [10].

i should equal 11

works for me

var myArray = [];
var i = 10;

// Only change code below this line.
do {
    myArray.push(i);
    i++;
} while (i < 11);

console.log(myArray)
console.log(i)

I don’t understand how this passed, but here was the code. maybe this is messed up by FCC?

// Setup

var myArray = [];

var i = 10;

// Only change code below this line.

do {

myArray.push(i);

i++;

} while (i < 5);

lol … who knows man

Has this happened to you before?

i think some of the questions are notoriously buggy but its hard to know which.

as long as you get the result they were looking for even if the tests dont pass youre good. start using console.log more

will do. thank you for your help!

oh ok i see. its not bugged. you were suppose to change 5 to 11

i can’t believe anybody learns from FCC. more often than not i get the answer, paste it in, and then TRY and understand it. I learn more from udemy courses.