print the numbers from 1 to 100, but only print the even numbers. use a while loop in javascript

The following code will print only even numbers from 1 to 100 using a while loop in JavaScript:

index.tsx
let num = 0;
while (num <= 100) {
  if (num % 2 === 0) {
    console.log(num);
  }
  num++;
}
94 chars
8 lines

Explanation:

  1. We initialize a variable called num to zero.
  2. We use a while loop to iterate over the numbers from 1 to 100. The loop will continue as long as num is less than or equal to 100.
  3. Inside the loop, we use an if statement to check if num is even. If it is, we print it to the console using the console.log() function.
  4. We increment num by 1 in each iteration of the loop until it reaches 100.

This code will output the even numbers from 1 to 100 to the console.

gistlibby LogSnag