-
Notifications
You must be signed in to change notification settings - Fork 0
/
caffeineScript.js
52 lines (43 loc) · 1.33 KB
/
caffeineScript.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
'use strict';
// Codewars - Andreas Sosilo
// 7 kyu - Caffeine Script
/*
Task:
-----
Complete the function caffeineBuzz, which takes a non-zero integer as its argument.
If the integer is divisible by 3, return the string "Java".
If the integer is divisible by 3 and divisible by 4, return the string "Coffee"
If one of the condition above is true and the integer is even, add "Script" to the end of the string.
If none of the condition is true, return the string "mocha_missing!"
caffeineBuzz(1) => "mocha_missing!"
caffeineBuzz(3) => "Java"
caffeineBuzz(6) => "JavaScript"
caffeineBuzz(12) => "CoffeeScript"
*/
// SOLUTION
const caffeineBuzz = (n) => {
let result = '';
let flag = false;
if (n % 3 === 0 && n % 4 === 0) {
result = 'Coffee';
flag = true;
} else if (n % 3 === 0) {
result = 'Java';
flag = true;
} else {
result = 'mocha_missing!';
}
return flag && n % 2 === 0 ? `${result}Script` : result;
};
// TEST CASES
console.log(caffeineBuzz(1)); // "mocha_missing!"
console.log(caffeineBuzz(3)); // "Java"
console.log(caffeineBuzz(6)); // "JavaScript"
console.log(caffeineBuzz(12)); // "CoffeeScript"
// BEST SOLUTION
// function caffeineBuzz(n){
// if (n % 12 === 0) return "CoffeeScript";
// if (n % 6 === 0) return "JavaScript";
// if (n % 3 === 0) return "Java";
// return "mocha_missing!";
// }