Skip to content
This repository has been archived by the owner. It is now read-only.

homework #1

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
25 changes: 18 additions & 7 deletions koans/AboutApplyingWhatWeHaveLearnt.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ describe("About Applying What We Have Learnt", function() {
}
}

expect(productsICanEat.length).toBe(FILL_ME_IN);
expect(productsICanEat.length).toBe(1);
});

it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () {

var productsICanEat = [];

/* solve using filter() & all() / any() */
var noNuts = products.filter(pizza => pizza.containsNuts === false)
productsICanEat = noNuts.filter(pizza => !pizza.ingredients.includes('mushrooms'))

expect(productsICanEat.length).toBe(FILL_ME_IN);
expect(productsICanEat.length).toBe(1);
});

/*********************************************************************************/
Expand All @@ -55,14 +57,19 @@ describe("About Applying What We Have Learnt", function() {
}
}

expect(sum).toBe(FILL_ME_IN);
expect(sum).toBe(233168);
});

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

var sum = FILL_ME_IN; /* try chaining range() and reduce() */
var sum = _.range(1, 1000).reduce((acc, cur) => {
if (cur % 5 === 0 || cur % 3 === 0) {
return acc + cur;
}
return acc;
}, 0); /* try chaining range() and reduce() */

expect(233168).toBe(FILL_ME_IN);
expect(sum).toBe(233168);
});

/*********************************************************************************/
Expand All @@ -75,15 +82,19 @@ describe("About Applying What We Have Learnt", function() {
}
}

expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN);
expect(ingredientCount['mushrooms']).toBe(2);
});

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

/* chain() together map(), flatten() and reduce() */

expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN);
_(products).chain()
.map(pizza => pizza.ingredients)
.flatten().reduce((list, ingredient) => ingredientCount[ingredient] = (ingredientCount[ingredient] || 0) + 1);

expect(ingredientCount['mushrooms']).toBe(2);
});

/*********************************************************************************/
Expand Down
60 changes: 30 additions & 30 deletions koans/AboutArrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ describe("About Arrays", function() {
//We shall contemplate truth by testing reality, via spec expectations.
it("should create arrays", function() {
var emptyArray = [];
expect(typeof(emptyArray)).toBe(FILL_ME_IN); //A mistake? - http://javascript.crockford.com/remedial.html
expect(emptyArray.length).toBe(FILL_ME_IN);
expect(typeof(emptyArray)).toBe('object'); //A mistake? - http://javascript.crockford.com/remedial.html
expect(emptyArray.length).toBe(0);

var multiTypeArray = [0, 1, "two", function () { return 3; }, {value1: 4, value2: 5}, [6, 7]];
expect(multiTypeArray[0]).toBe(FILL_ME_IN);
expect(multiTypeArray[2]).toBe(FILL_ME_IN);
expect(multiTypeArray[3]()).toBe(FILL_ME_IN);
expect(multiTypeArray[4].value1).toBe(FILL_ME_IN);
expect(multiTypeArray[4]["value2"]).toBe(FILL_ME_IN);
expect(multiTypeArray[5][0]).toBe(FILL_ME_IN);
expect(multiTypeArray[0]).toBe(0);
expect(multiTypeArray[2]).toBe("two");
expect(multiTypeArray[3]()).toBe(3);
expect(multiTypeArray[4].value1).toBe(4);
expect(multiTypeArray[4]["value2"]).toBe(5);
expect(multiTypeArray[5][0]).toBe(6);
});

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

array[1] = 2;
expect(array).toEqual([1, FILL_ME_IN]);
expect(array).toEqual([1, 2]);

array.push(3);
expect(array).toEqual(FILL_ME_IN);
expect(array).toEqual([1, 2, 3]);
});

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

expect(fourNumberArray.length).toBe(FILL_ME_IN);
expect(fourNumberArray.length).toBe(4);
fourNumberArray.push(5, 6);
expect(fourNumberArray.length).toBe(FILL_ME_IN);
expect(fourNumberArray.length).toBe(6);

var tenEmptyElementArray = new Array(10);
expect(tenEmptyElementArray.length).toBe(FILL_ME_IN);
expect(tenEmptyElementArray.length).toBe(10);

tenEmptyElementArray.length = 5;
expect(tenEmptyElementArray.length).toBe(FILL_ME_IN);
expect(tenEmptyElementArray.length).toBe(5);
});

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

expect(array.slice(0, 1)).toEqual(FILL_ME_IN);
expect(array.slice(0, 2)).toEqual(FILL_ME_IN);
expect(array.slice(2, 2)).toEqual(FILL_ME_IN);
expect(array.slice(2, 20)).toEqual(FILL_ME_IN);
expect(array.slice(3, 0)).toEqual(FILL_ME_IN);
expect(array.slice(3, 100)).toEqual(FILL_ME_IN);
expect(array.slice(5, 1)).toEqual(FILL_ME_IN);
expect(array.slice(0, 1)).toEqual(['peanut']);
expect(array.slice(0, 2)).toEqual(["peanut", "butter"]);
expect(array.slice(2, 2)).toEqual([]);
expect(array.slice(2, 20)).toEqual(["and", "jelly"]);
expect(array.slice(3, 0)).toEqual([]);
expect(array.slice(3, 100)).toEqual(["jelly"]);
expect(array.slice(5, 1)).toEqual([]);
});

it("should know array references", function () {
Expand All @@ -62,36 +62,36 @@ describe("About Arrays", function() {
refArray[1] = "changed in function";
}
passedByReference(array);
expect(array[1]).toBe(FILL_ME_IN);
expect(array[1]).toBe("changed in function");

var assignedArray = array;
assignedArray[5] = "changed in assignedArray";
expect(array[5]).toBe(FILL_ME_IN);
expect(array[5]).toBe("changed in assignedArray");

var copyOfArray = array.slice();
copyOfArray[3] = "changed in copyOfArray";
expect(array[3]).toBe(FILL_ME_IN);
expect(array[3]).toBe("three");
});

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

expect(array).toEqual(FILL_ME_IN);
expect(array).toEqual([1, 2, 3]);

var poppedValue = array.pop();
expect(poppedValue).toBe(FILL_ME_IN);
expect(array).toEqual(FILL_ME_IN);
expect(poppedValue).toBe(3);
expect(array).toEqual([1, 2]);
});

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

array.unshift(3);
expect(array).toEqual(FILL_ME_IN);
expect(array).toEqual([3, 1, 2]);

var shiftedValue = array.shift();
expect(shiftedValue).toEqual(FILL_ME_IN);
expect(array).toEqual(FILL_ME_IN);
expect(shiftedValue).toEqual(3);
expect(array).toEqual([1, 2]);
});
});
10 changes: 5 additions & 5 deletions koans/AboutExpects.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ describe('About Expects', function() {
it('should expect true', function() {

// Your journey begins here: Replace the word false with true
expect(false).toBeTruthy();
expect(true).toBeTruthy();
});

// To understand reality, we must compare our expectations against reality.
it('should expect equality', function() {
var expectedValue = FILL_ME_IN;
var expectedValue = 2;
var actualValue = 1 + 1;

expect(actualValue === expectedValue).toBeTruthy();
});

// Some ways of asserting equality are better than others.
it('should assert equality a better way', function() {
var expectedValue = FILL_ME_IN;
var expectedValue = 2;
var actualValue = 1 + 1;

// toEqual() compares using common sense equality.
Expand All @@ -26,7 +26,7 @@ describe('About Expects', function() {

// Sometimes you need to be precise about what you "type."
it('should assert equality with ===', function() {
var expectedValue = FILL_ME_IN;
var expectedValue = '2';
var actualValue = (1 + 1).toString();

// toBe() will always use === to compare.
Expand All @@ -35,6 +35,6 @@ describe('About Expects', function() {

// Sometimes we will ask you to fill in the values.
it('should have filled in values', function() {
expect(1 + 1).toEqual(FILL_ME_IN);
expect(1 + 1).toEqual(2);
});
});
22 changes: 11 additions & 11 deletions koans/AboutFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("About Functions", function() {
return a + b;
}

expect(add(1, 2)).toBe(FILL_ME_IN);
expect(add(1, 2)).toBe(3);
});

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

expect(getMessage()).toBe(FILL_ME_IN);
expect(overrideMessage()).toBe(FILL_ME_IN);
expect(message).toBe(FILL_ME_IN);
expect(getMessage()).toBe("Outer");
expect(overrideMessage()).toBe("Inner");
expect(message).toBe("Outer");
});

it("should have lexical scoping", function () {
Expand All @@ -35,7 +35,7 @@ describe("About Functions", function() {
}
return childfunction();
}
expect(parentfunction()).toBe(FILL_ME_IN);
expect(parentfunction()).toBe("local");
});

it("should use lexical scoping to synthesise functions", function () {
Expand All @@ -52,7 +52,7 @@ describe("About Functions", function() {
var mysteryFunction3 = makeMysteryFunction(3);
var mysteryFunction5 = makeMysteryFunction(5);

expect(mysteryFunction3(10) + mysteryFunction5(5)).toBe(FILL_ME_IN);
expect(mysteryFunction3(10) + mysteryFunction5(5)).toBe(23);
});

it("should allow extra function arguments", function () {
Expand All @@ -61,13 +61,13 @@ describe("About Functions", function() {
return firstArg;
}

expect(returnFirstArg("first", "second", "third")).toBe(FILL_ME_IN);
expect(returnFirstArg("first", "second", "third")).toBe("first");

function returnSecondArg(firstArg, secondArg) {
return secondArg;
}

expect(returnSecondArg("only give first arg")).toBe(FILL_ME_IN);
expect(returnSecondArg("only give first arg")).toBe(undefined);

function returnAllArgs() {
var argsArray = [];
Expand All @@ -77,7 +77,7 @@ describe("About Functions", function() {
return argsArray.join(",");
}

expect(returnAllArgs("first", "second", "third")).toBe(FILL_ME_IN);
expect(returnAllArgs("first", "second", "third")).toBe('first,second,third');
});

it("should pass functions as values", function () {
Expand All @@ -91,10 +91,10 @@ describe("About Functions", function() {
};

var praiseSinger = { givePraise: appendRules };
expect(praiseSinger.givePraise("John")).toBe(FILL_ME_IN);
expect(praiseSinger.givePraise("John")).toBe("John rules!");

praiseSinger.givePraise = appendDoubleRules;
expect(praiseSinger.givePraise("Mary")).toBe(FILL_ME_IN);
expect(praiseSinger.givePraise("Mary")).toBe("Mary totally rules!");

});
});
36 changes: 18 additions & 18 deletions koans/AboutHigherOrderFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ describe("About Higher Order Functions", function () {
var numbers = [1,2,3];
var odd = _(numbers).filter(function (x) { return x % 2 !== 0 });

expect(odd).toEqual(FILL_ME_IN);
expect(odd.length).toBe(FILL_ME_IN);
expect(numbers.length).toBe(FILL_ME_IN);
expect(odd).toEqual([1,3]);
expect(odd.length).toBe(2);
expect(numbers.length).toBe(3);
});

it("should use 'map' to transform each element", function () {
var numbers = [1, 2, 3];
var numbersPlus1 = _(numbers).map(function(x) { return x + 1 });

expect(numbersPlus1).toEqual(FILL_ME_IN);
expect(numbers).toEqual(FILL_ME_IN);
expect(numbersPlus1).toEqual([2, 3, 4]);
expect(numbers).toEqual([1, 2, 3]);
});

it("should use 'reduce' to update the same result on each iteration", function () {
var numbers = [1, 2, 3];
var reduction = _(numbers).reduce(
function(/* result from last call */ memo, /* current */ x) { return memo + x }, /* initial */ 0);

expect(reduction).toBe(FILL_ME_IN);
expect(numbers).toEqual(FILL_ME_IN);
expect(reduction).toBe(6);
expect(numbers).toEqual([1, 2, 3]);
});

it("should use 'forEach' for simple iteration", function () {
Expand All @@ -42,8 +42,8 @@ describe("About Higher Order Functions", function () {

_(numbers).forEach(isEven);

expect(msg).toEqual(FILL_ME_IN);
expect(numbers).toEqual(FILL_ME_IN);
expect(msg).toEqual('falsetruefalse');
expect(numbers).toEqual([1,2,3]);
});

it("should use 'all' to test whether all items pass condition", function () {
Expand All @@ -52,8 +52,8 @@ describe("About Higher Order Functions", function () {

var isEven = function(x) { return x % 2 === 0 };

expect(_(onlyEven).all(isEven)).toBe(FILL_ME_IN);
expect(_(mixedBag).all(isEven)).toBe(FILL_ME_IN);
expect(_(onlyEven).all(isEven)).toBe(true);
expect(_(mixedBag).all(isEven)).toBe(false);
});

it("should use 'any' to test if any items passes condition" , function () {
Expand All @@ -62,18 +62,18 @@ describe("About Higher Order Functions", function () {

var isEven = function(x) { return x % 2 === 0 };

expect(_(onlyEven).any(isEven)).toBe(FILL_ME_IN);
expect(_(mixedBag).any(isEven)).toBe(FILL_ME_IN);
expect(_(onlyEven).any(isEven)).toBe(true);
expect(_(mixedBag).any(isEven)).toBe(true);
});

it("should use range to generate an array", function() {
expect(_.range(3)).toEqual(FILL_ME_IN);
expect(_.range(1, 4)).toEqual(FILL_ME_IN);
expect(_.range(0, -4, -1)).toEqual(FILL_ME_IN);
expect(_.range(3)).toEqual([0,1,2]);
expect(_.range(1, 4)).toEqual([1,2,3]);
expect(_.range(0, -4, -1)).toEqual([0,-1,-2,-3]);
});

it("should use flatten to make nested arrays easy to work with", function() {
expect(_([ [1, 2], [3, 4] ]).flatten()).toEqual(FILL_ME_IN);
expect(_([ [1, 2], [3, 4] ]).flatten()).toEqual([1,2,3,4]);
});

it("should use chain() ... .value() to use multiple higher order functions", function() {
Expand All @@ -83,7 +83,7 @@ describe("About Higher Order Functions", function () {
.reduce(function (sum, x) { return sum + x })
.value();

expect(result).toEqual(FILL_ME_IN);
expect(result).toEqual(6);
});

});
Expand Down
Loading