forked from Make-School-Labs/fizz-buzz-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzbuzz.js
66 lines (60 loc) · 1.42 KB
/
fizzbuzz.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const FIZZ = 'fizz'
const BUZZ = 'buzz'
const FIZZBUZZ = 'fizzbuzz'
/**
* Returns true when n is divisible by 3
* @param {Number} n
* @returns {Boolean} fizziness
* */
function isFizzy(n) {
return n % 3 === 0
}
/**
* Returns true when n is divisible by 5
* @param {Number} n
* @returns {Boolean} buzziness
*/
function isBuzzy(n) {
return n % 5 === 0
}
/**
* Returns FIZZ, BUZZ, or FIZZBUZZ when a number is divisible by 3, 5, or both
* @param {Number} n
* @returns {String} '', 'FIZZ', 'BUZZ', or 'FIZZBUZZ'
*/
function fizzyBuzzy(n) {
let result = ''
if (isFizzy(n)) { result += FIZZ }
if (isBuzzy(n)) { result += BUZZ }
return result
}
/**
* Generates a results object describing a sequence of fizz buzz for count.
* @param {Number} count
* @returns {Object} with properties count, fizz, buzz, and fizzbuzz
*/
function fizzBuzz(count) {
let result = { count, fizz: 0, buzz: 0, fizzBuzz: 0 }
for (let i = 1; i <= count; i += 1) {
const str = fizzyBuzzy(i)
switch(str) {
case FIZZ:
result.fizz += 1
break
case BUZZ:
result.buzz += 1
break
case FIZZBUZZ:
result.fizzBuzz += 1
break
}
}
return result
}
module.exports.isFizzy = isFizzy
module.exports.isBuzzy = isBuzzy
module.exports.fizzyBuzzy = fizzyBuzzy
module.exports.fizzBuzz = fizzBuzz
module.exports.FIZZ = FIZZ
module.exports.BUZZ = BUZZ
module.exports.FIZZBUZZ = FIZZBUZZ