-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzbuzz.ts
54 lines (49 loc) · 1.07 KB
/
fizzbuzz.ts
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
export const FIZZ = 'fizz'
export const BUZZ = 'buzz'
export const FIZZBUZZ = 'fizzbuzz'
interface FizziumBuzzord {
count: number,
fizz: number,
buzz: number,
fizzBuzz: number,
}
// returns true when n is divisible by 3.
export const isFizzy = (n:number):boolean => {
return n % 3 === 0
}
// returns true when n is divisible by 5.
export const isBuzzy = (n:number):boolean => {
return n % 5 === 0
}
// returns FIZZ, BUZZ, or FIZZBUZZ
// when a number is divisible by 3, 5, or both.
export const fizzyBuzzy = (n:number):string => {
let result = ''
if (isFizzy(n)) { result += FIZZ }
if (isBuzzy(n)) { result += BUZZ }
return result
}
// returns an object describing sequences of fizz buzz counts.
export const fizzBuzz = (count:number):FizziumBuzzord => {
let result = {
count: 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
}