Solving Fizz Buzz Using Javascript
Fizz Buzz is a common interview question where a program should print numbers from 1 to 20 but for multiples of 3 it will print Fizz instead of that number and for the multiples of five print Buzz and print FizzBuzz for the numbers which is multiple of 3 and 5 at the same time.
Input : Integer number
Output :
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz Buzz
16
17
Fizz
19
Buzz
Implementation #
function fizzbuzz(num) {
for (var i = 1; i <= num; i++) {
if (i % 15 == 0) console.log("FizzBuzz");
else if (i % 3 == 0) console.log("Fizz");
else if (i % 5 == 0) console.log("Buzz");
else console.log(i);
}
}
Main Point: Using modulus operator
The code is availalbe here : https://repl.it/@ahmd1/FizzBuzz-Algorithm
There are so many ways to solve the Fizz Buzz problem so feel free to submit your solution with any programming language.
Credits #
Photo by Christina Kirschnerova