Javascript Algroithm | Fizbuzz

Dark sphere

fizzbuzz problem is a very common coding question that is asked during coding interviews here the expected output is to iterate over a given number of times and print Fizz if the number is divisible by 3 and to print Buzz if the number is divisible by 5 and to print “FizzBuzz” if the number is divisible by both 3 and 5 or to just the print the number if it does not satisy any of the above conditions.

Well, know that we know what is the expected output. let me explain the solution I have written for this problem, First we define a function that expects an argument of count which would be the number of times we will iterate.

Then we define a for loop with the argument(count) as the maximum and then define an if condition to check whether the index(i) in the loop is divisible by both 3 and 5, if true we then print “fizzbuz” to the console.

After the If condition we define an else if condition which checks if the index(i) is divisible by 3 which makes us print “Fizz” to console, After the first Else If condition we define our second Else if condition which checks if the index is divisible by 5 when true will print the string “Buzz” to the console. And finally, we define an else statement to print the index(i).

This is the solution I have written for the fizzbuzz problem using javascript

function fizzBuzz(count){
   for(let i=0;i<count;i++){
       if(i % 3 == 0 && i % 5 == 0 ){
           console.log('Fizz Buzz')
       }
       else if(i % 3 == 0){
           console.log('Fizz')
       }
       else if(i % 5 == 0){
           console.log('Buzz')
       }
       else{
           console.log(i)
       }
   }
}
fizzBuzz(100)

Subscribe to UIUXTEK Newsletter

One update per week. All the latest posts directly in your inbox.