Skip to content

Commit 37a796e

Browse files
committed
Complete AboutApplyingWhatWeHaveLearnt.js
1 parent 17bdd6f commit 37a796e

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

koans/AboutApplyingWhatWeHaveLearnt.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,18 @@ describe("About Applying What We Have Learnt", function() {
3232
}
3333
}
3434

35-
expect(productsICanEat.length).toBe(FILL_ME_IN);
35+
expect(productsICanEat.length).toBe(1);
3636
});
3737

3838
it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () {
3939

4040
var productsICanEat = [];
4141

4242
/* solve using filter() & all() / any() */
43+
productsICanEat = products.filter(function(x){ return x.containsNuts === false; })
44+
.filter(function(y){ return y.ingredients.indexOf('mushrooms') === -1 });
4345

44-
expect(productsICanEat.length).toBe(FILL_ME_IN);
46+
expect(productsICanEat.length).toBe(1);
4547
});
4648

4749
/*********************************************************************************/
@@ -55,14 +57,21 @@ describe("About Applying What We Have Learnt", function() {
5557
}
5658
}
5759

58-
expect(sum).toBe(FILL_ME_IN);
60+
expect(sum).toBe(233168);
5961
});
6062

6163
it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () {
6264

63-
var sum = FILL_ME_IN; /* try chaining range() and reduce() */
65+
var sum = _.range(1, 1000)
66+
.reduce(function(memo, elem){
67+
if (elem % 3 === 0 || elem % 5 === 0) {
68+
return memo + elem;
69+
} else {
70+
return memo;
71+
}
72+
}, 0); /* try chaining range() and reduce() */
6473

65-
expect(233168).toBe(FILL_ME_IN);
74+
expect(233168).toBe(sum);
6675
});
6776

6877
/*********************************************************************************/
@@ -75,15 +84,29 @@ describe("About Applying What We Have Learnt", function() {
7584
}
7685
}
7786

78-
expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN);
87+
expect(ingredientCount['mushrooms']).toBe(2);
7988
});
8089

8190
it("should count the ingredient occurrence (functional)", function () {
8291
var ingredientCount = { "{ingredient name}": 0 };
8392

8493
/* chain() together map(), flatten() and reduce() */
8594

86-
expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN);
95+
_.chain(products)
96+
.map(function(product){
97+
return product.ingredients;
98+
})
99+
.flatten()
100+
.reduce(function(memo, elem){
101+
if (elem === 'mushrooms') {
102+
ingredientCount['mushrooms'] = (ingredientCount['mushrooms'] || 0 ) + 1;
103+
} else {
104+
return;
105+
}
106+
})
107+
.value();
108+
109+
expect(ingredientCount['mushrooms']).toBe(2);
87110
});
88111

89112
/*********************************************************************************/

0 commit comments

Comments
 (0)