-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_fizz_buzz.js
More file actions
71 lines (57 loc) · 1.65 KB
/
07_fizz_buzz.js
File metadata and controls
71 lines (57 loc) · 1.65 KB
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
66
67
68
69
70
71
/*
Write a function that takes an integer as input and returns a string.
If the integer is divisible by 3, return "fizz".
If the integer is divisible by 5, return "buzz".
If the integer is divisible by both 3 and 5, return "fizzbuzz".
Otherwise, return the integer as a string.
Examples:
fizzBuzz(3) => "fizz"
fizzBuzz(5) => "buzz"
fizzBuzz(15)=> "fizzbuzz"
fizzBuzz(7) => "7"
**There won't be any negative numbers**
It's not necessary to print the result on screen,
however to test your function you are free to print the result
*/
function isDivisible(dividend, divisor) {
return dividend % divisor === 0;
}
function fizzBuzz(number) {
const isdivisibleBy3 = isDivisible(number, 3);
const isdivisibleBy5 = isDivisible(number, 5);
if (isdivisibleBy3 && isdivisibleBy5) {
return "fizzbuzz";
}
if (isdivisibleBy3) {
return "fizz";
}
if (isdivisibleBy5) {
return "buzz";
}
return "" + number;
}
function getMark(isPassed) {
return isPassed ? '✅' : '❌';
}
function getMessage(number, actual, expected) {
let message = "Number :" + number + "\n Expexted : " + expected;
message += " Result => " + actual;
return message;
}
function testFizzBuzz(number, expected) {
const actual = fizzBuzz(number);
const isPassed = actual === expected;
console.log(getMark(isPassed), getMessage(number, actual, expected));
}
function tests() {
testFizzBuzz(3, "fizz");
testFizzBuzz(9, "fizz");
testFizzBuzz(5, "buzz");
testFizzBuzz(20, "buzz");
testFizzBuzz(15, "fizzbuzz");
testFizzBuzz(30, "fizzbuzz");
testFizzBuzz(0, "fizzbuzz");
testFizzBuzz(7, "7");
testFizzBuzz(1, "1");
}
tests();