-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponsibleDrinking.js
67 lines (56 loc) · 1.88 KB
/
responsibleDrinking.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
66
67
// https://www.codewars.com/kata/5aee86c5783bb432cd000018/train/javascript
// Codewars Bar recommends you drink 1 glass of water per standard drink so you're not hungover tomorrow morning.
// Your fellow coders have bought you several drinks tonight in the form of a string. Return a string suggesting how many glasses of water you should drink to not be hungover.
// Examples
// "1 beer" --> "1 glass of water"
// because you drank one standard drink
// "1 shot, 5 beers, 2 shots, 1 glass of wine, 1 beer" --> "10 glasses of water"
// because you drank ten standard drinks
function hydrate(s) {
let splitS = s.split(' ')
let totalDrinks = splitS.map(e => parseInt(e)).reduce((acc, curr) => !isNaN(curr) ? acc + curr : acc)
if(totalDrinks > 1){
return `${totalDrinks} glasses of water`
}else{
(totalDrinks === 1)
return `${totalDrinks} glass of water`
}
}
//alt
function hydrate(s) {
let answer = 0;
for (let i = 0; i < s.length; i++) {
if (parseInt(s[i]) > 0) {
answer += +s[i];
}
}
return answer > 1 ? `${answer} glasses of water`: '1 glass of water'
}
function hydrate(s) {
let answer = 0;
for (let i = 0; i < s.length; i++){
if(parseInt(s[i]) > 0) {
answer += +s[i];
}
}
return answer > 1 ? `${answer} glasses of water`: '1 glass of water'
}
//alt
function hydrate(s) {
let res = 0;
for (let i in s) {
if (s[i] > 0) {
res += +s[i];
}
}
return res > 1 ? res + ' glasses of water' : '1 glass of water';
}
//alt
function hydrate(s) {
let amount = s.split('').filter(e => Number(e)).reduce((a,b) => Number(a) + Number(b));
return `${amount} ${amount == 1 ? 'glass' : 'glasses'} of water`;
}
function hydrate(s){
let amount = s.split('').filter(e => Number(e)).reduce((a, b) => Number(a) + Number(b));
return `${amount}`
}