Iterate with javascript while loops help

Im completely stuck on [https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-while-loops] task, I am using the exact same method shown in the video and on the previous help thread yet I am getting the wrong answer somehow. Please help?

Your code so far


// Setup
var myArray = [];

// Only change code below this line
var i = 0;
while(i < 5) {
myArray.push(i);
i++;

}
console.log(myArray)

Challenge: Iterate with JavaScript While Loops

Link to the challenge:

Well, your loop produces

[ 0, 1, 2, 3, 4 ]

but you need

[ 5, 4, 3, 2, 1, 0 ]

Do you recall how to make a variable decrease in value?

By using i-- if iā€™m correct? I tried this but it just created an infinite loop that crashes my browser. But maybe Iā€™m just wrong here lol

Sure, you get an infinite loop if you check if i is less than some numberā€¦ but if you are decreasing i, shouldnā€™t you change your check?

There are a couple of steps here to make this change:

  1. You need to change where the index variable starts, what number it starts on.
  2. You need to change how it is checked, when the while loop knows when to stop.
  3. You need to change how the index variable is changed on each iteration.

If you mixed up one of those, you could get an infinite loop.

Why not log out the index variable on each iteration to see what is happening?

1 Like

Another clue, you ā€˜pushā€™ to the end of an array. You can do the opposite and place your values to the front of the array. That will decrease the values.

Ive now tried this method and gotten the correct numbers in the array, however I am still unsure how to make it count from 5 downwards, rather than from 0 upwards?

// Setup
var myArray = [];

// Only change code below this line
var i = 0;
while(i < 6) {
    myArray.push(i);
    i++;
   
    
}
console.log(myArray)

You already said how to make the variable decrease.

but you need to


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 (').

Another clue, you ā€˜pushā€™ to the end of an array. You can do the opposite ā€¦

Right, that would work, but the the instructions explicitly mention ā€œpushingā€. The purpose of the lesson is to learn about while loops, not to explore prototype methods.

1 Like
// Setup
var myArray = [];

// Only change code below this line
var i = 6;
while(i < 0) {
    myArray.push(i);
    i--;
   
    
}
console.log(myArray)

Sorry Iā€™m still really confused haha. Ive now tried this method and itā€™s just returning as a blank array?

Your issue is here:

while(i < 0) {

Since i starts out at 6, it starts out above 0 and fails the while test before it even begins. Instead of checking it it is less than 0, you should loop it while it is ā€¦

When I changed that, the loop started to work for me. Then I had to make a small change on the previous line to get it to pass.

Ohhhhhh Iā€™ve figured it out now yay!

Thanks for all of the help!

// Setup
var myArray = [];

// Only change code below this line
var i = 5;
while(i > -1) {
    myArray.push(i);
    i--;
   
    
}
console.log(myArray)

Cool, good job.

I wrapped it in spoiler tags since it is a working solution.

The only thing I would have done a little differently is that instead of running the loop while i is greater than -1, I would have looped it while i is greater than or equal to 0. Since weā€™re dealing with integers, the effect is the same, but semantically it makes more sense to me since 0 is what weā€™re targeting here, not -1. But yeah, they both work the same in this case.

Take a second and read through the code and understand how those three steps interact - initializing the index variable, checking the indexing variable, changing the indexing variable.

Good work, have fun on the next challenge.

2 Likes