Basic JavaScript - Iterate with JavaScript While Loops

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

// Setup
const myArray = [];

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.2 Safari/605.1.15

Challenge: Basic JavaScript - Iterate with JavaScript While Loops

Link to the challenge:

The instruction: " Add the numbers 5 through 0 (inclusive) in descending order to myArray using a while loop."

You should get the following order of numbers:

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

At the moment you have the following:

[0, 1, 2, 3, 4]
  • Declare a variable i and initialize it with a max value, required by the instruction
  • Change the condition in the ‘while’ loop (i should be greater than or equal to the least number)
  • The push() method adds the specified elements to the end of an array and returns the new length of the array
  • Add the numbers 5 through 0 (inclusive) in descending order by changing the i variable - start from the greatest number and add the next (lesser) number (by subtracting i from the previous value, not by using addition i++).
  • Use the console. log() method ** at the end of the code to output a message to the web console** (to check if the printed result in the console is OK or not):
console.log(myArray);

P.S. Always explain what part of the instruction you do not understand. That way, we can provide a better explanation for the issue in question.

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