solve FizzBuzz problem
up to n iterations : print to console
[ for-loop | modulo operator | ternary 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.

ternary operator → frequently used as an alternative to an if...else statement.


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


Ternary Operator

Ternary Operator is frequently used as an alternative to if...else statements.

if (condition) {

expressionIfTrue;

} else {

expressionIfFalse;

}

Ternary Operator takes three operands ↴

condition evaluates to true or false.

? question mark → expression to execute if the condition is truthy

: colon → expression to execute if the condition is falsy

condition ? expressionIfTrue : expressionIfFalse

let score = 75;

let result = score >= 55

? "You passed"

: "You failed";

console.log(result); returns ↴

You passed → first expression executed


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 a number of times.

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

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

Loop through numbers from 0 to num

for (let x = 0; x < num; ) {}update expression NOT included in the loop statement.

update expression increment happens within the console log statement.

++x increments x by 1

ensuring that x starts from 1 during the first iteration.

console.log((++x % 3 ? "" : "Fizz") + (x % 5 ? "" : "Buzz") || x );

truthy value is a value that is considered true when encountered in a Boolean context.

All values are truthy unless they are defined as falsy.

falsy values include 0

1 % 3 → evaluates to 1 true truthy

2 % 3 → evaluates to 2 true truthy

3 % 3 → evaluates to 0 false falsy

4 % 3 → evaluates to 1 true truthy

During each iteration of the loop, a ternary operator checks whether x is divisble by 3 or 5 or both.

Increment x by 1 and check if it is a multiple of 3

(++x % 3 ? "" : "Fizz")

If expression returns 0 (falsy), prepend "Fizz"

Otherwise return an empty string ""

Check if x is a multiple of 5

(x % 5 ? "" : "Buzz") || x

If expression returns 0 (falsy) append "Buzz"

Otherwise return an empty string ""

else if x is not divisible by 3 or 5, print the number x itself.

If x is a multiple of both 3 and 5, then "Fizz" + "Buzz"

are concatenated together to give "FizzBuzz"


Call the function with ↴

fizzBuzz(numLimit);


Perform FizzBuzz for 15 iterations.

const numLimit = 15;

function fizzBuzz(num) {

for (let x = 0; x < num; ) {

console.log((++x % 3 ? "" : "Fizz") + (x % 5 ? "" : "Buzz") || 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 method using a nested ternary operator ↴

const numLimit = 15;

function fizzBuzz(num) {

for (let x = 0; x <= num; ) {

console.log(x % 15 ? (x % 5 ? (x % 3 ? x : "Fizz") : "Buzz") : "FizzBuzz");

x++;

}

}

call function

fizzBuzz(numLimit); → printed to console

Solve FizzBuzz problem

Enter number of iterations to run