solve FizzBuzz problem
up to n iterations : print to console
[ switch | for-loop | modulo operator ]

FizzBuzz

Write a function that prints to console the fizzBuzz sequence for n iterations.


FizzBuzz is a challenge where traditionally, you loop from 1 to 100 and print out each number.

However, if the number is divisible by 3, you print out "Fizz"

If the number is divisible by 5, you print out "Buzz"

If the number is divisible by both 3 and 5, you print out "FizzBuzz"


Example ...

Enter number of iterations to run ...

15 → number

The function prints the fizzBuzz sequence for 15 iterations to the console.

1

2

Fizz

4

Buzz

Fizz

7

8

Fizz

Buzz

11

Fizz

13

14

FizzBuzz → printed to console

Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Perform FizzBuzz challenge using ↴

switch statement → perform different actions based on different conditions.

for loop → executes a block of code a number of times.

modulo operator % (remainder operator) → returns the remainder left over when one operand is divided by a second operand.


switch statement is used to perform different actions based on different conditions.

The value of the expression is compared with the values of each case

If there is a match, the associated block of code is executed.

If there is no match, the default code block is executed.

switch statement can replace multiple if checks.

The expression will be evaluated once, and then compared with the expressions that you define in each case clause, from top to bottom.

switch (expression) {

case value1:

// code block to be executed

// if expression matches value1

break;

case value2:

// code block to be executed

// if expression matches value2

break;

case value3:

// code block to be executed

// if expression matches value3

break;

default:

// code block to be executed

// if expression does not match any case

}

Boolean true can be used as the expression.

This means that each case will evaluate its condition as a boolean comparison.

let score = 72;

switch (true) { → true is used as the expression

case score >= 85:

console.log("Grade: A");

break;

case score >= 70:

console.log("Grade: B");

break;

case score >= 55:

console.log("Grade: C");

break;

case score >= 40:

console.log("Grade: D");

break;

default:

console.log("Grade: F");

} returns ↴

Grade: B → score >= 70


switch statement can be used inside a for loop.

In each iteration, the switch statement performs different actions based on the variable value set in the loop.


for loop repeatedly executes a block of code until a specified condition evaluates to false.

The loop runs a block of code a set number of times, defined by an initialization, a condition, and an increment.

for (let x = 0; x < 4; x++) {

console.log(x);

}

Loop variable x is initialized to 0

Condition x < 4 is checked before each iteration.

The loop will continue to run as long as x is less than 4

The loop repeatedly executes a block of code 4 times, from 0 to 3

For each iteration of the loop, the current value of x is printed to the console.

After each iteration, x is incremented by 1 x++

When x reaches 4 the condition evaluates to false, terminating the loop.

0

1

2

3 → printed to console


Modulo operator % (remainder operator) returns the remainder left over when one number is divided by a second number.

10 % 2 → remainder 0

10 % 3 → remainder 1

10 % 4 → remainder 2

10 % 5 → remainder 0

10 % 6 → remainder 4

10 % 7 → remainder 3

10 % 8 → remainder 2

10 % 9 → remainder 1

10 % 10 → remainder 0

10 % 2 === 010 is divisible by 2

10 % 5 === 010 is divisible by 5

10 % 10 === 010 is divisible by 10


Initialize a variable to set the upper limit for the FizzBuzz sequence.

const numLimit = 15; → user input


Define a function fizzBuzz to run the fizzBuzz challenge.

function fizzBuzz(num) {}

The function takes a number as input num and returns the fizzBuzz sequence to console for n iterations.

for loop repeatedly executes a block of code until a specified condition evaluates to false.

Loop through numbers from 1 to num

for (let x = 1; x <= num; x++) {} x

true is used as the expression, and each case is checked to see if it also evaluates to true.

switch (true) {}

Check if x is a multiple of both 3 and 5

case x % 15 === 0:

If true, print "FizzBuzz"

console.log("FizzBuzz")

break exit the switch statement

Check if x is a multiple of 3

case x % 3 === 0:

If true, print "Fizz"

console.log("Fizz")

break exit the switch statement

Check if x is a multiple of 5

case x % 5 === 0:

If true, print "Buzz"

console.log("Buzz")

break exit the switch statement

Default case for numbers that are not multiples of 3 or 5

Print x the number itself.

default:

console.log(x)


Call the function with ↴

fizzBuzz(numLimit);


Perform FizzBuzz for 15 iterations.

const numLimit = 15;

function fizzBuzz(num) {

for (let x = 1; x <= num; x++) {

switch (true) {

case x % 15 === 0:

console.log("FizzBuzz");

break;

case x % 3 === 0:

console.log("Fizz");

break;

case x % 5 === 0:

console.log("Buzz");

break;

default:

console.log(x);

}

}

}

call function

fizzBuzz(numLimit); returns ↴

1

2

Fizz

4

Buzz

Fizz

7

8

Fizz

Buzz

11

Fizz

13

14

FizzBuzz → printed to console


Alternative ↴

x % 3 === 0 && x % 5 === 0 same as ↴

x % 15 === 0

Solve FizzBuzz problem

Enter number of iterations to run