Programming Challenge 5/99 FizzBuzz

After a long break, I’m coming back with the next task on my challenge list.


<script>
    function fizz() {
    res = document.getElementById("res");
    txt = document.getElementById("text").value;
    num = parseInt(txt);
    if (num % 5 === 0 && num % 3 === 0){
    	res.innerText = "FizzBuzz";
    }
    else if (num % 3 === 0){
        res.innerText = "Fizz";
        return;
    }
    else if (num % 5 === 0){
        res.innerText = "Buzz";
        return;
    }  
    else {
    	res.innerText = num;
    }
	}
</script>
<input type="text" value="15" id="text"><input type="button" value="FizzBuzz" onclick="fizz()">
<br><p id="res"></p>

FizzBuzz is a good example of how we can use modulo operation.

As we know in FizzBuzz when the number is divisible by 3 players have to replace a number with the word Fizz and when the number is divisible by 5 with the word Buzz. Also when the number is divisible both by 3 and 5, word will be FizzBuzz.

Modulo operation gives us a remainder from division, so when it gives 0 the number is divisible.

Leave a Comment