FizzBuzz369 views

AlgoPill
 function fizzBuzz(n){
    var i,output = [];
    for(i=0;i<=n;i++){
        if(i%15===0){
            output.push("FizzBuzz");
        } else if(i%3===0){
            output.push("Fizz");
        } else if(i%5===0){
            output.push("Buzz");
        } else {
            output.push(i);
        }
    }
    return output;
}

Problem Statement: Given a positive integer n, for all numbers between zero and n (inclusive) , output “Fizz” if number is a multiple of three, output “Buzz” if number is multiple of five, output “FizzBuzz” if number is a multiple of 15, otherwise output the number itself.

Solution: Solution is very simple. The only trick is to first test for 15 then for 5 and 3 because 15 is also a multiple of 3 and 5 and therefore all multiples of 15 are also multiple of 3 and/or 5.

Post a Comment

You must be logged in to post a comment.