Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.2 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
cf1111c1c11feddfaeb1bdef Iterate with JavaScript While Loops 1 18220 iterate-with-javascript-while-loops

--description--

You can run the same code multiple times by using a loop.

The first type of loop we will learn is called a while loop because it runs while a specified condition is true and stops once that condition is no longer true.

const ourArray = [];
let i = 0;

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

In the code example above, the while loop will execute 5 times and append the numbers 0 through 4 to ourArray.

Let's try getting a while loop to work by pushing values to an array.

--instructions--

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

--hints--

You should be using a while loop for this.

assert(__helpers.removeJSComments(code).match(/while/g));

myArray should equal [5, 4, 3, 2, 1, 0].

assert.deepEqual(myArray, [5, 4, 3, 2, 1, 0]);

--seed--

--seed-contents--

// Setup
const myArray = [];

// Only change code below this line

--solutions--

const myArray = [];
let i = 5;
while (i >= 0) {
  myArray.push(i);
  i--;
}