Skip to content
This repository was archived by the owner on Nov 7, 2020. It is now read-only.

Commit 53e1132

Browse files
author
ashley williams
committed
Merge pull request #29 from rmurphey/fizzbuzz
use modulo === 0 instead of implicit divisibility in fizzbuzz solution
2 parents c17b37e + 0fd040d commit 53e1132

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

app/flowControl.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ if (typeof define !== 'function') { var define = require('amdefine')(module); }
33
define(function() {
44
return {
55
fizzBuzz : function(num) {
6+
7+
// INSTRUCTIONS
68
// write a function that receives a number as its argument;
79
// if the number is divisible by 3, the function should return 'fizz';
810
// if the number is divisible by 5, the function should return 'buzz';
@@ -11,25 +13,35 @@ define(function() {
1113
//
1214
// otherwise the function should return the number, or false if no number
1315
// was provided or if the value provided was not a number
14-
if (typeof num !== 'number') { return false; }
1516

16-
// not divisible by 3 or 5
17-
if (num % 3 && num % 5) {
18-
return num;
17+
// make sure the value provided was a number, if not, return false
18+
if (typeof num !== 'number') {
19+
return false;
1920
}
2021

21-
// divisible by 3 but not 5
22-
if (num % 5) {
22+
// if the number is divisible by 3 AND 5, then when divided by both,
23+
// the remainder for each operation will be zero
24+
// return 'fizzbuzz'
25+
if (num % 3 === 0 && num % 5 === 0) {
26+
return 'fizzbuzz';
27+
}
28+
29+
// if the number is divisible by 3, when divided by 3, the remainder is zero
30+
// return 'fizz'
31+
if (num % 3 === 0) {
2332
return 'fizz';
2433
}
2534

26-
// divisible by 5 but not 3
27-
if (num % 3) {
35+
// if the number is divisible by 5, when divided by 5, the remainder is zero
36+
// return 'buzz'
37+
if (num % 5 === 0) {
2838
return 'buzz';
2939
}
3040

31-
// divisible by 5 and 3
32-
return 'fizzbuzz';
41+
// if the number is not divisble by 3 or 5, i.e. has skipped all previous
42+
// conditions, return the number
43+
return num;
44+
3345
}
3446
};
3547
});

0 commit comments

Comments
 (0)