Iterate with JavaScript Do...While Loops (Why do I need to erase the code inside the while loop)

Iterate with JavaScript Do...While Loops (Why do I need to erase the code inside the while loop)
0.0 0

#1

Tell us what’s happening:
Why do I need to erase the code inside the while loop

{
myArray.push(i);
i++;
}

if the do…while method is meant to run the code at least once?

I’m under the impression the do code should run once, then checks the while condition which in this case is false and so fulfills the pass conditions.

Your code so far


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

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

Your browser information:

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

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


#2

Be careful you’re writing your do...while loop incorrectly, here is the correct syntax :

do {
 // Something
} while (condition);

The challenge requires you to push only once the number 10 in myArray. As the do...while executes only once, no matter the condition being passed in the while statement, your code will be executed at least once.

Just delete your while statement, write correctly your do...while statement and you’re good to go!


#3

I understand the pass conditions require the code to look like this:

do {
myArray.push(i);
i++;
}
while (i < 5);

The code will run at least once before checking the while condition.

But I don’t understand why the while statement needs to be erased.

I also ran the code with and without the while statements. With the while statement the code returns [10, 11] and without the statement the code returns [10].

But the condition i < 5 isn’t being met. And since var i is initialized to 10 then nothing in the while statement should run. But it seems I’m missing something since the return statements are different when including versus not including the while statement.


#4

Hi,
Javascript ignores the white space and indentation (but we humans don’t).

Below is your code with a few line breaks thrown in to show more clearly what is happening.

var myArray = [];
var i = 10;

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

{
  myArray.push(i);   //push i = 11
  i++;  // now i = 12
}

console.log(i) // 12
console.log(myArray) // [10,11]

Having a do without a while would be a syntax error

// this won't run
do {
  myArray.push(i);
  i++;
}

//this will run
do {
  myArray.push(i);
  i++;
}

while (i < 5) 

#5

The part I don’t understand is if the while condition isn’t met (i < 5) why is the while statement running?


#6

Hi,

The code I copied is yours, not mine. I just put a line break in for clarity but the code executes just the same.

You do not have a while statement. You have a do...while statement followed by some code wrapped in { }. It just doesn’t appear that way because of where your lines break.

  1. The do…while executes only once because the condition is not true.
  2. Then you have two lines of code enclosed in curly braces { }

#7

So what I think you’re saying is this one code:

do {
myArray.push(i);
i++;
}
while (i < 5);

and this:

{
myArray.push(i);
i++;
}

is another code. Am I correct?


#8

Yes! That is correct. I know it doesn’t look that way but that is how the JavaScript engine is interpreting that code


#9

Thanks so much! This clears up my confusion.


#10

You are very welcome! I’m glad I was able to help