FizzBuzz Solution – Check is multiple of using JavaScript

Read a number and check if multiple of 3 or 5 and print outputs as required. Given a number n, for each integer I in the range from 1 to n inclusive, print one value per line as follows:

  • If i is a multiple of both 3 and 5 , print FizzBuzz
  • If i is a multiple of 3 (but not 5) Print Fizz
  • If i is a multiple of 5 (but not 3) Print Buzz
  • If i is a multiple of 3 and 5, print the value of i

Solution:

function fizzBuzz(n) {
    for(let i =1; i<=n ; i++){
        if(i%3 == 0 && i%5 == 0)
            console.log("FizzBuzz")
        else if(i%3 == 0)
            console.log("Fizz")
        else if(i%5 == 0)
            console.log("Buzz")
        else
            console.log(i)
    }
}

function main() {
    const n = parseInt(readLine().trim(), 10);

    fizzBuzz(n);
}

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *