Skip to content

Commit 0c88abc

Browse files
committed
updated
1 parent 0e99868 commit 0c88abc

8 files changed

+143
-107
lines changed

koans/AboutApplyingWhatWeHaveLearnt.js

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,32 @@ 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

42+
productsICanEat = _.filter(products, function (x){
43+
_.every((x.ingredients === 'mushrooms') || (x.containsNuts === true));
44+
});
45+
4246
/* solve using filter() & all() / any() */
47+
/* var hasMushroomsOrNuts = function (x){
48+
return !(_x.ingredients === 'mushrooms' || x.ingredients === 'nuts'){
49+
console.log(x + 'has shrooms or nuts');
50+
*/
51+
var hasMushroomsOrNuts = function (x){
52+
for (var key in x)
53+
if (x[key].ingredients === 'mushrooms' || x[containsNuts]){
54+
console.log(x[key][ingredients] + ' has shrooms or nuts');
55+
} else {
56+
prodsIeat.push(x[key]);}
57+
};
58+
4359

44-
expect(productsICanEat.length).toBe(FILL_ME_IN);
60+
expect(productsICanEat.length).toBe(0);
4561
});
4662

4763
/*********************************************************************************/
@@ -55,14 +71,23 @@ describe("About Applying What We Have Learnt", function() {
5571
}
5672
}
5773

58-
expect(sum).toBe(FILL_ME_IN);
74+
expect(sum).toBe(233168);
5975
});
6076

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

63-
var sum = FILL_ME_IN; /* try chaining range() and reduce() */
79+
var sum = function(){
80+
var results = [];
81+
_.each(_.range(1000), function(num){
82+
if (num % 3 === 0 || num % 5 === 0){
83+
results.push(num);
84+
}
85+
});
86+
return _.reduce(results, function(x,y){return x + y;});
87+
};
88+
/* try chaining range() and reduce() */
6489

65-
expect(233168).toBe(FILL_ME_IN);
90+
expect(233168).toBe(233168);
6691
});
6792

6893
/*********************************************************************************/
@@ -75,15 +100,23 @@ describe("About Applying What We Have Learnt", function() {
75100
}
76101
}
77102

78-
expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN);
103+
expect(ingredientCount['mushrooms']).toBe(2);
79104
});
80105

81106
it("should count the ingredient occurrence (functional)", function () {
82107
var ingredientCount = { "{ingredient name}": 0 };
83108

84109
/* chain() together map(), flatten() and reduce() */
85-
86-
expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN);
110+
_.chain(products)
111+
.map(function(name){return name.ingredients();})
112+
.flatten()
113+
.reduce(function(counts, ingredient){
114+
counts[ingredient] = (counts[ingredient] || 0) + 1;
115+
return counts;
116+
}, {})
117+
.value();
118+
119+
expect(ingredientCount['mushrooms']).toBe(2);
87120
});
88121

89122
/*********************************************************************************/

koans/AboutArrays.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ describe("About Arrays", function() {
33
//We shall contemplate truth by testing reality, via spec expectations.
44
it("should create arrays", function() {
55
var emptyArray = [];
6-
expect(typeof(emptyArray)).toBe(FILL_ME_IN); //A mistake? - http:javascript.crockford.com/remedial.html
7-
expect(emptyArray.length).toBe(FILL_ME_IN);
6+
expect(typeof(emptyArray)).toBe('object'); //A mistake? - http:javascript.crockford.com/remedial.html
7+
expect(emptyArray.length).toBe(0);
88

99
var multiTypeArray = [0, 1, "two", function () { return 3; }, {value1: 4, value2: 5}, [6, 7]];
10-
expect(multiTypeArray[0]).toBe(FILL_ME_IN);
11-
expect(multiTypeArray[2]).toBe(FILL_ME_IN);
12-
expect(multiTypeArray[3]()).toBe(FILL_ME_IN);
13-
expect(multiTypeArray[4].value1).toBe(FILL_ME_IN);
14-
expect(multiTypeArray[4]["value2"]).toBe(FILL_ME_IN);
15-
expect(multiTypeArray[5][0]).toBe(FILL_ME_IN);
10+
expect(multiTypeArray[0]).toBe(0);
11+
expect(multiTypeArray[2]).toBe("two");
12+
expect(multiTypeArray[3]()).toBe(3);
13+
expect(multiTypeArray[4].value1).toBe(4);
14+
expect(multiTypeArray[4]["value2"]).toBe(5);
15+
expect(multiTypeArray[5][0]).toBe(6);
1616
});
1717

1818
it("should understand array literals", function () {
@@ -23,36 +23,36 @@ describe("About Arrays", function() {
2323
expect(array).toEqual([1]);
2424

2525
array[1] = 2;
26-
expect(array).toEqual([1, FILL_ME_IN]);
26+
expect(array).toEqual([1, 2]);
2727

2828
array.push(3);
29-
expect(array).toEqual(FILL_ME_IN);
29+
expect(array).toEqual([1, 2, 3]);
3030
});
3131

3232
it("should understand array length", function () {
3333
var fourNumberArray = [1, 2, 3, 4];
3434

35-
expect(fourNumberArray.length).toBe(FILL_ME_IN);
35+
expect(fourNumberArray.length).toBe(4);
3636
fourNumberArray.push(5, 6);
37-
expect(fourNumberArray.length).toBe(FILL_ME_IN);
37+
expect(fourNumberArray.length).toBe(6);
3838

3939
var tenEmptyElementArray = new Array(10);
40-
expect(tenEmptyElementArray.length).toBe(FILL_ME_IN);
40+
expect(tenEmptyElementArray.length).toBe(10);
4141

4242
tenEmptyElementArray.length = 5;
43-
expect(tenEmptyElementArray.length).toBe(FILL_ME_IN);
43+
expect(tenEmptyElementArray.length).toBe(5);
4444
});
4545

4646
it("should slice arrays", function () {
4747
var array = ["peanut", "butter", "and", "jelly"];
4848

49-
expect(array.slice(0, 1)).toEqual(FILL_ME_IN);
50-
expect(array.slice(0, 2)).toEqual(FILL_ME_IN);
51-
expect(array.slice(2, 2)).toEqual(FILL_ME_IN);
52-
expect(array.slice(2, 20)).toEqual(FILL_ME_IN);
53-
expect(array.slice(3, 0)).toEqual(FILL_ME_IN);
54-
expect(array.slice(3, 100)).toEqual(FILL_ME_IN);
55-
expect(array.slice(5, 1)).toEqual(FILL_ME_IN);
49+
expect(array.slice(0, 1)).toEqual(['peanut']);
50+
expect(array.slice(0, 2)).toEqual(['peanut','butter']);
51+
expect(array.slice(2, 2)).toEqual([]);
52+
expect(array.slice(2, 20)).toEqual(['and', 'jelly']);
53+
expect(array.slice(3, 0)).toEqual([]);
54+
expect(array.slice(3, 100)).toEqual(['jelly']);
55+
expect(array.slice(5, 1)).toEqual([]);
5656
});
5757

5858
it("should know array references", function () {
@@ -62,36 +62,36 @@ describe("About Arrays", function() {
6262
refArray[1] = "changed in function";
6363
}
6464
passedByReference(array);
65-
expect(array[1]).toBe(FILL_ME_IN);
65+
expect(array[1]).toBe('changed in function');
6666

6767
var assignedArray = array;
6868
assignedArray[5] = "changed in assignedArray";
69-
expect(array[5]).toBe(FILL_ME_IN);
69+
expect(array[5]).toBe("changed in assignedArray");
7070

7171
var copyOfArray = array.slice();
7272
copyOfArray[3] = "changed in copyOfArray";
73-
expect(array[3]).toBe(FILL_ME_IN);
73+
expect(array[3]).toBe("three");
7474
});
7575

7676
it("should push and pop", function () {
7777
var array = [1, 2];
7878
array.push(3);
7979

80-
expect(array).toEqual(FILL_ME_IN);
80+
expect(array).toEqual([1,2,3]);
8181

8282
var poppedValue = array.pop();
83-
expect(poppedValue).toBe(FILL_ME_IN);
84-
expect(array).toEqual(FILL_ME_IN);
83+
expect(poppedValue).toBe(3);
84+
expect(array).toEqual([1,2]);
8585
});
8686

8787
it("should know about shifting arrays", function () {
8888
var array = [1, 2];
8989

9090
array.unshift(3);
91-
expect(array).toEqual(FILL_ME_IN);
91+
expect(array).toEqual([3,1,2]);
9292

9393
var shiftedValue = array.shift();
94-
expect(shiftedValue).toEqual(FILL_ME_IN);
95-
expect(array).toEqual(FILL_ME_IN);
94+
expect(shiftedValue).toEqual(3);
95+
expect(array).toEqual([1,2]);
9696
});
9797
});

koans/AboutExpects.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ describe("About Expects", function() {
22

33
//We shall contemplate truth by testing reality, via spec expectations.
44
it("should expect true", function() {
5-
expect(false).toBeTruthy(); //This should be true
5+
expect(true).toBeTruthy(); //This should be true
66
});
77

88
//To understand reality, we must compare our expectations against reality.
99
it("should expect equality", function () {
10-
var expectedValue = FILL_ME_IN;
10+
var expectedValue = 2;
1111
var actualValue = 1 + 1;
1212

1313
expect(actualValue === expectedValue).toBeTruthy();
1414
});
1515

1616
//Some ways of asserting equality are better than others.
1717
it("should assert equality a better way", function () {
18-
var expectedValue = FILL_ME_IN;
18+
var expectedValue = 2;
1919
var actualValue = 1 + 1;
2020

2121
// toEqual() compares using common sense equality.
@@ -24,7 +24,7 @@ describe("About Expects", function() {
2424

2525
//Sometimes you need to be really exact about what you "type".
2626
it("should assert equality with ===", function () {
27-
var expectedValue = FILL_ME_IN;
27+
var expectedValue = '2';
2828
var actualValue = (1 + 1).toString();
2929

3030
// toBe() will always use === to compare.
@@ -33,6 +33,6 @@ describe("About Expects", function() {
3333

3434
//Sometimes we will ask you to fill in the values.
3535
it("should have filled in values", function () {
36-
expect(1 + 1).toEqual(FILL_ME_IN);
36+
expect(1 + 1).toEqual(2);
3737
});
3838
});

koans/AboutFunctions.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe("About Functions", function() {
66
return a + b;
77
}
88

9-
expect(add(1, 2)).toBe(FILL_ME_IN);
9+
expect(add(1, 2)).toBe(3);
1010
});
1111

1212
it("should know internal variables override outer variables", function () {
@@ -21,9 +21,9 @@ describe("About Functions", function() {
2121
return message;
2222
}
2323

24-
expect(getMessage()).toBe(FILL_ME_IN);
25-
expect(overrideMessage()).toBe(FILL_ME_IN);
26-
expect(message).toBe(FILL_ME_IN);
24+
expect(getMessage()).toBe("Outer");
25+
expect(overrideMessage()).toBe('Inner');
26+
expect(message).toBe('Outer');
2727
});
2828

2929
it("should have lexical scoping", function () {
@@ -35,7 +35,7 @@ describe("About Functions", function() {
3535
}
3636
return childfunction();
3737
}
38-
expect(parentfunction()).toBe(FILL_ME_IN);
38+
expect(parentfunction()).toBe('local');
3939
});
4040

4141
it("should use lexical scoping to synthesise functions", function () {
@@ -49,7 +49,7 @@ describe("About Functions", function() {
4949
var increaseBy3 = makeIncreaseByFunction(3);
5050
var increaseBy5 = makeIncreaseByFunction(5);
5151

52-
expect(increaseBy3(10) + increaseBy5(10)).toBe(FILL_ME_IN);
52+
expect(increaseBy3(10) + increaseBy5(10)).toBe(28);
5353
});
5454

5555
it("should allow extra function arguments", function () {
@@ -58,13 +58,13 @@ describe("About Functions", function() {
5858
return firstArg;
5959
}
6060

61-
expect(returnFirstArg("first", "second", "third")).toBe(FILL_ME_IN);
61+
expect(returnFirstArg("first", "second", "third")).toBe("first", "second", "third");
6262

6363
function returnSecondArg(firstArg, secondArg) {
6464
return secondArg;
6565
}
6666

67-
expect(returnSecondArg("only give first arg")).toBe(FILL_ME_IN);
67+
expect(returnSecondArg("only give first arg")).toBe(undefined);
6868

6969
function returnAllArgs() {
7070
var argsArray = [];
@@ -74,7 +74,7 @@ describe("About Functions", function() {
7474
return argsArray.join(",");
7575
}
7676

77-
expect(returnAllArgs("first", "second", "third")).toBe(FILL_ME_IN);
77+
expect(returnAllArgs("first", "second", "third")).toBe("first,second,third");
7878
});
7979

8080
it("should pass functions as values", function () {
@@ -88,21 +88,24 @@ describe("About Functions", function() {
8888
};
8989

9090
var praiseSinger = { givePraise: appendRules };
91-
expect(praiseSinger.givePraise("John")).toBe(FILL_ME_IN);
91+
expect(praiseSinger.givePraise("John")).toBe("John rules!");
9292

9393
praiseSinger.givePraise = appendDoubleRules;
94-
expect(praiseSinger.givePraise("Mary")).toBe(FILL_ME_IN);
94+
expect(praiseSinger.givePraise("Mary")).toBe("Mary totally rules!");
9595

9696
});
9797

9898
it("should use function body as a string", function () {
9999
var add = new Function("a", "b", "return a + b;");
100-
expect(add(1, 2)).toBe(FILL_ME_IN);
100+
expect(add(1, 2)).toBe(3);
101101

102102
var multiply = function (a, b) {
103103
//An internal comment
104104
return a * b;
105105
};
106-
expect(multiply.toString()).toBe(FILL_ME_IN);
106+
expect(multiply.toString()).toBe('function (a, b) {
107+
//An internal comment
108+
return a * b;
109+
}');
107110
});
108111
});

0 commit comments

Comments
 (0)