-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
halloween-trick-or-treat.js
36 lines (30 loc) · 1.18 KB
/
halloween-trick-or-treat.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
function trickOrTreat(children,candies){
const hasBomb = bag => bag.some(item => item === 'bomb');
if (children > candies.length || candies.some(hasBomb)) {
return 'Trick or Treat!';
}
const numCandies = bag => bag.reduce((total, thing) => thing === 'candy' ? total + 1 : total, 0);
const numCandiesInFirstBag = numCandies(candies[0]);
if (numCandiesInFirstBag >= 2) {
const allSameAmmountOfCandies = candies.every(bag => numCandies(bag) === numCandiesInFirstBag);
if (allSameAmmountOfCandies) {
return 'Thank you, strange uncle!';
}
}
return 'Trick or Treat!';
}
console.log(
trickOrTreat(3,[['candy','apple','candy'],['candy','candy'],['candy','candy']])
, 'Thank you, strange uncle!');
console.log(
trickOrTreat(3,[['candy','apple'],['apple','candy'],['candy','apple']])
, 'Trick or treat!');
console.log(
trickOrTreat(3,[['candy','apple','candy'],['candy','candy'],['candy','candy','candy']])
, 'Trick or treat!');
console.log(
trickOrTreat(3,[['candy','apple','candy'],['candy','candy']])
, 'Trick or treat!');
console.log(
trickOrTreat(3,[['candy','apple','candy'],['candy','candy'],['candy','bomb','candy']])
, 'Trick or treat!');