Search⌘ K
AI Features

Solution: Loops in JavaScript

Discover how to use loops in JavaScript to solve problems like the FizzBuzz challenge and a number guessing game. Learn through step-by-step solutions that explain the use of for and while loops, conditional statements, and user input handling. This lesson helps build practical programming skills for repetitive tasks.

We'll cover the following...

Solution 1

Here is a possible solution for checking if the number is a multiple of 33 or 55.

Javascript (babel-node)
for (let i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}

Explanation

  • Line 1: The for loop starts with i being initialized to 11. It will loop as long as i is less than or equal to 100100.

  • Lines 2–10: ...