solve FizzBuzz problem
up to n iterations : return as an array
[ for-loop | modulo operator | push ]

FizzBuzz problem

Write a function that returns an array with 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 returns the fizzBuzz sequence for 15 iterations, as an array.

[1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]

Arrays are used to store multiple values in a single variable.

Each value is called an element, and each element has a numeric position in the array, known as its index.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Arrays can contain any data type, including numbers, strings, and objects.

const arr1 = [2, 4, 6]; array

arr1[0]; element at index 0 → 2

arr1[1]; element at index 1 → 4

arr1[2]; element at index 2 → 6

arr1[3]; element at index 3 → undefined index not found


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.

push() method → adds specified elements to the end of an array and returns the new length of the array.


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.

const arr = []; → empty array

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

arr.push(x);

}

console.log(arr);

Initialize an empty array arr

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 pushed to the end of the array.

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] → array returned


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


push() method adds new elements to the end of an array.

Add 4 to end of array.

const arr2 = [1, 2, 3];

arr2.push(4);

console.log(arr2); returns ↴

[1, 2, 3, 4]4 added to end of array

The push() method changes the length of the array.

arr2 is modified.

Using the spread operator creates a new array.

Add 4 to a new array.

const arr3 = [1, 2 , 3];

const arr4 = [...arr3, 4];

console.log(arr4); returns ↴

[1, 2, 3, 4]4 added to new array

console.log(arr3); returns ↴

[1, 2 ,3]

arr3 remains unchanged.


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 an array with the fizzBuzz sequence for n iterations.

Initialize an empty array to store the result.

const arr = [] arr

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 append -

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

If number x is divisible (with no remainder), the respective strings are appended to the output array arr

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

if (x % 3 === 0 && x % 5 === 0)

If true, add "FizzBuzz" to the array arr

arr.push("FizzBuzz")

Check if number x is divisible by 3

else if (x % 3 === 0)

If true, add "Fizz" to the array arr

arr.push("Fizz")

Check if number x is divisible by 5

else if (x % 5 === 0)

If true, add "Buzz" to the array arr

arr.push("Buzz")

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

Add the number x to the array arr

arr.push(x)

Return the array containing the FizzBuzz results.

return arr


Call the function with ↴

fizzBuzz(numLimit);


Perform FizzBuzz for 15 iterations.

const numLimit = 15;

function fizzBuzz(num) {

const arr = [];

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

if (x % 3 === 0 && x % 5 === 0) {

arr.push("FizzBuzz");

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

arr.push("Fizz");

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

arr.push("Buzz");

} else {

arr.push(x);

}

}

return arr;

}

call function

fizzBuzz(numLimit); returns ↴

[1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]


Alternative ↴

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

(x % 15 === 0)

Solve FizzBuzz problem

Enter number of iterations to run