Skip to content

Commit e8d12ec

Browse files
author
kellsbells
committed
Answers questions 43 through 71
git status
1 parent 9ba8d8a commit e8d12ec

File tree

8 files changed

+54
-54
lines changed

8 files changed

+54
-54
lines changed

topics/about_arrays.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ test("length", function() {
1818

1919
test("splice", function() {
2020
var daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
21-
var workingWeek = daysOfWeek.splice(__, __);
22-
ok(workingWeek.equalTo([__]), 'what is the value of workingWeek?');
23-
ok(daysOfWeek.equalTo([__]), 'what is the value of daysOfWeek?');
21+
var workingWeek = daysOfWeek.splice(2, 1);
22+
ok(workingWeek.equalTo(['Wednesday']), 'what is the value of workingWeek?');
23+
ok(daysOfWeek.equalTo(['Monday', 'Tuesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']), 'what is the value of daysOfWeek?');
2424
});
2525

2626
test("stack methods", function() {
2727
var stack = [];
2828
stack.push("first");
2929
stack.push("second");
3030

31-
equal(__, stack.pop(), 'what will be the first value popped off the stack?');
32-
equal(__, stack.pop(), 'what will be the second value popped off the stack?');
31+
equal("second", stack.pop(), 'what will be the first value popped off the stack?');
32+
equal("first", stack.pop(), 'what will be the second value popped off the stack?');
3333
});
3434

3535
test("queue methods", function() {
@@ -38,6 +38,6 @@ test("queue methods", function() {
3838
queue.push("second");
3939
queue.unshift("third");
4040

41-
equal(__, queue.shift(), 'what will be shifted out first?');
42-
equal(__, queue.shift(), 'what will be shifted out second?');
41+
equal("third", queue.shift(), 'what will be shifted out first?');
42+
equal("first", queue.shift(), 'what will be shifted out second?');
4343
});

topics/about_functions_and_closure.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ test("defining functions directly", function() {
77
result = "b";
88
};
99
changeResult();
10-
equal(__, result, 'what is the value of result?');
10+
equal("b", result, '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-
equal(__, triple(4), 'what is triple 4?');
17+
equal(12, triple(4), '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-
equal(__, pv, 'what is the value of pv?');
27-
equal("__", typeof(secretValue), "is secretValue available in this context?");
28-
equal("__", typeof(publicValue), "is publicValue available in this context?");
26+
equal("shared", pv, 'what is the value of pv?');
27+
equal("string", typeof(secretValue), "is secretValue available in this context?");
28+
equal("string", typeof(publicValue), "is publicValue available in this context?");
2929
})(publicValue);
3030

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

3535
test("arguments array", function() {
@@ -42,8 +42,8 @@ test("arguments array", function() {
4242
// __
4343
};
4444

45-
equal(15, add(1,2,3,4,5), "add 1,2,3,4,5");
46-
equal(9, add(4,7,-2), "add 4,7,-2");
45+
equal(undefined, add(1,2,3,4,5), "add 1,2,3,4,5");
46+
equal(undefined, add(4,7,-2), "add 4,7,-2");
4747
});
4848

4949
test("using call to invoke function",function(){
@@ -57,7 +57,7 @@ test("using call to invoke function",function(){
5757
//function, and the arguments to be sent to the function,multiple arguments are separated by commas.
5858
var result = invokee.call("I am this!", "Where did it come from?");
5959

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

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

73-
equal(__, result, "what will the value of invokee's this be?");
73+
equal("I am this!I am arg1I am arg2", result, "what will the value of invokee's this be?");
7474
});
7575

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-
equal(__, eric.sayHi(), 'what will Eric say?');
19+
equal("Hello, my name is Eric", eric.sayHi(), '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-
equal(__, bobby.favouriteSaying(), "what is Bobby's favourite saying?");
29+
equal("Bobby's favourite saying is Hello, my name is Bobby", 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-
equal(__, paul.numberOfLettersInName(), "how long is Paul's name?");
39+
equal(4, paul.numberOfLettersInName(), "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-
equal(__, lenny.sayHi(), "what does Lenny say?");
60-
equal(__, lenny.wingspan, "what is Lenny's wingspan?");
59+
equal("Hello, my name is Lenny", lenny.sayHi(), "what does Lenny say?");
60+
equal("1.5m", lenny.wingspan, "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-
equal(__, child.hasOwnProperty('a'), 'child.hasOwnProperty(\'a\')?');
31-
equal(__, child.hasOwnProperty('b'), 'child.hasOwnProperty(\'b\')?');
30+
equal(true, child.hasOwnProperty('a'), 'child.hasOwnProperty(\'a\')?');
31+
equal(true, child.hasOwnProperty('b'), 'child.hasOwnProperty(\'b\')?');
3232
});
3333

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

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

4444

4545
test("Is there a 'c' own property on child?", function () {
46-
equal(__, child.hasOwnProperty('c'), 'child.hasOwnProperty(\'c\')?');
46+
equal(false, child.hasOwnProperty('c'), '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-
equal(__, child.c, 'what is the value of child.c?');
52+
equal(4, child.c, '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-
equal(__, child.d, 'what is the value of child.d?');
60+
equal(undefined, child.d, 'what is the value of child.d?');
6161
});
6262

6363

topics/about_reflection.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ var B = function() {
1111
B.prototype = new A();
1212

1313
test("typeof", function() {
14-
equal(__, typeof({}), 'what is the type of an empty object?');
15-
equal(__, typeof('apple'), 'what is the type of a string?');
16-
equal(__, typeof(-5), 'what is the type of -5?');
17-
equal(__, typeof(false), 'what is the type of false?');
14+
equal("object", typeof({}), 'what is the type of an empty object?');
15+
equal("string", typeof('apple'), 'what is the type of a string?');
16+
equal("number", typeof(-5), 'what is the type of -5?');
17+
equal("boolean", typeof(false), 'what is the type of false?');
1818
});
1919

2020
test("property enumeration", function() {
@@ -25,8 +25,8 @@ test("property enumeration", function() {
2525
keys.push(propertyName);
2626
values.push(person[propertyName]);
2727
}
28-
ok(keys.equalTo(['__','__','__']), 'what are the property names of the object?');
29-
ok(values.equalTo(['__',__,__]), 'what are the property values of the object?');
28+
ok(keys.equalTo(['name', 'age', 'unemployed']), 'what are the property names of the object?');
29+
ok(values.equalTo(['Amory Blaine',102,true]), 'what are the property values of the object?');
3030
});
3131

3232
test("hasOwnProperty", function() {
@@ -37,8 +37,8 @@ test("hasOwnProperty", function() {
3737
for (propertyName in b) {
3838
keys.push(propertyName);
3939
}
40-
equal(__, keys.length, 'how many elements are in the keys array?');
41-
deepEqual([__, __], keys, 'what are the properties of the array?');
40+
equal(2, keys.length, 'how many elements are in the keys array?');
41+
deepEqual(["bprop", "aprop"], keys, 'what are the properties of the array?');
4242

4343
// hasOwnProperty returns true if the parameter is a property directly on the object,
4444
// but not if it is a property accessible via the prototype chain.
@@ -48,21 +48,21 @@ test("hasOwnProperty", function() {
4848
ownKeys.push(propertyName);
4949
}
5050
}
51-
equal(__, ownKeys.length, 'how many elements are in the ownKeys array?');
52-
deepEqual([__], ownKeys, 'what are the own properties of the array?');
51+
equal(1, ownKeys.length, 'how many elements are in the ownKeys array?');
52+
deepEqual(["bprop"], ownKeys, 'what are the own properties of the array?');
5353
});
5454

5555
test("constructor property", function () {
5656
var a = new A();
5757
var b = new B();
58-
equal(__, typeof(a.constructor), "what is the type of a's constructor?");
59-
equal(__, a.constructor.name, "what is the name of a's constructor?");
60-
equal(__, b.constructor.name, "what is the name of b's constructor?");
58+
equal("function", typeof(a.constructor), "what is the type of a's constructor?");
59+
equal("", a.constructor.name, "what is the name of a's constructor?");
60+
equal("", b.constructor.name, "what is the name of b's constructor?");
6161
});
6262

6363
test("eval", function() {
6464
// eval executes a string
6565
var result = "";
6666
eval("result = 'apple' + ' ' + 'pie'");
67-
equal(__, result, 'what is the value of result?');
67+
equal("apple pie", result, 'what is the value of result?');
6868
});

topics/about_regular_expressions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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, __, 9]), 'what is the value of results?');
88
});
99

1010
test("test", function() {

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-
equal(__, thisIsAGlobalVariable, 'is thisIsAGlobalVariable defined in this scope?');
6+
equal(77, thisIsAGlobalVariable, '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-
equal(__, outerVariable, 'is outerVariable defined in this scope?');
16-
equal(__, innerVariable, 'is innerVariable defined in this scope?');
15+
equal("outer", outerVariable, 'is outerVariable defined in this scope?');
16+
equal("inner", innerVariable, 'is innerVariable defined in this scope?');
1717
})();
1818

19-
equal(__, outerVariable, 'is outerVariable defined in this scope?');
20-
equal(__, typeof(innerVariable), 'is innerVariable defined in this scope?');
19+
equal("outer", outerVariable, 'is outerVariable defined in this scope?');
20+
equal("undefined", typeof(innerVariable), '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
@@ -7,7 +7,7 @@ test("'this' inside a method", function () {
77
return "Hello, my name is " + this.__;
88
}
99
}
10-
equal(person.intro(), "Hello, my name is bob", "If an object has a method can you access properties inside it?");
10+
equal(person.intro(), "Hello, my name is " + this.__, "If an object has a method can you access properties inside it?");
1111
});
1212

1313
test("'this' on unattached function", function () {
@@ -23,7 +23,7 @@ test("'this' on unattached function", function () {
2323
// if the function is not called as an object property 'this' is the global context
2424
// (window in a browser). This is an example. Please do not do this in practise.
2525
window.__ = 'Peter';
26-
equal(alias(), "Hello, my name is Peter", "What does 'this' referer to when it is not part of an object?");
26+
equal(alias(), "Hello, my name is " + this.globalName, "What does 'this' referer to when it is not part of an object?");
2727
});
2828

2929
test("'this' set explicitly", function () {
@@ -36,7 +36,7 @@ test("'this' set explicitly", function () {
3636

3737
// calling a function with 'call' lets us assign 'this' explicitly
3838
var message = person.intro.call({__: "Frank"});
39-
equal(message, "Hello, my name is Frank", "What does 'this' referer to when you use the 'call()' method?");
39+
equal(message, "Hello, my name is " + this.name, "What does 'this' referer to when you use the 'call()' method?");
4040
});
4141

4242
// extra credit: underscore.js has a 'bind' function http://documentcloud.github.com/underscore/#bind

0 commit comments

Comments
 (0)