solve FizzBuzz problem
up to n iterations : return as an array
[ recursion | modulus operator | length | push | join ]

FizzBuzz

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 ↴

recursion → programming technique where a function calls itself repeatedly to solve a problem.

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.

length property → sets or returns the number of elements in an array.

join() method → returns an array as a string.


Recursion The act of a function calling itself.

Recursion is used to solve problems that contain smaller sub-problems.

A recursive function can receive two inputs: a base case (ends recursion) or a recursive case (resumes recursion).

Use recursion to find the factorial of 5.

let x = 5;

function factorial(num) {

if (num > 1) { Recursive call

return num * factorial(num - 1);

}

else { Base case

return 1;

};

}

call function

factorial(x); returns ↴

120 factorial of 5 → 120


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.


join() method joins all elements of an array into a single string with a specified separator between each element. The original array is unchanged.

("") separator → returns a string joined with no spaces between each character.

(" ") separator → returns string joined with a single space between each element.

const arr5 = ["H", "e", "l", "l", "o"]; array

arr5.join(""); returns ↴

"Hello" → string

const arr6 = ["Hello", "World"]; array

arr6.join(" "); returns ↴

"Hello World" → string


length property returns the number of elements in an array.

const arr7 = [1, 2, 3, 4, 5, 6];

arr7.length; returns ↴

6 → there are 6 elements in the array


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, current = 1, results = []) {}

The function takes a number as input num user input,

current current number being processed, default value is 1

results array to accumulate the results, default is as an empty array []

The function returns an array with the fizzBuzz sequence for n iterations.

Base case

If current exceeds num, return the results array and end execution of function.

if (current > num) {

return results

}

Recursive call

If current is less than or equal to num, initialize an array to hold "Fizz", "Buzz", or the current number.

let output = [] output → array

Check if current is a multiple of 3

If true, add "Fizz" to output array.

if (current % 3 === 0) output.push("Fizz")

Check if current is a multiple of 5

If true, add to "Buzz" to output array.

if (current % 5 === 0) output.push("Buzz")

If the output array is empty it will have a length of 0

current is neither divisible by 3 nor 5

if (output.length === 0) {}

Add the current number to results

results.push(current)

else if array is NOT empty

Concatenate output array elements into a string and add to results

results.push(output.join(""))

join("") adds "Fizz" to "Buzz" without spaces to get "FizzBuzz"

RECURSIVE CALL to process the next number.

return fizzBuzz(num, current + 1, results);


FizzBuzz for first 7 iterations ↴

Iteration ↴

1 [1]

2 [1, 2]

3 [1, 2, "Fizz"]

4 [1, 2, "Fizz", 4]

5 [1, 2, "Fizz", 4, "Buzz"]

6 [1, 2, "Fizz", 4, "Buzz", "Fizz"]

8 [1, 2, "Fizz", 4, "Buzz", "Fizz", 7]


Call the function with ↴

fizzBuzz(numLimit);


Perform FizzBuzz for 15 iterations.

const numLimit = 15;

function fizzBuzz(num, current = 1, results = []) {

if (current > num) {

return results;

}

let output = [];

if (current % 3 === 0) output.push("Fizz");

if (current % 5 === 0) output.push("Buzz");

if (output.length === 0) {

results.push(current);

} else {

results.push(output.join(""));

}

return fizzBuzz(num, current + 1, results);

}

call function

fizzBuzz(numLimit); returns ↴

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


Alternative - recursion backwards | print to console ↴

const numLimit = 15;

function fizzBuzzBackward(num) {

if (num === 0) return;

if (num % 15 === 0) console.log("FizzBuzz");

else if (num % 5 === 0) console.log("Buzz");

else if (num % 3 === 0) console.log("Fizz");

else console.log(num);

fizzBuzzBackward(num - 1);

}

call function

fizzBuzzBackward(numLimit); → print to console

Solve FizzBuzz problem

Enter number of iterations to run