Author: Theodore Odeluga
This is a Node command line project and Node.js can be downloaded from here
Instructions for running a command line program with Node.js can be found here
The source files for this project can be downloaded from here
Simply put, Fizz Buzz is an exercise for testing coding skills at the entry level. It’s a familiar feature with assessors and candidates in programming interviews.
What we’ll be doing here is creating a simple example to reveal its basic mechanism.
Here, we’ll be leveraging modulo, an operator used for finding divisional remainders.
Modulo can also be used for selecting equally divisible numbers between factors and their multiples.
The specific task is to print a numeric sequence with the word “Fizz” for all the multiples of 3, “Buzz” for all multiples of 5 and “Fizz Buzz” for all multiples of both 3 and 5.
Straightforward enough, so let’s get started.
The first point of order is to list all our variables.
let i;
let three = 3;
let five = 5;
let count = 0;
let limit = 100;
You’ll see that the main variables are named after their values.
Self-documentation is a useful habit to get into when creating objects in code.
As per the variables three and five, it’s great if an object simply does what it says on the tin and avoids the use of “magic numbers” – variables whose purpose isn’t clear despite an important role.
Once our variables are set up, we can open up the definition of the program’s only function – surprise, surprise named – FizzBuzz.
The next line opens a for loop which encompasses the bulk of the function and is driven by an iterator (I) which traverses the value of the critical number (limit) until it reaches a maximum which of course is defined by that critical number.
Limit is also a customizable feature and can be used to determine how much processing the program will perform and how many multiples output as a result.
The count variable increments throughout proceedings, providing values for the variables three and five to test for equal divisibility with modulo.
Let’s use the first conditional statement in the program as an example to illustrate.
if(count % three == 0){
console.log(count);
console.log('Fizz - multiple of 3');
}
As you can see above, if the test returns true, console.log outputs a message indicating the value generated by count is a multiple of 3.
This process repeats similar tests on other multiples divisible by the other factor (5) or both 3 and 5.
The result of all this is a list of outputs showing a series of multiple-factor relationships.
// Output:
3
Fizz - multiple of 3
5
Buzz - multiple of 5
6
Fizz - multiple of 3
9
Fizz - multiple of 3
10
Buzz - multiple of 5
12
Fizz - multiple of 3
15
Fizz - multiple of 3
15
Buzz - multiple of 5
15
FizzBuzz - multiple of both 3 and 5
Here’s the entire program for convenience.
let i;
let three = 3;
let five = 5;
let count = 0;
let limit = 100;
function FizzBuzz(){
for(i=0; i < limit; i++){
count++;
if(count % three == 0){
console.log(count);
console.log('Fizz - multiple of 3');
}
if(count % five == 0){
console.log(count);
console.log('Buzz - multiple of 5');
}
if(count % three == 0 && count % five == 0){
console.log(count);
console.log('FizzBuzz - multiple of both 3 and 5');
}
}
}
FizzBuzz();
Conclusion
Fizz Buzz is a great way to demonstrate your abilities with variables and functions. I hope this inspires you to try out more complex challenges. Many thanks for reading and please do revisit – there’ll be lot’s more interesting material to come. All the best.