JavaScript: How to do a FizzBuzz programming task?
2 min readNov 6, 2022
FizzBuzz programming task is a general interview question where the candidate need to write a program that prints either Fizz or Buzz or FizzBuzz along with the number.
For example, if you have a range of numbers from 0 until 100 that prints,
- Fizz if the number is a multiple of 3
- Buzz if the number is a multiple of 5
- FizzBuzz if the number is a multiple of 15 or a multiple of both 3 and 5
Below is the code on how you can do it in JavaScript programming language,
// FizzBuzz challenge
for (var i = 0; i <= 100; i++) {
if (i % 15 === 0) {
console.log("FizzBuzz", i)
} else if (i % 5 === 0) {
console.log("Buzz", i)
} else if (i % 3 === 0) {
console.log("Fizz", i)
}
}
In the above code,
for
loop iterates by 1 and the loop starts with i 0 and ends when the value of i is 100.if
expression returns a value eithertrue
orfalse
if the number value ini
is a multiple or not.console.log
is a method for sending log output to the web console.
I hope my article helps you understand how to do a FizzBuzz programming task in JavaScript.
If you like my articles, please follow me. You can also support me by buying me a coffee.