solve FizzBuzz problem
up to n iterations : print to console
[ 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 ↴

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.


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.

Initialize a variable to hold the output message.

let message message

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++) {}

During each iteration of the loop, conditional statements determine when to assign -

"Fizz", "Buzz", "FizzBuzz" or the current number x itself.

If number x is divisible (with no remainder), the respective strings are assigned to message

Check if the current number x is divisible by both 3 and 5

if (x % 15 === 0)

If true, assign "FizzBuzz" to message

message = "FizzBuzz"

Check if number x is divisible by 3

else if (x % 3 === 0)

If true, assign "Fizz" to message

message = "Fizz"

Check if number x is divisible by 5

else if (x % 5 === 0)

If true, assign "Buzz" to message

message = "Buzz"

If x is not divisible by 3 or 5 assign the current number x itself.

Assign the number x to message

message = x

Print out message to console.

console.log(message)


Call the function with ↴

fizzBuzz(numLimit);


Perform FizzBuzz for 15 iterations.

const numLimit = 15;

function fizzBuzz(num) {

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

let message;

if (x % 15 === 0) {

message = "FizzBuzz";

} else if (x % 3 === 0) {

message = "Fizz";

} else if (x % 5 === 0) {

message = "Buzz";

} else {

message = x;

}

console.log(message);

}

}

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)


Alternative without using else if statements ↴

function fizzBuzz(num) {

let message;

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

message = "";

if (x % 3 === 0) message += "Fizz";

if (x % 5 === 0) message += "Buzz";

if (message === "") message += x;

console.log(message);

}

}

Solve FizzBuzz problem

Enter number of iterations to run