Basic JavaScript: Iterate with JavaScript While Loops

Basic JavaScript: Iterate with JavaScript While Loops
0

#1

Exercise URL - https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-while-loops

I am passing the lesson but not sure if I am doing something completely different or if my code is truly correct LOL!!!

Here is my code solution:

var myArray = [];
  
// Only change code below this line.

var myArray = [0,1,2,3,4];

while(myArray <= 5) {
  myArray.push[0,1,2,3,4];
  i++;
  }

#2

This is wrong. You are directly creating an array with the elements 0 to 4.

Your while loop doesn’t run, because the condition myArray <= 5 is false.

You are using a varibale i inside your loop that was never declared.

You could either declare a counter, like let i = 0, then use the counter value as a way to run the loops. Maybe while (i < 5) { do something; i++; }.
Or you could use the array.length property to run the loop. while (myArray.length < 5) myArray.push(myArray.length);

Should work…


#3

This works. Thanks you!