Objective
Given a positive integer as an input print:
- 'fizz' if the number is a multiple of 3,
- 'buzz' if the number is a multiple of 5,
- 'fizzbuzz' if the number is a multiple of both 3 && 5,
- the same input for all other cases.
Input/Output
- fizzBuzz(n) where n=1 prints 1
- fizzBuzz(n) where n=6 prints 'fizz'
- fizzBuzz(n) where n=10 prints 'buzz'
- fizzBuzz(n) where n=15 prints 'fizzbuzz'
- fizzBuzz(n) where n=7 prints 7
Clarifications
Assumptions
- Consider only the cases for positive numbers.
- Will print the same input argument for all other cases that doesn't match either or both multiple of both 3 && 5.
Constraints
- Consider cases for only positive integer.
- For all other cases print the same input number.
- Prevent stack overflow.
Edge Cases
- Negative Number
- 0
- Null
- Undefined
- NaN (Not a Number)
- String
- Fraction
- Argument Length
- OverFlow
- Underflow
Algorithm
In programming we've two very important operators:
- Division ( / ) operator yields quotient.
- Modulus ( % ) operator yields remainder.
The crux of the algorithm is knowing if the input is exactly divisibley by 3 or 5 or both. When a number is exactly divisible the remainder is 0. With this information let's come up with pseudocode.
Fizzbuzz Pseudocode
function fizzBuzz(num) {
IF num is not a number OR num < 1 OR num is not an integer
RETURN num
ELSE IF num is divisible by 3 AND divisible by 5
RETURN "fizzbuzz"
ELSE IF num is divisible by 3:
RETURN "fizz"
ELSE IF num is divisible by 5:
RETURN "buzz"
ELSE
RETURN num
}
Using the pseudocoe we'll write the fully functional fizzbuzz algorithm as follows:
FlowChart
Here's the flowchart to walk you through the Fizzbuzz program in a visual format.
Fizzbuzz Javascript Code
Here's the Javascript implementation of the fizzbuzz algorithm.
const fizzBuzz = (num) => {
if (typeof num !== 'number' || num < 1 || !Number.isInteger(num)) {
return num;
} else if (num % 3 === 0 && num % 5 === 0) {
return 'fizzbuzz';
} else if (num % 3 === 0) {
return 'fizz';
} else if (num % 5 === 0) {
return 'buzz';
} else {
return num;
}
};
Time/Space Complexity
The time complexity of our algorithm for fizzbuzz(number) is linear since we make a single iteration. Also, since we do not use any extra space therefore space complexity of this algorthim is constant.
- Time O(n) = n, Linear.
- Space O(n) = 1, Constant.
Unit Test
Next, we'll make use of Jest, "a delightful Javascript Testing Framework" to test our algorithm. In particular, we'll test all the following cases:
- constraints
- edge cases
- regular tests
Dependency
Since we are using Jest, we must have Node.js® and Jest installed. We'll also house all tests under __tests__ folder.
const { fizzBuzz } = require('../fizzBuzz');
const fizzBuzz = [
['Hello', 'Hello'],
[undefined, undefined],
[null, null],
[-30, -30],
[0, 0],
['1', '1'],
[3.1415, 3.1415],
[1, 1],
[2, 2],
[3, 'fizz'],
[4, 4],
[5, 'buzz'],
[6, 'fizz'],
[7, 7],
[8, 8],
[9, 'fizz'],
[10, 'buzz'],
[11, 11],
[12, 'fizz'],
[13, 13],
[14, 14],
[15, 'fizzbuzz'],
];
describe('\nfizzBuzz Test', () => {
test.each(fizzBuzzTestCases)(
'given %p as argument returns %p',
(firstArg, expectedResult) => {
const actual = fizzBuzz(firstArg);
expect(actual).toEqual(expectedResult);
}
);
it('fizzBuzz() function should exists', () => {
expect(typeof fizzBuzz).toBe('function');
});
it('fizzBuzz() function expects exactly 1 argument', () => {
expect(fizzBuzz.length).toBe(1);
});
});
Technologies
References
Social Network
Keywords
- Fizzbuzz, Javascript, JS, ES6, Javascript Interview, Algorithm, Datastructure, Technical Interview, Interview Preparation, Coding, Coding Challenge, Programming Challenge