In this exercise, we are asked to identify four sets of numbers.
- First, we need to find numbers divisible by the Least Common Multiple of 3 and 5.
- Second, we need to find numbers divisible only by 3.
- Third, we need to find numbers divisible only by 5.
- Lastly, we need to find numbers not divisible by the LCM, 3, or 5.
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.
- if i % 15 == 0, then index i = 'FizzBuzz'
- if i % 3 == 0, then index i = 'Fizz'
- if i % 5 == 0, then index i = 'Buzz'
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.