Need some explaining

// Setup

const myArray = [];

let i = 10;

// Only change code below this line

do {

  myArray.push(i);

  i++;

}

while (i < 5) ;

console.log(myArray);

can you explain where “i” is suppose to = 11

Trace through the code line by line, keeping track of what that variable is.

1 Like

Let’s forget it’s some code and try to read it as plain english to keep it simpler :slight_smile:
The variable i starts as 10 right?
Next is no matter what put the current value of i in the array. Wouldn’t that make myArray [10]? then increase the value of i by 1.
That makes myArray [10] and i to be 11 right?
What’s the next step? is i < 5? It’s not because 11 > 5. So no reason to go again through the do…while loop and we move to the next statement which is the console.log(myArray)

I hope this helps :slight_smile:

1 Like

oh my heavens…wow i feel a little embarrassed now, i shouldve figured that out…I can tear apart a car engine and put it back together but didnt see this one…hahahaha , thank you

1 Like

Nothing to feel embarassed about man. Sometimes even after years of coding you stumble into simple code and you just need to stop trying, go for a walk and come back and try again with a fresh brain while keeping it simple. So keep fire burning you are doing great :slight_smile:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

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