FizzBuzz

I will solve the classic FizzBuzz exercise using html, css, and javascript.

FizzBuzz Exercise Description

Given an integer 'n', return a string array where:

  1. answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
  2. answer[i] == "Fizz" if i is divisible by 3.
  3. answer[i] == "Buzz" if i is divisible by 5.
  4. answer[i] == i (as a string) if none of the above conditions are true.
  5. 0 < n < 10^4.

FizzBuzz Exercise Logic

In this exercise, we are asked to identify four sets of numbers.


In order to reduce the number of comparisons, we can calculate beforehand the LCM of 3 and 5. This turns out to be 15. In order to check if a number is divisible by our key numbers, we can use the modulo operation. As we remember, the modulo operation returns the remainder of the Euclidean division in the format a % b where a is the dividend and b is the divisor. We can then set up our comparisons displayed below in pseudo code.
In order to have an interactive demonstration, I will parse the user input. I must ensure that the user input is indeed a number and that the number exists in the constraint 0 < n < 10^4. Lastly, I must display the array on the webpage. I will use two javascript functions, the first one will check for proper input, and the second function will execute the exercise logic.

FizzBuzz Exercise Demonstration

Please enter a number 'n' where 0 < n < 10^4