The “FizzBuzz” problem

On September 22, 2015, in Code, Javascript, Work - out of office, by sailesh

In the FizzBuzz problem, we are printing a sequence of positive integers up to a limit given, replacing multiples of 3 with “Fizz”, multiples of 5 with “Buzz”, and replacing multiples of both 3 and 5 with “FizzBuzz”.

  • Enter an Upper Limit:
  • Generated Sequence:

Javascript Code:

function fizzBuzz(upperLimit) {
    var limit = upperLimit || 100; // Getting a limit or default value
    var str = "";
    for (var i = 1; i <= limit; i++) {
        str = str 
              + ((i % 3 == 0) 
                ? ((i % 5 == 0) ? "FizzBuzz" : "Fizz") 
                : ((i % 5 == 0) ? "Buzz" : i)) + ", ";
    }
    return (str + "...");
}

 

Leave a Reply

Enable Google Transliteration.(To type in English, press Ctrl+g)