-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy path5kyu_GuessTheGifts.js
41 lines (32 loc) · 2.22 KB
/
5kyu_GuessTheGifts.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
// 5kyu - Guess the Gifts!
// It's Christmas! You had to wait the whole year for this moment. You can already see all the presents under the Christmas tree. But you have to wait for the next morning in order to unwrap them. You really want to know, what's inside those boxes. But as a clever child, you can do your assumptions already.
// You know, you were a good child this year. So you may assume, that you'll only get things from your wishlist. You see those presents, you can lift them and you can shake them a bit. Now you can make you assumptions about what you'll get.
// Your Task
// You will be given a wishlist (array), containing all possible items. Each item is in the format: {name: "toy car", size: "medium", clatters: "a bit", weight: "medium"} (Ruby version has an analog hash structure, see example below)
// You also get a list of presents (array), you see under the christmas tree, which have the following format each: {size: "small", clatters: "no", weight: "light"}
// Your task is to return the names of all wishlisted presents that you might have gotten.
// Rules
// Possible values for size: "small", "medium", "large"
// Possible values for clatters: "no", "a bit", "yes"
// Possible values for weight: "light", "medium", "heavy"
// Don't add any item more than once to the result
// The order of names in the output doesn't matter
// It's possible, that multiple items from your wish list have the same attribute values. If they match the attributes of one of the presents, add all of them.
// Example
// var wishlist = [
// {name: "Mini Puzzle", size: "small", clatters: "yes", weight: "light"},
// {name: "Toy Car", size: "medium", clatters: "a bit", weight: "medium"},
// {name: "Card Game", size: "small", clatters: "no", weight: "light"}
// ];
// var presents = [
// {size: "medium", clatters: "a bit", weight: "medium"},
// {size: "small", clatters: "yes", weight: "light"}
// ];
// guessGifts(wishlist, presents); // must return ["Toy Car", "Mini Puzzle"]
function guessGifts(wishlist, presents) {
return wishlist.filter(function(x){
return presents.some(function(y){
return x.size == y.size && x.clatters == y.clatters && x.weight == y.weight;
});
}).map(function(x){ return x.name; });
}