Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution koans Otus #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions topics/about_arrays.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
module("About Arrays (topics/about_arrays.js)");

test("array literal syntax and indexing", function() {
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?');
equal(favouriteThings[0], "cellar door");
equal(favouriteThings[1], 42);
equal(favouriteThings[2], true);
});

test("array type", function() {
equal(__, typeof([]), 'what is the type of an array?');
test("array type", function () {
equal(typeof ([]), 'object');
});

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, 3);
});

test("splice", function() {
test("splice", function () {
var daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
var workingWeek = daysOfWeek.splice(__, __);
var workingWeek = daysOfWeek.splice(0, 5);
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?');
});

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("second", stack.pop(), 'what will be the first value popped off the stack?');
equal("first", 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("third", queue.shift(), 'what will be shifted out first?');
equal("first", queue.shift(), 'what will be shifted out second?');
});
12 changes: 6 additions & 6 deletions topics/about_asserts.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

module("About Asserts (topics/about_asserts.js)");

test("ok", function() {
ok(__ === true, 'what will satisfy the ok assertion?');
test("ok", function () {
ok(true === true, 'what will satisfy the ok assertion?');
});

test("not ok", function() {
ok(__ === false, 'what is a false value?');
test("not ok", function () {
ok(false === false, 'what is a false value?');
});

test("equal", function() {
equal(__, 1 + 1, 'what will satisfy the equal assertion?');
test("equal", function () {
equal(2, 1 + 1, 'what will satisfy the equal assertion?');
});
8 changes: 4 additions & 4 deletions topics/about_assignment.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

module("About Assignment (topics/about_assignment.js)");

test("local variables", function() {
var temp = __;
test("local variables", function () {
var temp = 1;
equal(temp, 1, "Assign a value to the variable temp");
});

test("global variables", function() {
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');
equal(window.temp, temp, 'global variables are assigned to the window object');
});
60 changes: 30 additions & 30 deletions topics/about_control_structures.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
module("About Control Structures (topics/about_control_structures.js)");

test("if", function() {
test("if", function () {
var isPositive = false;
if (2 > 0) {
isPositive = true;
}
equal(__, isPositive, 'what is the value of isPositive?');
equal(true, isPositive, 'what is the value of isPositive?');
});

test("for", function() {
test("for", function () {
var counter = 10;
for (var i = 1; i <= 3; i++) {
counter = counter + i;
}
equal(__, counter, 'what is the value of counter?');
equal(16, counter, 'what is the value of counter?');
});

test("for in", function() {
test("for in", function () {
// this syntax will be explained in about objects
var person = {
name: "Amory Blaine",
Expand All @@ -25,49 +25,49 @@ test("for in", function() {
var result = "";
// for in enumerates the property names of an object
for (var property_name in person) {
result = result + property_name;
result = result + property_name;
}
equal(__, result, 'what is the value of result?');
equal("nameage", result, 'what is the value of result?');
});

test("ternary operator", function() {
test("ternary operator", function () {
var fruit = true ? "apple" : "orange";
equal(__, fruit, 'what is the value of fruit?');
equal("apple", fruit, 'what is the value of fruit?');

fruit = false ? "apple" : "orange";
equal(__, fruit, 'now what is the value of fruit?');
equal("orange", fruit, 'now what is the value of fruit?');
});

test("switch", function() {
test("switch", function () {
var result = 0;
switch (2) {
case 1:
result = 1;
break;
case 1+1:
case 1 + 1:
result = 2;
break;
}
equal(__, result, 'what is the value of result?');
equal(2, result, 'what is the value of result?');
});

test("switch default case", function() {
var result = "Pippin";
switch ("m") {
case "f":
result = "Frodo";
break;
case "s":
result = "Samwise";
break;
default:
result = "Merry";
break;
}
equal(__, result, 'what is the value of result?');
test("switch default case", function () {
var result = "Pippin";
switch ("m") {
case "f":
result = "Frodo";
break;
case "s":
result = "Samwise";
break;
default:
result = "Merry";
break;
}
equal("Merry", result, 'what is the value of result?');
});

test("null coalescing", function() {
var result = null || "a value";
equal(__, result, 'what is the value of result?');
test("null coalescing", function () {
var result = null || "a value";
equal("a value", result, 'what is the value of result?');
});
22 changes: 11 additions & 11 deletions topics/about_equality.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@

module("About Equality (topics/about_equality.js)");

test("numeric equality", function() {
equal(3 + __, 7, "");
test("numeric equality", function () {
equal(3 + 4, 7, "");
});

test("string equality", function() {
equal("3" + __, "37", "concatenate the strings");
test("string equality", function () {
equal("3" + "7", "37", "concatenate the strings");
});

test("equality without type coercion", function() {
ok(3 === __, 'what is exactly equal to 3?');
test("equality without type coercion", function () {
ok(3 === 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 () {
ok(3 == "3", 'what string is equal to 3, with type coercion?');
});

test("string literals", function() {
equal(__, "frankenstein", "quote types are interchangable, but must match.");
equal(__, 'frankenstein', "quote types can use both single and double quotes.");
test("string literals", function () {
equal(`frankenstein`, "frankenstein", "quote types are interchangable, but must match.");
equal(`frankenstein`, 'frankenstein', "quote types can use both single and double quotes.");
});
64 changes: 32 additions & 32 deletions topics/about_functions_and_closure.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
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 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("b", 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(12, 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) {
(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("shared", pv, 'what is the value of pv?');
equal("string", typeof (secretValue), "is secretValue available in this context?");
equal("string", 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("undefined", typeof (secretValue), "is secretValue available in this context?");
equal("string", 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++) {
for (var i = 0; i < arguments.length; i++) {
// complete the implementation of this method so that it returns the sum of its arguments
// __
total += arguments[i]
}
// __
return total
};

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 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, 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?");

equal("I am this!Where did it come from?", 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?");
var result = invokee.apply("I am this!", ["I am arg1", "I am arg2"]);

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

20 changes: 10 additions & 10 deletions topics/about_numbers.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

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(true, typeOfIntegers === typeOfFloats, 'are ints and floats the same type?');
equal("number", typeOfIntegers, 'what is the javascript numeric type?');
equal(1, 1.0, 'what is a integer number 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(true, isNaN(resultOfFailedOperations), 'what will satisfy the equals assertion?');
equal(false, resultOfFailedOperations == NaN, 'is NaN == NaN?');
});
Loading