Member-only story

JavaScript Algorithm: FizzBuzz

Your usual programming algorithm.

Max N
3 min readApr 20, 2020

We are going to write a function called fizzbuzz that will accept no arguments.

The goal of this function is to print out all numbers from 1 to 100 but with three exceptions:

  1. For every number that is divisible by 3 and 5, console log "FizzBuzz".
  2. For every number that is divisible by only 3 and not 5, console log "Fizz".
  3. For every number that is divisible by only 5 and not 3, console .log "Buzz".

Here is a partial example with numbers from 12 to 20:

...
"Fizz"
13
14
"FizzBuzz"
16
17
"Fizz"
19
"Buzz"
...

First, we create a for-loop that will count from 1 to 100.

for (let i = 1; i <= 100; i++) {}

Inside the for-loop, we will create a series of if...else statements. The first if-statement checks to see if the number is divisible by both 3 and 5. If that’s the case, we print "FizzBuzz" using console.log().

if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
}

Next, we create an else if statement. If the number doesn’t divide by both 3 and 5 but it is divisible by 3 only, we print out "Fizz".

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.

No responses yet