diff --git a/.gitignore b/.gitignore
index 986544fb..3333e7a4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
*.swp
-.idea
\ No newline at end of file
+.idea
+.vscode/
\ No newline at end of file
diff --git a/license.txt b/LICENSE
similarity index 100%
rename from license.txt
rename to LICENSE
diff --git a/README.md b/README.md
index 6a5de4ac..cb69fdb9 100644
--- a/README.md
+++ b/README.md
@@ -1,12 +1,25 @@
-Update
+Javascript Koans
======
+## Overview
+
JavaScript Koans is an interactive learning environment that uses failing tests to introduce students to aspects of JavaScript in a logical sequence.
-The inspiration for this project comes from the Edgecase Ruby Koans and the book 'Javascript: The Good Parts'.
+The inspiration for this project comes from the [Edgecase Ruby Koans](http://www.rubykoans.com/) and the book '[Javascript: The Good Parts'](https://www.oreilly.com/library/view/javascript-the-good/9780596517748/).
+
+## Getting Started
-Open the file jskoans.htm in your favourite browser and make the tests pass.
+Open the file [jskoans.htm](jskoans.htm) in your favourite browser and make the tests pass by editing the files located in the `topics` directory
+
+## Further Information
The koans use the [Qunit](http://qunitjs.com/) test syntax and test runner.
Get started with Ryan Anklam's [Learn JavaScript completely On the Cloud With the JavaScript Koans and Cloud9 IDE](http://blog.bittersweetryan.com/2011/08/learn-some-javascript-completely-on.html)
+
+## License
+
+This project is under the MIT License:
+
+* [MIT License](LICENSE)
+
diff --git a/jskoans.htm b/jskoans.htm
index f9427527..9b47877c 100644
--- a/jskoans.htm
+++ b/jskoans.htm
@@ -1,40 +1,44 @@
-
-
-
To begin, find the file 'topics/about_asserts.js', and complete the tests.
+
+
+
test markup, will be hidden
+
diff --git a/support/koans.js b/support/koans.js
index 4f11ea54..90cc825d 100644
--- a/support/koans.js
+++ b/support/koans.js
@@ -1,83 +1,77 @@
-
var __ = "incomplete";
-// ignore this. It simplifies determining array equality
-Array.prototype.equalTo = function(compareTo) {
- if (this.length !== compareTo.length) {
- return false;
- }
- for(var i = 0; i < compareTo.length; i++) {
- if (this[i] !== compareTo[i]) {
- return false;
- }
- }
- return true;
-};
-
-(function() {
-
- var lastAssertLogReason, ignoreFurtherFailures = false;
- var zenMessages = [
- "The path to enlightenment has many stones",
- "Do not stray from your path, for enlightenment comes with perseverance",
- "The only Zen you find on tops of mountains is the Zen you bring there",
- "Enlightenment occurs when someone becomes inspired by information and uses it to enhance their life",
- "Be master of mind rather than mastered by mind",
- "Zen is not some kind of excitement, but concentration on our usual everyday routine",
- "I think self-awareness is probably the most important thing towards being a champion",
- "The reward of all action is to be found in enlightenment",
- "lasting enlightenment can be achieved only through persistent exercise of real love",
- "The real meaning of enlightenment is to gaze with undimmed eyes on all darkness",
- "Do not think you will necessarily be aware of your own enlightenment",
- "Enlightenment must come little by little - otherwise it would overwhelm",
- "The greatest gift is to give people your enlightenment, to share it. It has to be the greatest",
- "In the beginner's mind there are many possibilities, but in the expert's mind there are few",
- "Only the hand that erases can write the true thing",
- "Enlightenment is ego's ultimate disappointment",
- "Man suffers only because he takes seriously what the gods made for fun",
- "It is easy to believe we are each waves and forget we are also the ocean",
- "Working out is my biggest hobby. It's my Zen hour. I just zone out",
- "A self-motivation is an enlightenment of mind, empowerment of heart and enrichment of soul to arise, awake and ascend to achieve the noble and coveted goal even if it entails walking on its enervating path all alone"
- ];
-
- QUnit.config.reorder = false;
+(function () {
+ var lastAssertLogReason,
+ ignoreFurtherFailures = false;
+ var zenMessages = [
+ "The path to enlightenment has many stones",
+ "Do not stray from your path, for enlightenment comes with perseverance",
+ "The only Zen you find on tops of mountains is the Zen you bring there",
+ "Enlightenment occurs when someone becomes inspired by information and uses it to enhance their life",
+ "Be master of mind rather than mastered by mind",
+ "Zen is not some kind of excitement, but concentration on our usual everyday routine",
+ "I think self-awareness is probably the most important thing towards being a champion",
+ "The reward of all action is to be found in enlightenment",
+ "lasting enlightenment can be achieved only through persistent exercise of real love",
+ "The real meaning of enlightenment is to gaze with undimmed eyes on all darkness",
+ "Do not think you will necessarily be aware of your own enlightenment",
+ "Enlightenment must come little by little - otherwise it would overwhelm",
+ "The greatest gift is to give people your enlightenment, to share it. It has to be the greatest",
+ "In the beginner's mind there are many possibilities, but in the expert's mind there are few",
+ "Only the hand that erases can write the true thing",
+ "Enlightenment is ego's ultimate disappointment",
+ "Man suffers only because he takes seriously what the gods made for fun",
+ "It is easy to believe we are each waves and forget we are also the ocean",
+ "Working out is my biggest hobby. It's my Zen hour. I just zone out",
+ "A self-motivation is an enlightenment of mind, empowerment of heart, and enrichment of soul to arise, awake, and ascend to achieve the noble and coveted goal even if it entails walking on its enervating path all alone",
+ ];
- QUnit.done(function(results) {
- var failures = results.failed;
- var total = results.total;
- if (failures > 0) {
- var failed = $('ol#qunit-tests > li.fail');
- failed.hide();
- $(failed[0]).show();
- }
- if (failures < total) {
- $('h3.welcome_message').hide();
- }
- if (failures > 0) {
- $("#zen-help").show();
- }
- $("body").scrollTop($(document).height());
- });
+ QUnit.config.reorder = false;
- QUnit.log(function(result) {
- lastAssertLogReason = result.message;
- });
+ QUnit.done(function (results) {
+ var failures = results.failed;
+ var total = results.total;
+ if (failures > 0) {
+ var failed = $("ol#qunit-tests > li.fail");
+ failed.hide();
+ $(failed[0]).show();
+ }
+ if (failures < total) {
+ $("h3.welcome_message").hide();
+ }
+ if (failures > 0) {
+ $("#zen-help").show();
+ }
+ $("body").scrollTop($(document).height());
+ });
- QUnit.testDone(function(result) {
- var message;
- if (!ignoreFurtherFailures && result.failed > 0) {
- ignoreFurtherFailures = true;
- message = "" + randomZenMessage() + "\nTry meditating on this: " + result.module + ": " + result.name + " (" + lastAssertLogReason + ")";
- $("#zen-help").html(message.replace(/\n/g, "
"));
+ console.log(message);
+ }
+ });
+ function randomZenMessage() {
+ var randomIndex = Math.floor(Math.random() * zenMessages.length);
+ var zenMessage = zenMessages[randomIndex];
+ zenMessage = zenMessage.charAt(0).toUpperCase() + zenMessage.substr(1);
+ return "" + zenMessage + ".";
+ }
})();
diff --git a/topics/about_arrays.js b/topics/about_arrays.js
index b2de2547..4914af28 100644
--- a/topics/about_arrays.js
+++ b/topics/about_arrays.js
@@ -1,45 +1,45 @@
module("About Arrays (topics/about_arrays.js)");
-test("array literal syntax and indexing", function() {
- var favouriteThings = ["cellar door", 42, true]; // note that array elements do not have to be of the same type
- equal(__, favouriteThings[0], 'what is in the first position of the array?');
- equal(__, favouriteThings[1], 'what is in the second position of the array?');
- equal(__, favouriteThings[2], 'what is in the third position of the array?');
+test("array literal syntax and indexing", function () {
+ var favouriteThings = ["cellar door", 42, true]; // Note that array elements do not have to be of the same type
+ equal(__, favouriteThings[0], "what is in the first position of the array?");
+ equal(__, favouriteThings[1], "what is in the second position of the array?");
+ equal(__, favouriteThings[2], "what is in the third position of the array?");
});
-test("array type", function() {
- equal(__, typeof([]), 'what is the type of an array?');
+test("array type", function () {
+ equal(__, typeof [], "what is the type of an array?");
});
-test("length", function() {
- var collection = ['a','b','c'];
- equal(__, collection.length, 'what is the length of the collection array?');
+test("length", function () {
+ var collection = ["a", "b", "c"];
+ equal(__, collection.length, "what is the length of the collection array?");
});
-test("splice", function() {
- var daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
+test("splice", function () {
+ var daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var workingWeek = daysOfWeek.splice(__, __);
var weekend = daysOfWeek;
- deepEqual(workingWeek, ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], 'what is the value of workingWeek?');
- deepEqual(weekend, ['Saturday', 'Sunday'], 'what is the value of weekend?');
+ deepEqual(workingWeek, ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"], "what is the value of workingWeek?");
+ deepEqual(weekend, ["Saturday", "Sunday"], "what is the value of weekend?");
});
-test("stack methods", function() {
+test("stack methods", function () {
var stack = [];
stack.push("first");
stack.push("second");
- equal(__, stack.pop(), 'what will be the first value popped off the stack?');
- equal(__, stack.pop(), 'what will be the second value popped off the stack?');
+ equal(__, stack.pop(), "what will be the first value popped off the stack?");
+ equal(__, stack.pop(), "what will be the second value popped off the stack?");
});
-test("queue methods", function() {
+test("queue methods", function () {
var queue = [];
queue.push("first");
queue.push("second");
queue.unshift("third");
- equal(__, queue.shift(), 'what will be shifted out first?');
- equal(__, queue.shift(), 'what will be shifted out second?');
+ equal(__, queue.shift(), "what will be shifted out first?");
+ equal(__, queue.shift(), "what will be shifted out second?");
});
diff --git a/topics/about_asserts.js b/topics/about_asserts.js
index baf7fc75..59db26d7 100644
--- a/topics/about_asserts.js
+++ b/topics/about_asserts.js
@@ -1,14 +1,13 @@
-
module("About Asserts (topics/about_asserts.js)");
-test("ok", function() {
- ok(__ === true, 'what will satisfy the ok assertion?');
+test("ok", function () {
+ ok(__ === true, "what will satisfy the `ok` assertion?");
});
-test("not ok", function() {
- ok(__ === false, 'what is a false value?');
+test("not ok", function () {
+ ok(__ === false, "what is a false value?");
});
-test("equal", function() {
- equal(__, 1 + 1, 'what will satisfy the equal assertion?');
+test("equal", function () {
+ equal(__, 1 + 1, "what will satisfy the `equal` assertion?");
});
diff --git a/topics/about_assignment.js b/topics/about_assignment.js
index 4532861e..6498919b 100644
--- a/topics/about_assignment.js
+++ b/topics/about_assignment.js
@@ -1,12 +1,11 @@
-
module("About Assignment (topics/about_assignment.js)");
-test("local variables", function() {
+test("local variables", function () {
var temp = __;
- equal(temp, 1, "Assign a value to the variable temp");
+ equal(temp, 1, "what value needs to be assigned to `temp`?");
});
-test("global variables", function() {
- temp = 1; // Not using var is an example. Always use var in practise.
- equal(window.__, temp, 'global variables are assigned to the window object');
+test("global variables", function () {
+ temp = 1; // Not using var is an example. Always use var in practice.
+ equal(window.__, temp, "what is added to the `window` object when you declare a global variable?");
});
diff --git a/topics/about_control_structures.js b/topics/about_control_structures.js
index aca1623b..bc6f9b0d 100644
--- a/topics/about_control_structures.js
+++ b/topics/about_control_structures.js
@@ -1,57 +1,58 @@
module("About Control Structures (topics/about_control_structures.js)");
-test("if", function() {
- var isPositive = false;
- if (2 > 0) {
- isPositive = true;
- }
- equal(__, isPositive, 'what is the value of isPositive?');
+test("if", function () {
+ var isPositive = false;
+ if (2 > 0) {
+ isPositive = true;
+ }
+ equal(__, isPositive, "what is the value of `isPositive`?");
});
-test("for", function() {
- var counter = 10;
- for (var i = 1; i <= 3; i++) {
- counter = counter + i;
- }
- equal(__, counter, 'what is the value of counter?');
+test("for", function () {
+ var counter = 10;
+ for (var i = 1; i <= 3; i++) {
+ counter = counter + i;
+ }
+ equal(__, counter, "what is the value of `counter`?");
});
-test("for in", function() {
- // this syntax will be explained in about objects
- var person = {
- name: "Amory Blaine",
- age: 102
- };
- var result = "";
- // for in enumerates the property names of an object
- for (var property_name in person) {
- result = result + property_name;
- }
- equal(__, result, 'what is the value of result?');
+test("for...in", function () {
+ // This syntax will be explained in "about objects"
+ var person = {
+ name: "Amory Blaine",
+ age: 102,
+ };
+
+ var result = "";
+ // `for...in` enumerates the property names of an object
+ for (var property_name in person) {
+ result = result + property_name;
+ }
+ equal(__, result, "what is the value of `result`?");
});
-test("ternary operator", function() {
- var fruit = true ? "apple" : "orange";
- equal(__, fruit, 'what is the value of fruit?');
+test("ternary operator", function () {
+ var fruit = true ? "apple" : "orange";
+ equal(__, fruit, "what is the value of `fruit`?");
- fruit = false ? "apple" : "orange";
- equal(__, fruit, 'now what is the value of fruit?');
+ fruit = false ? "apple" : "orange";
+ equal(__, fruit, "now what is the value of `fruit`?");
});
-test("switch", function() {
- var result = 0;
- switch (2) {
- case 1:
- result = 1;
- break;
- case 1+1:
- result = 2;
- break;
- }
- equal(__, result, 'what is the value of result?');
+test("switch", function () {
+ var result = 0;
+ switch (2) {
+ case 1:
+ result = 1;
+ break;
+ case 1 + 1:
+ result = 2;
+ break;
+ }
+ equal(__, result, "what is the value of `result`?");
});
-test("switch default case", function() {
+test("switch default case", function () {
var result = "Pippin";
switch ("m") {
case "f":
@@ -59,15 +60,15 @@ test("switch default case", function() {
break;
case "s":
result = "Samwise";
- break;
+ break;
default:
result = "Merry";
break;
}
- equal(__, result, 'what is the value of result?');
+ equal(__, result, "what is the value of `result`?");
});
-test("null coalescing", function() {
+test("null coalescing", function () {
var result = null || "a value";
- equal(__, result, 'what is the value of result?');
+ equal(__, result, "what is the value of `result`?");
});
diff --git a/topics/about_equality.js b/topics/about_equality.js
index fe3e3d21..50b1d70d 100644
--- a/topics/about_equality.js
+++ b/topics/about_equality.js
@@ -1,23 +1,23 @@
-
module("About Equality (topics/about_equality.js)");
-test("numeric equality", function() {
- equal(3 + __, 7, "");
+test("numeric equality", function () {
+ equal(3 + __, 7, "what happens when you add two numbers?");
});
-test("string equality", function() {
- equal("3" + __, "37", "concatenate the strings");
+test("string equality", function () {
+ equal("3" + __, "37", "what happens when you add two strings?");
});
-test("equality without type coercion", function() {
- ok(3 === __, 'what is exactly equal to 3?');
+test("equality without type coercion", function () {
+ ok(3 === __, "what is exactly equal to 3?");
});
-test("equality with type coercion", function() {
- ok(3 == "__", 'what string is equal to 3, with type coercion?');
+test("equality with type coercion", function () {
+ // You answer must be a string to be zen
+ ok(3 == "__", "what string is equal to 3, with type coercion?");
});
-test("string literals", function() {
- equal(__, "frankenstein", "quote types are interchangable, but must match.");
+test("string literals", function () {
+ equal(__, "frankenstein", "quote types are interchangeable, but must match.");
equal(__, 'frankenstein', "quote types can use both single and double quotes.");
});
diff --git a/topics/about_functions_and_closure.js b/topics/about_functions_and_closure.js
index d435c232..93c126cc 100644
--- a/topics/about_functions_and_closure.js
+++ b/topics/about_functions_and_closure.js
@@ -1,75 +1,76 @@
module("About Functions And Closure (topics/about_functions_and_closure.js)");
-test("defining functions directly", function() {
+test("defining functions directly", function () {
var result = "a";
function changeResult() {
- // the ability to access a variables defined in the same scope as the function is known as 'closure'
+ // The ability to access variables defined in the same scope as the function is known as "closure"
result = "b";
- };
+ }
changeResult();
- equal(__, result, 'what is the value of result?');
+ equal(__, result, "what is the value of `result`?");
});
-test("assigning functions to variables", function() {
- var triple = function(input) {
+test("assigning functions to variables", function () {
+ var triple = function (input) {
return input * 3;
};
- equal(__, triple(4), 'what is triple 4?');
+ equal(__, triple(4), "what is triple 4?");
});
-test("self invoking functions", function() {
+test("self invoking functions", function () {
var publicValue = "shared";
- // self invoking functions are used to provide scoping and to alias variables
- (function(pv) {
+ // Self invoking functions are used to provide scoping and to alias variables
+ (function (pv) {
var secretValue = "password";
- equal(__, pv, 'what is the value of pv?');
- equal("__", typeof(secretValue), "is secretValue available in this context?");
- equal("__", typeof(publicValue), "is publicValue available in this context?");
+ equal(__, pv, "what is the value of `pv`?");
+ equal(__, typeof secretValue, "is `secretValue` available in this context?");
+ equal(__, typeof publicValue, "is `publicValue` available in this context?");
})(publicValue);
- equal("__", typeof(secretValue), "is secretValue available in this context?");
- equal("__", typeof(publicValue), "is publicValue available in this context?");
+ equal(__, typeof secretValue, "is `secretValue` available in this context?");
+ equal(__, typeof publicValue, "is `publicValue` available in this context?");
});
-test("arguments array", function() {
- var add = function() {
+test("arguments array", function () {
+ var add = function () {
var total = 0;
- for(var i = 0; i < arguments.length; i++) {
- // complete the implementation of this method so that it returns the sum of its arguments
+ for (var i = 0; i < arguments.length; i++) {
+ // Complete the implementation of this method so that it returns the sum of its arguments
// __
}
// __
};
- equal(15, add(1,2,3,4,5), "add 1,2,3,4,5");
- equal(9, add(4,7,-2), "add 4,7,-2");
+ equal(15, add(1, 2, 3, 4, 5), "add 1, 2, 3, 4, 5");
+ equal(9, add(4, 7, -2), "add 4, 7, -2");
});
-test("using call to invoke function",function(){
- var invokee = function( message ){
- return this + message;
+test("using call to invoke function", function () {
+ var invokee = function (message) {
+ return this + message;
};
-
- //another way to invoke a function is to use the call function which allows
- //you to set the callers "this" context. Call can take any number of arguments:
- //the first one is always the context that this should be set to in the called
- //function, and the arguments to be sent to the function,multiple arguments are separated by commas.
- var result = invokee.call("I am this!", "Where did it come from?");
-
- equal(__, result, "what will the value of invokee's this be?");
+
+ /* Another way to invoke a function is to use the `call` function, which allows
+ you to set the caller's `this` context. `call` can take any number of arguments:
+ the first one is always the context that `this` should be set to in the called
+ function while the rest are the arguments to be sent to the function (multiple
+ arguments are comma separated). */
+ var result = invokee.call("I am this!", "where did it come from?");
+
+ equal(__, result, "what will the value of `invokee`'s `this` be?");
});
-test("using apply to invoke function",function(){
- var invokee = function( message1, message2 ){
- return this + message1 + message2;
+test("using apply to invoke function", function () {
+ var invokee = function (message1, message2) {
+ return this + message1 + message2;
};
-
- //similar to the call function is the apply function. Apply only has two
- //arguments: the first is the context that this should be set to in the called
- //function and the second is the array of arguments to be passed into the called function.
- var result = invokee.apply("I am this!", ["I am arg1","I am arg2"]);
-
- equal(__, result, "what will the value of invokee's this be?");
-});
+ /* Similar to the `call` function is the `apply` function. `apply` only has two
+ arguments: the first is the context that `this` should be set to in the called
+ function and the second is the array of arguments to be passed into the called
+ function. */
+ var result = invokee.apply("I am this!", ["I am arg1", "I am arg2"]);
+
+ equal(__, result, "what will the value of `invokee`'s `this` be?");
+});
diff --git a/topics/about_numbers.js b/topics/about_numbers.js
index 1319acd8..9dfcc4c2 100644
--- a/topics/about_numbers.js
+++ b/topics/about_numbers.js
@@ -1,16 +1,15 @@
-
module("About Numbers (topics/about_numbers.js)");
-test("types", function() {
- var typeOfIntegers = typeof(6);
- var typeOfFloats = typeof(3.14159);
- equal(__, typeOfIntegers === typeOfFloats, 'are ints and floats the same type?');
- equal(__, typeOfIntegers, 'what is the javascript numeric type?');
- equal(__, 1.0, 'what is a integer number equivalent to 1.0?');
+test("types", function () {
+ var typeOfIntegers = typeof 6;
+ var typeOfFloats = typeof 3.14159;
+ equal(__, typeOfIntegers === typeOfFloats, "are integers and floats the same type?");
+ equal(__, typeOfIntegers, "what is the javascript numeric type?");
+ equal(__, 1.0, "what integer number is equivalent to 1.0?");
});
-test("NaN", function() {
- var resultOfFailedOperations = 7/'apple';
- equal(__, isNaN(resultOfFailedOperations), 'what will satisfy the equals assertion?');
- equal(__, resultOfFailedOperations == NaN, 'is NaN == NaN?');
+test("NaN", function () {
+ var resultOfFailedOperations = 7 / "apple";
+ equal(__, isNaN(resultOfFailedOperations), "what will satisfy the equals assertion?");
+ equal(__, resultOfFailedOperations == NaN, "is NaN == NaN?");
});
diff --git a/topics/about_objects.js b/topics/about_objects.js
index 24c03533..a320fbf5 100644
--- a/topics/about_objects.js
+++ b/topics/about_objects.js
@@ -1,43 +1,42 @@
-
module("About Objects (topics/about_objects.js)");
-test("object type", function() {
+test("object type", function () {
var empty_object = {};
- equal(__, typeof(empty_object), 'what is the type of an object?');
+ equal(__, typeof empty_object, "what is the type of an object?");
});
-test("object literal notation", function() {
+test("object literal notation", function () {
var person = {
- __:__,
- __:__
+ __: __,
+ __: __, // Trailing commas https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas
};
equal("Amory Blaine", person.name, "what is the person's name?");
equal(102, person.age, "what is the person's age?");
});
-test("dynamically adding properties", function() {
+test("dynamically adding properties", function () {
var person = {};
person.__ = "Amory Blaine";
person.__ = 102;
equal("Amory Blaine", person.name, "what is the person's name?");
equal(102, person.age, "what is the person's age?");
-});
+});
-test("adding properties from strings", function() {
+test("adding properties from strings", function () {
var person = {};
- person["__"] = "Amory Blaine";
- person["__"] = 102;
+ person[__] = "Amory Blaine";
+ person[__] = 102;
equal("Amory Blaine", person.name, "what is the person's name?");
equal(102, person.age, "what is the person's age?");
});
-test("adding functions", function() {
+test("adding functions", function () {
var person = {
name: "Amory Blaine",
age: 102,
- toString: function() {
- return __; // HINT: use the 'this' keyword to refer to the person object.
- }
+ toString: function () {
+ return __; // HINT: use the `this` keyword to refer to the person object.
+ },
};
- equal("I Amory Blaine am 102 years old.", person.toString(), "what should the toString function be?");
+ equal("I Amory Blaine am 102 years old.", person.toString(), "what should the `toString` function be?");
});
diff --git a/topics/about_operators.js b/topics/about_operators.js
index 9859900b..4aead4e2 100644
--- a/topics/about_operators.js
+++ b/topics/about_operators.js
@@ -1,47 +1,46 @@
-
module("About Operators (topics/about_operators.js)");
-test("addition", function() {
- var result = 0;
- //starting i at 0, add i to result and increment i by 1 until i is equal to 5
- for (var i = 0; i <= 5; i++) {
- result = result + i;
- }
- equal(__, result, "What is the value of result?");
+test("addition", function () {
+ var result = 0;
+ // Starting i at 0, add i to result and increment i by 1 until i is equal to 5
+ for (var i = 0; i <= 5; i++) {
+ result = result + i;
+ }
+ equal(__, result, "what is the value of `result`?");
});
-test("assignment addition", function() {
- var result = 0;
- for (var i = 0; i <=5; i++) {
- //the code below is just like saying result = result + i; but is more concise
- result += i;
- }
- equal(__, result, "What is the value of result?");
+test("assignment addition", function () {
+ var result = 0;
+ for (var i = 0; i <= 5; i++) {
+ // The code below is just like saying result = result + i; but is more concise
+ result += i;
+ }
+ equal(__, result, "what is the value of `result`?");
});
-test("subtraction", function() {
- var result = 5;
- for (var i = 0; i <= 2; i++) {
- result = result - i;
- }
- equal(__, result, "What is the value of result?");
+test("subtraction", function () {
+ var result = 5;
+ for (var i = 0; i <= 2; i++) {
+ result = result - i;
+ }
+ equal(__, result, "what is the value of `result`?");
});
-test("assignment subtraction", function() {
- var result = 5;
- for (var i = 0; i <= 2; i++) {
- result -= i;
- }
- equal(__, result, "What is the value of result?");
+test("assignment subtraction", function () {
+ var result = 5;
+ for (var i = 0; i <= 2; i++) {
+ result -= i;
+ }
+ equal(__, result, "what is the value of `result`?");
});
-//Assignment operators are available for multiplication and division as well
-//let's do one more, the modulo operator, used for showing division remainder
+// Assignment operators are available for multiplication and division as well
+// let's do one more, the modulo operator, used for showing division remainder
-test("modulus", function() {
- var result = 10;
- var x = 5;
- //again this is exactly the same as result = result % x
- result %= x;
- equal(__, result, "What is the value of result?");
+test("modulus", function () {
+ var result = 10;
+ var x = 5;
+ // Again this is exactly the same as result = result % x
+ result %= x;
+ equal(__, result, "what is the value of `result`?");
});
diff --git a/topics/about_prototypal_inheritance.js b/topics/about_prototypal_inheritance.js
index 811c040e..b8b76361 100644
--- a/topics/about_prototypal_inheritance.js
+++ b/topics/about_prototypal_inheritance.js
@@ -1,60 +1,59 @@
-
-// demonstrate the effect of modifying an objects prototype before and after the object is constructed
+// Demonstrate the effect of modifying an objects prototype before and after the object is constructed
module("About Prototypal Inheritance (topics/about_prototypal_inheritance.js)");
-// this 'class' pattern defines a class by its constructor
-var Mammal = function(name) {
+// This 'class' pattern defines a class by its constructor
+var Mammal = function (name) {
this.name = name;
-}
-// things that don't need to be set in the constructor should be added to the constructor's prototype property.
+};
+// Things that don't need to be set in the constructor should be added to the constructor's prototype property.
Mammal.prototype = {
- sayHi: function() {
+ sayHi: function () {
return "Hello, my name is " + this.name;
- }
-}
+ },
+};
-test("defining a 'class'", function() {
- var eric = new Mammal("Eric");
- equal(__, eric.sayHi(), 'what will Eric say?');
+test("defining a 'class'", function () {
+ var eric = new Mammal("Eric");
+ equal(__, eric.sayHi(), "what will Eric say?");
});
-// add another function to the Mammal 'type' that uses the sayHi function
-Mammal.prototype.favouriteSaying = function() {
- return this.name + "'s favourite saying is " + this.sayHi();
-}
+// Add another function to the `Mammal` class. Make use of the existing `sayHi` function
+Mammal.prototype.favouriteSaying = function () {
+ return this.name + "'s favourite saying is " + this.sayHi();
+};
-test("more functions", function() {
+test("more functions", function () {
var bobby = new Mammal("Bobby");
- equal(__, bobby.favouriteSaying(), "what is Bobby's favourite saying?");
+ equal(__, bobby.favouriteSaying(), "what is Bobby's favourite saying?");
});
-test("calling functions added to a prototype after an object was created", function() {
+test("calling functions added to a prototype after an object was created", function () {
var paul = new Mammal("Paul");
- Mammal.prototype.numberOfLettersInName = function() {
+ Mammal.prototype.numberOfLettersInName = function () {
return this.name.length;
};
- // the following statement asks the paul object to call a function that was added
- // to the Mammal prototype after paul was constructed.
+ // The following statement asks the `paul` object to call a function that was added
+ // to the `Mammal` prototype after `paul` was constructed.
equal(__, paul.numberOfLettersInName(), "how long is Paul's name?");
});
-// helper function for inheritance.
-// From https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_Revisited
-function extend(child, supertype){
- child.prototype = supertype.prototype;
-}
+// Helper function for inheritance.
+// from https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_Revisited
+function extend(child, supertype) {
+ child.prototype = supertype.prototype;
+}
// "Subclass" Mammal
function Bat(name, wingspan) {
Mammal.call(this, name);
this.wingspan = wingspan;
-}
+}
-// configure inheritance
+// Configure inheritance
extend(Bat, Mammal);
-test("Inheritance", function() {
+test("inheritance", function () {
var lenny = new Bat("Lenny", "1.5m");
equal(__, lenny.sayHi(), "what does Lenny say?");
equal(__, lenny.wingspan, "what is Lenny's wingspan?");
diff --git a/topics/about_prototype_chain.js b/topics/about_prototype_chain.js
index 46e3b4df..10b65acf 100644
--- a/topics/about_prototype_chain.js
+++ b/topics/about_prototype_chain.js
@@ -1,11 +1,11 @@
-// demonstrate objects prototype chain
+// Demonstrate objects prototype chain
// https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_and_the_prototype_chain
module("About Prototype Chain (topics/about_prototype_chain.js)");
var father = {
- b: 3,
- c: 4
+ b: 3,
+ c: 4,
};
var child = Object.create(father);
@@ -26,38 +26,34 @@ child.b = 2;
* ---------------------- ---- ---- ----
* */
-test("Is there an 'a' and 'b' own property on child?", function () {
- equal(__, child.hasOwnProperty('a'), 'child.hasOwnProperty(\'a\')?');
- equal(__, child.hasOwnProperty('b'), 'child.hasOwnProperty(\'b\')?');
+test("is there an `a` and `b` own property on `child`?", function () {
+ equal(__, child.hasOwnProperty("a"), "what is the value of `child.hasOwnProperty('a')`?");
+ equal(__, child.hasOwnProperty("b"), "what is the value of `child.hasOwnProperty('b')`?");
});
-test("Is there an 'a' and 'b' property on child?", function () {
- equal(__, child.a, 'what is \'a\' value?');
- equal(__, child.b, 'what is \'b\' value?');
+test("is there an `a` and `b` property on `child`?", function () {
+ equal(__, child.a, "what is the value of `a`?");
+ equal(__, child.b, "what is the value of `b`?");
});
-test("If 'b' was removed, whats b value?", function () {
- delete child.b;
- equal(__, child.b, 'what is \'b\' value now?');
+test("if `b` was removed, what is the value of `b`?", function () {
+ delete child.b;
+ equal(__, child.b, "what is the value of `b` now?");
});
-
-test("Is there a 'c' own property on child?", function () {
- equal(__, child.hasOwnProperty('c'), 'child.hasOwnProperty(\'c\')?');
+test("is there a `c` own property on `child`?", function () {
+ equal(__, child.hasOwnProperty("c"), "what is the value of `child.hasOwnProperty('c')`?");
});
-// Is there a 'c' own property on child? No, check its prototype
-// Is there a 'c' own property on child.[[Prototype]]? Yes, its value is...
-test("Is there a 'c' property on child?", function () {
- equal(__, child.c, 'what is the value of child.c?');
+// Is there a `c` own property on `child`? No, check its prototype
+// Is there a `c` own property on child.[[Prototype]]? Yes, its value is...
+test("is there a `c` property on `child`?", function () {
+ equal(__, child.c, "what is the value of `child.c`?");
});
-
-// Is there a 'd' own property on child? No, check its prototype
-// Is there a 'd' own property on child.[[Prototype]]? No, check it prototype
+// Is there a `d` own property on `child`? No, check its prototype
+// Is there a `d` own property on child.[[Prototype]]? No, check it prototype
// child.[[Prototype]].[[Prototype]] is null, stop searching, no property found, return...
-test("Is there an 'd' property on child?", function () {
- equal(__, child.d, 'what is the value of child.d?');
+test("is there an `d` property on `child`?", function () {
+ equal(__, child.d, "what is the value of `child.d`?");
});
-
-
diff --git a/topics/about_reflection.js b/topics/about_reflection.js
index 63868648..e2c198f6 100644
--- a/topics/about_reflection.js
+++ b/topics/about_reflection.js
@@ -2,34 +2,34 @@ module("About Reflection (topics/about_reflection.js)");
function A() {
this.aprop = "A";
-};
+}
function B() {
this.bprop = "B";
-};
+}
B.prototype = new A();
-test("typeof", function() {
- equal(__, typeof({}), 'what is the type of an empty object?');
- equal(__, typeof('apple'), 'what is the type of a string?');
- equal(__, typeof(-5), 'what is the type of -5?');
- equal(__, typeof(false), 'what is the type of false?');
+test("typeof", function () {
+ equal(__, typeof {}, "what is the type of an empty object?");
+ equal(__, typeof "apple", "what is the type of a string?");
+ equal(__, typeof -5, "what is the type of -5?");
+ equal(__, typeof false, "what is the type of false?");
});
-test("property enumeration", function() {
+test("property enumeration", function () {
var keys = [];
var values = [];
- var person = {name: 'Amory Blaine', age: 102, unemployed: true};
- for(var propertyName in person) {
+ var person = { name: "Amory Blaine", age: 102, unemployed: true };
+ for (var propertyName in person) {
keys.push(propertyName);
values.push(person[propertyName]);
}
- ok(keys.equalTo(['__','__','__']), 'what are the property names of the object?');
- ok(values.equalTo(['__',__,__]), 'what are the property values of the object?');
+ deepEqual(keys, [__, __, __], "what are the property names of the object?");
+ deepEqual(values, [__, __, __], "what are the property values of the object?");
});
-test("hasOwnProperty", function() {
+test("hasOwnProperty", function () {
var b = new B();
var propertyName;
@@ -37,32 +37,32 @@ test("hasOwnProperty", function() {
for (propertyName in b) {
keys.push(propertyName);
}
- equal(__, keys.length, 'how many elements are in the keys array?');
- deepEqual([__, __], keys, 'what are the properties of the array?');
+ equal(__, keys.length, "how many elements are in the keys array?");
+ deepEqual([__, __], keys, "what are the properties of the array?");
- // hasOwnProperty returns true if the parameter is a property directly on the object,
+ // `hasOwnProperty` returns `true` if the parameter is a property directly on the object,
// but not if it is a property accessible via the prototype chain.
var ownKeys = [];
- for(propertyName in b) {
+ for (propertyName in b) {
if (b.hasOwnProperty(propertyName)) {
ownKeys.push(propertyName);
}
}
- equal(__, ownKeys.length, 'how many elements are in the ownKeys array?');
- deepEqual([__], ownKeys, 'what are the own properties of the array?');
+ equal(__, ownKeys.length, "how many elements are in the `ownKeys` array?");
+ deepEqual([__], ownKeys, "what are the own properties of the array?");
});
test("constructor property", function () {
var a = new A();
var b = new B();
- equal(__, typeof(a.constructor), "what is the type of a's constructor?");
- equal(__, a.constructor.name, "what is the name of a's constructor?");
- equal(__, b.constructor.name, "what is the name of b's constructor?");
+ equal(__, typeof a.constructor, "what is the type of `a`'s constructor?");
+ equal(__, a.constructor.name, "what is the name of `a`'s constructor?");
+ equal(__, b.constructor.name, "what is the name of `b`'s constructor?");
});
-test("eval", function() {
- // eval executes a string
+test("eval", function () {
+ // `eval` executes a string
var result = "";
eval("result = 'apple' + ' ' + 'pie'");
- equal(__, result, 'what is the value of result?');
+ equal(__, result, "what is the value of `result`?");
});
diff --git a/topics/about_regular_expressions.js b/topics/about_regular_expressions.js
index b49bc723..378f3f45 100644
--- a/topics/about_regular_expressions.js
+++ b/topics/about_regular_expressions.js
@@ -1,31 +1,31 @@
-
module("About Regular Expressions (topics/about_regular_expressions.js)");
-test("exec", function() {
+test("exec", function () {
var numberFinder = /(\d).*(\d)/;
var results = numberFinder.exec("what if 6 turned out to be 9?");
- ok(results.equalTo([__, __, __]), 'what is the value of results?');
+ deepEqual(results, [__, __, __], "what is the value of `results`?");
});
-test("test", function() {
+test("test", function () {
var containsSelect = /select/.test(" select * from users ");
- equal(__, containsSelect, 'does the string provided contain "select"?');
+ equal(__, containsSelect, "does the string provided contain 'select'?");
});
-test("match", function() {
+test("match", function () {
var matches = "what if 6 turned out to be 9?".match(/(\d)/g);
- ok(matches.equalTo([__, __]), 'what is the value of matches?');
+ deepEqual(matches, [__, __], "what is the value of `matches`?");
});
-test("replace", function() {
+test("replace", function () {
var pie = "apple pie".replace("apple", "strawberry");
- equal(__, pie, 'what is the value of pie?');
+ equal(__, pie, "what is the value of `pie`?");
- pie = "what if 6 turned out to be 9?".replace(/\d/g, function(number) { // the second parameter can be a string or a function
- var map = {'6': 'six','9': 'nine'};
+ pie = "what if '6' turned out to be '9'?".replace(/\d/g, function (number) {
+ // The second parameter can be a string or a function
+ var map = { "6": "six", "9": "nine" };
return map[number];
});
- equal(__, pie, 'what is the value of pie?');
+ equal(__, pie, "what is the value of `pie`?");
});
// THE END
diff --git a/topics/about_scope.js b/topics/about_scope.js
index efa802a0..d2383ea2 100644
--- a/topics/about_scope.js
+++ b/topics/about_scope.js
@@ -2,20 +2,20 @@ module("About Scope (topics/about_scope.js)");
thisIsAGlobalVariable = 77;
-test("global variables", function() {
- equal(__, thisIsAGlobalVariable, 'is thisIsAGlobalVariable defined in this scope?');
+test("global variables", function () {
+ equal(__, thisIsAGlobalVariable, "is `thisIsAGlobalVariable` defined in this scope?");
});
-test("variables declared inside of a function", function() {
+test("variables declared inside of a function", function () {
var outerVariable = "outer";
- // this is a self-invoking function. Notice that it calls itself at the end ().
- (function() {
+ // This is a self-invoking function. Notice that it calls itself at the end ().
+ (function () {
var innerVariable = "inner";
- equal(__, outerVariable, 'is outerVariable defined in this scope?');
- equal(__, innerVariable, 'is innerVariable defined in this scope?');
+ equal(__, outerVariable, "is `outerVariable` defined in this scope?");
+ equal(__, innerVariable, "is `innerVariable` defined in this scope?");
})();
- equal(__, outerVariable, 'is outerVariable defined in this scope?');
- equal(__, typeof(innerVariable), 'is innerVariable defined in this scope?');
+ equal(__, outerVariable, "is `outerVariable` defined in this scope?");
+ equal(__, typeof innerVariable, "is `innerVariable` defined in this scope?");
});
diff --git a/topics/about_strings.js b/topics/about_strings.js
index 18f9c68a..07796585 100644
--- a/topics/about_strings.js
+++ b/topics/about_strings.js
@@ -1,34 +1,34 @@
-
module("About Strings (topics/about_strings.js)");
-test("delimiters", function() {
+test("delimiters", function () {
var singleQuotedString = 'apple';
var doubleQuotedString = "apple";
- equal(__, singleQuotedString === doubleQuotedString, 'are the two strings equal?');
+ equal(__, singleQuotedString === doubleQuotedString, "are the two strings equal?");
});
-test("concatenation", function() {
+test("concatenation", function () {
var fruit = "apple";
var dish = "pie";
- equal(__, fruit + " " + dish, 'what is the value of fruit + " " + dish?');
+ equal(__, fruit + " " + dish, 'what is the value of `fruit + " " + dish`?');
});
-test("character Type", function() {
- var characterType = typeof("Amory".charAt(1)); // typeof will be explained in about reflection
- equal(__, characterType, 'Javascript has no character type');
+test("character Type", function () {
+ var characterType = typeof "Amory".charAt(1); // typeof will be explained in "about reflection"
+ equal(__, characterType, "if Javascript has no character type, what is this type?");
});
-test("escape character", function() {
- var stringWithAnEscapedCharacter = "\u0041pple";
- equal(__, stringWithAnEscapedCharacter, 'what is the value of stringWithAnEscapedCharacter?');
+test("escape character", function () {
+ // Escape sequence using an Unicode code point https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#escape_sequences
+ var stringWithAnEscapedCharacter = "\u0041pple";
+ equal(__, stringWithAnEscapedCharacter, "what is the value of `stringWithAnEscapedCharacter`?");
});
-test("string.length", function() {
+test("string.length", function () {
var fruit = "apple";
- equal(__, fruit.length, 'what is the value of fruit.length?');
+ equal(__, fruit.length, "what is the value of `fruit.length`?");
});
-test("slice", function() {
+test("slice", function () {
var fruit = "apple pie";
- equal(__, fruit.slice(0,5), 'what is the value of fruit.slice(0,5)?');
+ equal(__, fruit.slice(0, 5), "what is the value of `fruit.slice(0,5)`?");
});
diff --git a/topics/about_this.js b/topics/about_this.js
index 85185f04..380231ad 100644
--- a/topics/about_this.js
+++ b/topics/about_this.js
@@ -1,44 +1,43 @@
module("About this (topics/about_this.js)");
-test("'this' inside a method", function () {
- var person = {
- name: 'bob',
- intro: function () {
- return "Hello, my name is " + this.__;
- }
- }
- equal(person.intro(), "Hello, my name is bob", "If an object has a method can you access properties inside it?");
+test("`this` inside a method", function () {
+ var person = {
+ name: "bob",
+ intro: function () {
+ return "Hello, my name is " + this.__;
+ },
+ };
+ equal(person.intro(), "Hello, my name is bob", "if an object has a method, can you access properties inside it?");
});
-test("'this' on unattached function", function () {
- var person = {
- globalName: 'bob',
- intro: function () {
- return "Hello, my name is " + this.globalName;
- }
- }
+test("`this` on unattached function", function () {
+ var person = {
+ globalName: "bob",
+ intro: function () {
+ return "Hello, my name is " + this.globalName;
+ },
+ };
- var alias = person.intro;
-
- // if the function is not called as an object property 'this' is the global context
- // (window in a browser). This is an example. Please do not do this in practise.
- window.__ = 'Peter';
- equal(alias(), "Hello, my name is Peter", "What does 'this' refer to when it is not part of an object?");
+ var alias = person.intro;
+
+ // If the function is not called as an object property `this` is the global context
+ // (`window` in a browser). This is an example. Please do not do this in practice.
+ window.__ = "Peter";
+ equal(alias(), "Hello, my name is Peter", "what does `this` refer to when it is not part of an object?");
});
-test("'this' set explicitly", function () {
- var person = {
- name: 'bob',
- intro: function () {
- return "Hello, my name is " + this.name;
- }
- }
+test("`this` set explicitly", function () {
+ var person = {
+ name: "bob",
+ intro: function () {
+ return "Hello, my name is " + this.name;
+ },
+ };
- // calling a function with 'call' lets us assign 'this' explicitly
- var message = person.intro.call({__: "Frank"});
- equal(message, "Hello, my name is Frank", "What does 'this' refer to when you use the 'call()' method?");
+ // Calling a function with `call` lets us assign `this` explicitly
+ var message = person.intro.call({ __: "Frank" });
+ equal(message, "Hello, my name is Frank", "what does `this` refer to when you use the `call()` method?");
});
-// extra credit: underscore.js has a 'bind' function http://documentcloud.github.com/underscore/#bind
-// read the source and see how it is implemented
-
+// Extra credit: underscore.js has a `bind` function http://documentcloud.github.com/underscore/#bind
+// Read the source and see how it is implemented
diff --git a/topics/about_truthyness.js b/topics/about_truthyness.js
index 9b524c14..c3b9a171 100644
--- a/topics/about_truthyness.js
+++ b/topics/about_truthyness.js
@@ -1,22 +1,21 @@
-
module("About Truthyness (topics/about_truthyness.js)");
-test("truthyness of positive numbers", function() {
+test("truthyness of positive numbers", function () {
var oneIsTruthy = 1 ? true : false;
- equal(__, oneIsTruthy, 'is one truthy?');
+ equal(__, oneIsTruthy, "is one truthy?");
});
-test("truthyness of negative numbers", function() {
+test("truthyness of negative numbers", function () {
var negativeOneIsTruthy = -1 ? true : false;
- equal(__, negativeOneIsTruthy, 'is -1 truthy?');
+ equal(__, negativeOneIsTruthy, "is -1 truthy?");
});
-test("truthyness of zero", function() {
+test("truthyness of zero", function () {
var zeroIsTruthy = 0 ? true : false;
- equal(__, zeroIsTruthy, 'is 0 truthy?');
+ equal(__, zeroIsTruthy, "is 0 truthy?");
});
-test("truthyness of null", function() {
+test("truthyness of null", function () {
var nullIsTruthy = null ? true : false;
- equal(__, nullIsTruthy, 'is null truthy?');
+ equal(__, nullIsTruthy, "is null truthy?");
});