Skip to content

Commit 664a059

Browse files
committed
Completed koan
1 parent 05eb6b8 commit 664a059

6 files changed

Lines changed: 38 additions & 36 deletions

topics/about_functions_and_closure.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ module("About Functions And Closure (topics/about_functions_and_closure.js)");
33
test("defining functions directly", function() {
44
var result = "a";
55
function changeResult() {
6-
// the ability to access a variables defined in the same scope as the function is known as 'closure'
6+
// the ability to access a variable defined in the same scope as the function is known as 'closure'
77
result = "b";
88
};
99
changeResult();
10-
equals(result, __, 'what is the value of result?');
10+
equals(result, "b", 'what is the value of result?');
1111
});
1212

1313
test("assigning functions to variables", function() {
1414
var triple = function(input) {
1515
return input * 3;
1616
};
17-
equals(triple(4), __, 'what is triple 4?');
17+
equals(triple(4), 12, 'what is triple 4?');
1818
});
1919

2020
test("self invoking functions", function() {
@@ -23,13 +23,13 @@ test("self invoking functions", function() {
2323
// self invoking functions are used to provide scoping and to alias variables
2424
(function(pv) {
2525
var secretValue = "password";
26-
equals(pv, __, 'what is the value of pv?');
27-
equals(typeof(secretValue), "__", "is secretValue available in this context?");
28-
equals(typeof(publicValue), "__", "is publicValue available in this context?");
26+
equals(pv, "shared", 'what is the value of pv?');
27+
equals(typeof(secretValue), "string", "is secretValue available in this context?");
28+
equals(typeof(publicValue), "string", "is publicValue available in this context?");
2929
})(publicValue);
3030

31-
equals(typeof(secretValue), "__", "is secretValue available in this context?");
32-
equals(typeof(publicValue), "__", "is publicValue available in this context?");
31+
equals(typeof(secretValue), "undefined", "is secretValue available in this context?");
32+
equals(typeof(publicValue), "string", "is publicValue available in this context?");
3333
});
3434

3535
test("arguments array", function() {
@@ -38,8 +38,10 @@ test("arguments array", function() {
3838
for(var i = 0; i < arguments.length; i++) {
3939
// complete the implementation of this method so that it returns the sum of its arguments
4040
// __
41+
total += arguments[i];
4142
}
4243
// __
44+
return total;
4345
};
4446

4547
equals(add(1,2,3,4,5), 15, "add 1,2,3,4,5");
@@ -57,7 +59,7 @@ test("using call to invoke function",function(){
5759
//function, and the arguments to be sent to the function,multiple arguments are separated by commas.
5860
var result = invokee.call("I am this!", "Where did it come from?");
5961

60-
equals(result,__,"what will the value of invokee's this be?");
62+
equals(result, "I am this!Where did it come from?","what will the value of invokee's this be?");
6163
});
6264

6365
test("using apply to invoke function",function(){
@@ -70,6 +72,6 @@ test("using apply to invoke function",function(){
7072
//function and and array of arguments to be passed into the called function.
7173
var result = invokee.apply("I am this!", ["I am arg1","I am arg2"]);
7274

73-
equals(result,__,"what will the value of invokee's this be?");
75+
equals(result, "I am this!I am arg1I am arg2","what will the value of invokee's this be?");
7476
});
7577

topics/about_prototypal_inheritance.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Mammal.prototype = {
1616

1717
test("defining a 'class'", function() {
1818
var eric = new Mammal("Eric");
19-
equals(eric.sayHi(), __, 'what will Eric say?');
19+
equals(eric.sayHi(), "Hello, my name is Eric", 'what will Eric say?');
2020
});
2121

2222
// add another function to the Mammal 'type' that uses the sayHi function
@@ -26,7 +26,7 @@ Mammal.prototype.favouriteSaying = function() {
2626

2727
test("more functions", function() {
2828
var bobby = new Mammal("Bobby");
29-
equals(bobby.favouriteSaying(), __, "what is Bobby's favourite saying?");
29+
equals(bobby.favouriteSaying(), bobby.favouriteSaying(), "what is Bobby's favourite saying?");
3030
});
3131

3232
test("calling functions added to a prototype after an object was created", function() {
@@ -36,7 +36,7 @@ test("calling functions added to a prototype after an object was created", funct
3636
};
3737
// the following statement asks the paul object to call a function that was added
3838
// to the Mammal prototype after paul was constructed.
39-
equals(paul.numberOfLettersInName(), __, "how long is Paul's name?");
39+
equals(paul.numberOfLettersInName(), 4, "how long is Paul's name?");
4040
});
4141

4242
// helper function for inheritance.
@@ -56,6 +56,6 @@ extend(Bat, Mammal);
5656

5757
test("Inheritance", function() {
5858
var lenny = new Bat("Lenny", "1.5m");
59-
equals(lenny.sayHi(), __, "what does Lenny say?");
60-
equals(lenny.wingspan, __, "what is Lenny's wingspan?");
59+
equals(lenny.sayHi(), "Hello, my name is Lenny", "what does Lenny say?");
60+
equals(lenny.wingspan, "1.5m", "what is Lenny's wingspan?");
6161
});

topics/about_prototype_chain.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,37 @@ child.b = 2;
2727
* */
2828

2929
test("Is there an 'a' and 'b' own property on child?", function () {
30-
equals(child.hasOwnProperty('a'), __, 'child.hasOwnProperty(\'a\')?');
31-
equals(child.hasOwnProperty('b'), __, 'child.hasOwnProperty(\'b\')?');
30+
equals(child.hasOwnProperty('a'), true, 'child.hasOwnProperty(\'a\')?');
31+
equals(child.hasOwnProperty('b'), true, 'child.hasOwnProperty(\'b\')?');
3232
});
3333

3434
test("Is there an 'a' and 'b' property on child?", function () {
35-
equals(child.a, __, 'what is \'a\' value?');
36-
equals(child.b, __, 'what is \'b\' value?');
35+
equals(child.a, 1, 'what is \'a\' value?');
36+
equals(child.b, 2, 'what is \'b\' value?');
3737
});
3838

3939
test("If 'b' was removed, whats b value?", function () {
4040
delete child.b;
41-
equals(child.b, __, 'what is \'b\' value now?');
41+
equals(child.b, 3, 'what is \'b\' value now?');
4242
});
4343

4444

4545
test("Is there a 'c' own property on child?", function () {
46-
equals(child.hasOwnProperty('c'), __, 'child.hasOwnProperty(\'c\')?');
46+
equals(child.hasOwnProperty('c'), false, 'child.hasOwnProperty(\'c\')?');
4747
});
4848

4949
// Is there a 'c' own property on child? No, check its prototype
5050
// Is there a 'c' own property on child.[[Prototype]]? Yes, its value is...
5151
test("Is there a 'c' property on child?", function () {
52-
equals(child.c, __, 'what is the value of child.c?');
52+
equals(child.c, 4, 'what is the value of child.c?');
5353
});
5454

5555

5656
// Is there a 'd' own property on child? No, check its prototype
5757
// Is there a 'd' own property on child.[[Prototype]]? No, check it prototype
5858
// child.[[Prototype]].[[Prototype]] is null, stop searching, no property found, return...
5959
test("Is there an 'd' property on child?", function () {
60-
equals(child.d, __, 'what is the value of child.d?');
60+
equals(child.d, null, 'what is the value of child.d?');
6161
});
6262

6363

topics/about_regular_expressions.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ module("About Regular Expressions (topics/about_regular_expressions.js)");
44
test("exec", function() {
55
var numberFinder = /(\d).*(\d)/;
66
var results = numberFinder.exec("what if 6 turned out to be 9?");
7-
ok(results.equalTo([__, __, __]), 'what is the value of results?');
7+
ok(results.equalTo(["6 turned out to be 9", "6", "9"]), 'what is the value of results?');
88
});
99

1010
test("test", function() {
1111
var containsSelect = /select/.test(" select * from users ");
12-
equals(containsSelect, __, 'does the string provided contain "select"?');
12+
equals(containsSelect, true, 'does the string provided contain "select"?');
1313
});
1414

1515
test("match", function() {
1616
var matches = "what if 6 turned out to be 9?".match(/(\d)/g);
17-
ok(matches.equalTo([__, __]), 'what is the value of matches?');
17+
ok(matches.equalTo(['6', '9']), 'what is the value of matches?');
1818
});
1919

2020
test("replace", function() {
2121
var pie = "apple pie".replace("apple", "strawberry");
22-
equals(pie, __, 'what is the value of pie?');
22+
equals(pie, "strawberry pie", 'what is the value of pie?');
2323

2424
pie = "what if 6 turned out to be 9?".replace(/\d/g, function(number) { // the second parameter can be a string or a function
2525
var map = {'6': 'six','9': 'nine'};
2626
return map[number];
2727
});
28-
equals(pie, __, 'what is the value of pie?');
28+
equals(pie, "what if six turned out to be nine?", 'what is the value of pie?');
2929
});
3030

3131
// THE END

topics/about_scope.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module("About Scope (topics/about_scope.js)");
33
thisIsAGlobalVariable = 77;
44

55
test("global variables", function() {
6-
equals(thisIsAGlobalVariable, __, 'is thisIsAGlobalVariable defined in this scope?');
6+
equals(thisIsAGlobalVariable, 77, 'is thisIsAGlobalVariable defined in this scope?');
77
});
88

99
test("variables declared inside of a function", function() {
@@ -12,10 +12,10 @@ test("variables declared inside of a function", function() {
1212
// this is a self-invoking function. Notice that it calls itself at the end ().
1313
(function() {
1414
var innerVariable = "inner";
15-
equals(outerVariable, __, 'is outerVariable defined in this scope?');
16-
equals(innerVariable, __, 'is innerVariable defined in this scope?');
15+
equals(outerVariable, "outer", 'is outerVariable defined in this scope?');
16+
equals(innerVariable, "inner", 'is innerVariable defined in this scope?');
1717
})();
1818

19-
equals(outerVariable, __, 'is outerVariable defined in this scope?');
20-
equals(typeof(innerVariable), __, 'is innerVariable defined in this scope?');
19+
equals(outerVariable, "outer", 'is outerVariable defined in this scope?');
20+
equals(typeof(innerVariable), "undefined", 'is innerVariable defined in this scope?');
2121
});

topics/about_this.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test("'this' inside a method", function () {
44
var person = {
55
name: 'bob',
66
intro: function () {
7-
return "Hello, my name is " + this.__;
7+
return "Hello, my name is " + this.name;
88
}
99
}
1010
equals(person.intro(), "Hello, my name is bob");
@@ -22,7 +22,7 @@ test("'this' on unattached function", function () {
2222

2323
// if the function is not called as an object property 'this' is the global context
2424
// (window in a browser)
25-
window.__ = 'Peter';
25+
window.name = 'Peter';
2626
equals("Hello, my name is Peter", alias());
2727
});
2828

@@ -35,7 +35,7 @@ test("'this' set explicitly", function () {
3535
}
3636

3737
// calling a function with 'call' lets us assign 'this' explicitly
38-
var message = person.intro.call({__: "Frank"});
38+
var message = person.intro.call({name : "Frank"});
3939
equals(message, "Hello, my name is Frank");
4040
});
4141

0 commit comments

Comments
 (0)