From 7df8cafc1e21edeb5048c4df71e313348678b1d5 Mon Sep 17 00:00:00 2001 From: Andrey Date: Sun, 16 Oct 2022 19:27:51 +0300 Subject: [PATCH] solution koans --- koans/AboutApplyingWhatWeHaveLearnt.js | 79 +++++++++++++++----------- koans/AboutArrays.js | 72 +++++++++++------------ koans/AboutExpects.js | 26 ++++----- koans/AboutFunctions.js | 32 +++++------ koans/AboutHigherOrderFunctions.js | 76 +++++++++++++------------ koans/AboutInheritance.js | 58 +++++++++---------- koans/AboutMutability.js | 35 ++++++------ koans/AboutObjects.js | 55 +++++++++--------- 8 files changed, 220 insertions(+), 213 deletions(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index eccc93763..9b8e1bdb0 100644 --- a/koans/AboutApplyingWhatWeHaveLearnt.js +++ b/koans/AboutApplyingWhatWeHaveLearnt.js @@ -1,16 +1,16 @@ var _; //globals -describe("About Applying What We Have Learnt", function() { +describe("About Applying What We Have Learnt", function () { var products; beforeEach(function () { products = [ - { name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false }, - { name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false }, - { name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false }, - { name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true }, - { name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true } + { name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false }, + { name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false }, + { name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false }, + { name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true }, + { name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true } ]; }); @@ -18,30 +18,32 @@ describe("About Applying What We Have Learnt", function() { it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (imperative)", function () { - var i,j,hasMushrooms, productsICanEat = []; - - for (i = 0; i < products.length; i+=1) { - if (products[i].containsNuts === false) { - hasMushrooms = false; - for (j = 0; j < products[i].ingredients.length; j+=1) { - if (products[i].ingredients[j] === "mushrooms") { - hasMushrooms = true; - } - } - if (!hasMushrooms) productsICanEat.push(products[i]); + var i, j, hasMushrooms, productsICanEat = []; + + for (i = 0; i < products.length; i += 1) { + if (products[i].containsNuts === false) { + hasMushrooms = false; + for (j = 0; j < products[i].ingredients.length; j += 1) { + if (products[i].ingredients[j] === "mushrooms") { + hasMushrooms = true; + } } + if (!hasMushrooms) productsICanEat.push(products[i]); + } } - 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 = []; + var productsICanEat = []; - /* solve using filter() & all() / any() */ + productsICanEat = products.filter(product => !product.ingredients.includes("nuts") && !product.ingredients.includes("mushrooms")) - expect(productsICanEat.length).toBe(FILL_ME_IN); + /* solve using filter() & all() / any() */ + + expect(productsICanEat.length).toBe(3); }); /*********************************************************************************/ @@ -49,41 +51,50 @@ describe("About Applying What We Have Learnt", function() { it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (imperative)", function () { var sum = 0; - for(var i=1; i<1000; i+=1) { + for (var i = 1; i < 1000; i += 1) { if (i % 3 === 0 || i % 5 === 0) { sum += i; } } - 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, el) => { + if (el % 3 === 0 || el % 5 === 0) { + acc += el + } + return acc + }, 0) - expect(233168).toBe(FILL_ME_IN); + expect(sum).toBe(233168); }); /*********************************************************************************/ - it("should count the ingredient occurrence (imperative)", function () { + it("should count the ingredient occurrence (imperative)", function () { var ingredientCount = { "{ingredient name}": 0 }; - for (i = 0; i < products.length; i+=1) { - for (j = 0; j < products[i].ingredients.length; j+=1) { - ingredientCount[products[i].ingredients[j]] = (ingredientCount[products[i].ingredients[j]] || 0) + 1; - } + for (i = 0; i < products.length; i += 1) { + for (j = 0; j < products[i].ingredients.length; j += 1) { + ingredientCount[products[i].ingredients[j]] = (ingredientCount[products[i].ingredients[j]] || 0) + 1; + } } - 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() */ + var ingredientCount = products.reduce((acc, el) => { + el.ingredients.forEach(item => { + acc[item] = acc[item] ? acc[item] + 1 : 1 + }) + return acc + }, {}) - expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN); + expect(ingredientCount['mushrooms']).toBe(2); }); /*********************************************************************************/ diff --git a/koans/AboutArrays.js b/koans/AboutArrays.js index f9b33f8cc..30a81b558 100644 --- a/koans/AboutArrays.js +++ b/koans/AboutArrays.js @@ -1,18 +1,18 @@ -describe("About Arrays", function() { +describe("About Arrays", function () { //We shall contemplate truth by testing reality, via spec expectations. - it("should create arrays", function() { + 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); - - 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(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(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 () { @@ -23,75 +23,75 @@ 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 () { - var array = [ "zero", "one", "two", "three", "four", "five" ]; + var array = ["zero", "one", "two", "three", "four", "five"]; function passedByReference(refArray) { - refArray[1] = "changed in 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]); }); }); diff --git a/koans/AboutExpects.js b/koans/AboutExpects.js index 7d1a827cb..577628ec0 100644 --- a/koans/AboutExpects.js +++ b/koans/AboutExpects.js @@ -1,40 +1,40 @@ -describe('About Expects', function() { +describe('About Expects', function () { // We shall contemplate truth by testing reality, via spec expectations. - it('should expect true', 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; + it('should expect equality', function () { + 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; + it('should assert equality a better way', function () { + var expectedValue = 2; var actualValue = 1 + 1; - // toEqual() compares using common sense equality. + // toEqual() compares using common sense equality. expect(actualValue).toEqual(expectedValue); }); // Sometimes you need to be precise about what you "type." - it('should assert equality with ===', function() { - var expectedValue = FILL_ME_IN; + it('should assert equality with ===', function () { + var expectedValue = "2"; var actualValue = (1 + 1).toString(); - // toBe() will always use === to compare. + // toBe() will always use === to compare. expect(actualValue).toBe(expectedValue); }); // Sometimes we will ask you to fill in the values. - it('should have filled in values', function() { - expect(1 + 1).toEqual(FILL_ME_IN); + it('should have filled in values', function () { + expect(1 + 1).toEqual(2); }); }); diff --git a/koans/AboutFunctions.js b/koans/AboutFunctions.js index 569100806..9ed4fde71 100644 --- a/koans/AboutFunctions.js +++ b/koans/AboutFunctions.js @@ -1,12 +1,12 @@ -describe("About Functions", function() { +describe("About Functions", function () { - it("should declare functions", function() { + it("should declare functions", function () { function add(a, b) { 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 () { @@ -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 () { @@ -35,15 +35,13 @@ 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 () { - function makeMysteryFunction(makerValue) - { - var newFunction = function doMysteriousThing(param) - { + function makeMysteryFunction(makerValue) { + var newFunction = function doMysteriousThing(param) { return makerValue + param; }; return newFunction; @@ -52,7 +50,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 () { @@ -61,13 +59,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 = []; @@ -77,7 +75,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 () { @@ -91,10 +89,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!"); }); }); diff --git a/koans/AboutHigherOrderFunctions.js b/koans/AboutHigherOrderFunctions.js index 07c416792..412b46e14 100644 --- a/koans/AboutHigherOrderFunctions.js +++ b/koans/AboutHigherOrderFunctions.js @@ -8,33 +8,34 @@ var _; //globals describe("About Higher Order Functions", function () { it("should use filter to return array items that meet a criteria", function () { - var numbers = [1,2,3]; + 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); + console.log(numbers) + 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 }); + 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); + 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 () { - var numbers = [1,2,3]; + var numbers = [1, 2, 3]; var msg = ""; var isEven = function (item) { msg += (item % 2) === 0; @@ -42,48 +43,49 @@ 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 () { - var onlyEven = [2,4,6]; - var mixedBag = [2,4,5,6]; + var onlyEven = [2, 4, 6]; + var mixedBag = [2, 4, 5, 6]; - var isEven = function(x) { return x % 2 === 0 }; + 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 () { - var onlyEven = [2,4,6]; - var mixedBag = [2,4,5,6]; + it("should use 'any' to test if any items passes condition", function () { + var onlyEven = [2, 4, 6]; + var mixedBag = [2, 4, 5, 6]; - var isEven = function(x) { return x % 2 === 0 }; + 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); + it("should use range to generate an array", function () { + console.log(_.range(3)) + 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); + it("should use flatten to make nested arrays easy to work with", function () { + expect(_([[1, 2], [3, 4]]).flatten()).toEqual([1, 2, 3, 4]); }); - it("should use chain() ... .value() to use multiple higher order functions", function() { - var result = _([ [0, 1], 2 ]).chain() - .flatten() - .map(function(x) { return x+1 } ) - .reduce(function (sum, x) { return sum + x }) - .value(); + it("should use chain() ... .value() to use multiple higher order functions", function () { + var result = _([[0, 1], 2]).chain() + .flatten() + .map(function (x) { return x + 1 }) + .reduce(function (sum, x) { return sum + x }) + .value(); - expect(result).toEqual(FILL_ME_IN); + expect(result).toEqual(6); }); }); diff --git a/koans/AboutInheritance.js b/koans/AboutInheritance.js index 5dba545b0..5af602cd6 100644 --- a/koans/AboutInheritance.js +++ b/koans/AboutInheritance.js @@ -2,8 +2,8 @@ function Muppet(age, hobby) { this.age = age; this.hobby = hobby; - this.answerNanny = function(){ - return "Everything's cool!"; + this.answerNanny = function () { + return "Everything's cool!"; } } @@ -11,40 +11,40 @@ function SwedishChef(age, hobby, mood) { Muppet.call(this, age, hobby); this.mood = mood; - this.cook = function() { + this.cook = function () { return "Mmmm soup!"; } } SwedishChef.prototype = new Muppet(); -describe("About inheritance", function() { - beforeEach(function(){ +describe("About inheritance", function () { + beforeEach(function () { this.muppet = new Muppet(2, "coding"); - this.swedishChef = new SwedishChef(2, "cooking", "chillin"); + this.swedishChef = new SwedishChef(2, "cooking", "chillin"); }); - it("should be able to call a method on the derived object", function() { - expect(this.swedishChef.cook()).toEqual(FILL_ME_IN); + it("should be able to call a method on the derived object", function () { + expect(this.swedishChef.cook()).toEqual("Mmmm soup!"); }); - it("should be able to call a method on the base object", function() { - expect(this.swedishChef.answerNanny()).toEqual(FILL_ME_IN); + it("should be able to call a method on the base object", function () { + expect(this.swedishChef.answerNanny()).toEqual("Everything's cool!"); }); - it("should set constructor parameters on the base object", function() { - expect(this.swedishChef.age).toEqual(FILL_ME_IN); - expect(this.swedishChef.hobby).toEqual(FILL_ME_IN); + it("should set constructor parameters on the base object", function () { + expect(this.swedishChef.age).toEqual(2); + expect(this.swedishChef.hobby).toEqual("cooking"); }); - it("should set constructor parameters on the derived object", function() { - expect(this.swedishChef.mood).toEqual(FILL_ME_IN); + it("should set constructor parameters on the derived object", function () { + expect(this.swedishChef.mood).toEqual("chillin"); }); }); // http://javascript.crockford.com/prototypal.html Object.prototype.beget = function () { - function F() {} + function F() { } F.prototype = this; return new F(); } @@ -53,7 +53,7 @@ function Gonzo(age, hobby, trick) { Muppet.call(this, age, hobby); this.trick = trick; - this.doTrick = function() { + this.doTrick = function () { return this.trick; } } @@ -61,25 +61,25 @@ function Gonzo(age, hobby, trick) { //no longer need to call the Muppet (base type) constructor Gonzo.prototype = Muppet.prototype.beget(); -describe("About Crockford's inheritance improvement", function() { - beforeEach(function(){ - this.gonzo = new Gonzo(3, "daredevil performer", "eat a tire"); +describe("About Crockford's inheritance improvement", function () { + beforeEach(function () { + this.gonzo = new Gonzo(3, "daredevil performer", "eat a tire"); }); - it("should be able to call a method on the derived object", function() { - expect(this.gonzo.doTrick()).toEqual(FILL_ME_IN); + it("should be able to call a method on the derived object", function () { + expect(this.gonzo.doTrick()).toEqual("eat a tire"); }); - it("should be able to call a method on the base object", function() { - expect(this.gonzo.answerNanny()).toEqual(FILL_ME_IN); + it("should be able to call a method on the base object", function () { + expect(this.gonzo.answerNanny()).toEqual('Everything\'s cool!'); }); - it("should set constructor parameters on the base object", function() { - expect(this.gonzo.age).toEqual(FILL_ME_IN); - expect(this.gonzo.hobby).toEqual(FILL_ME_IN); + it("should set constructor parameters on the base object", function () { + expect(this.gonzo.age).toEqual(3); + expect(this.gonzo.hobby).toEqual("daredevil performer"); }); - it("should set constructor parameters on the derived object", function() { - expect(this.gonzo.trick).toEqual(FILL_ME_IN); + it("should set constructor parameters on the derived object", function () { + expect(this.gonzo.trick).toEqual("eat a tire"); }); }); diff --git a/koans/AboutMutability.js b/koans/AboutMutability.js index fd9b69af2..f0e99cf1b 100644 --- a/koans/AboutMutability.js +++ b/koans/AboutMutability.js @@ -1,27 +1,25 @@ -describe("About Mutability", function() { +describe("About Mutability", function () { it("should expect object properties to be public and mutable", function () { - var aPerson = {firstname: "John", lastname: "Smith" }; + var aPerson = { firstname: "John", lastname: "Smith" }; aPerson.firstname = "Alan"; - expect(aPerson.firstname).toBe(FILL_ME_IN); + expect(aPerson.firstname).toBe("Alan"); }); it("should understand that constructed properties are public and mutable", function () { - function Person(firstname, lastname) - { + function Person(firstname, lastname) { this.firstname = firstname; this.lastname = lastname; } - var aPerson = new Person ("John", "Smith"); + var aPerson = new Person("John", "Smith"); aPerson.firstname = "Alan"; - expect(aPerson.firstname).toBe(FILL_ME_IN); + expect(aPerson.firstname).toBe("Alan"); }); it("should expect prototype properties to be public and mutable", function () { - function Person(firstname, lastname) - { + function Person(firstname, lastname) { this.firstname = firstname; this.lastname = lastname; } @@ -29,40 +27,39 @@ describe("About Mutability", function() { return this.firstname + " " + this.lastname; }; - var aPerson = new Person ("John", "Smith"); - expect(aPerson.getFullName()).toBe(FILL_ME_IN); + var aPerson = new Person("John", "Smith"); + expect(aPerson.getFullName()).toBe("John Smith"); aPerson.getFullName = function () { return this.lastname + ", " + this.firstname; }; - expect(aPerson.getFullName()).toBe(FILL_ME_IN); + expect(aPerson.getFullName()).toBe("Smith, John"); }); it("should know that variables inside a constructor and constructor args are private", function () { - function Person(firstname, lastname) - { + function Person(firstname, lastname) { var fullName = firstname + " " + lastname; this.getFirstName = function () { return firstname; }; this.getLastName = function () { return lastname; }; this.getFullName = function () { return fullName; }; } - var aPerson = new Person ("John", "Smith"); + var aPerson = new Person("John", "Smith"); aPerson.firstname = "Penny"; aPerson.lastname = "Andrews"; aPerson.fullName = "Penny Andrews"; - expect(aPerson.getFirstName()).toBe(FILL_ME_IN); - expect(aPerson.getLastName()).toBe(FILL_ME_IN); - expect(aPerson.getFullName()).toBe(FILL_ME_IN); + expect(aPerson.getFirstName()).toBe("John"); + expect(aPerson.getLastName()).toBe("Smith"); + expect(aPerson.getFullName()).toBe("John Smith"); aPerson.getFullName = function () { return aPerson.lastname + ", " + aPerson.firstname; }; - expect(aPerson.getFullName()).toBe(FILL_ME_IN); + expect(aPerson.getFullName()).toBe("Andrews, Penny"); }); }); diff --git a/koans/AboutObjects.js b/koans/AboutObjects.js index 9f9ed93cc..d8e7486ba 100644 --- a/koans/AboutObjects.js +++ b/koans/AboutObjects.js @@ -4,23 +4,23 @@ describe("About Objects", function () { var megalomaniac; beforeEach(function () { - megalomaniac = { mastermind: "Joker", henchwoman: "Harley" }; + megalomaniac = { mastermind: "Joker", henchwoman: "Harley" }; }); it("should confirm objects are collections of properties", function () { - expect(megalomaniac.mastermind).toBe(FILL_ME_IN); + expect(megalomaniac.mastermind).toBe("Joker"); }); it("should confirm that properties are case sensitive", function () { - expect(megalomaniac.henchwoman).toBe(FILL_ME_IN); - expect(megalomaniac.henchWoman).toBe(FILL_ME_IN); + expect(megalomaniac.henchwoman).toBe("Harley"); + expect(megalomaniac.henchWoman).toBe(undefined); }); }); it("should know properties that are functions act like methods", function () { var megalomaniac = { - mastermind : "Brain", + mastermind: "Brain", henchman: "Pinky", battleCry: function (noOfBrains) { return "They are " + this.henchman + " and the" + @@ -29,7 +29,7 @@ describe("About Objects", function () { }; var battleCry = megalomaniac.battleCry(4); - expect(FILL_ME_IN).toMatch(battleCry); + expect('They are Pinky and the Brain Brain Brain Brain').toMatch(battleCry); }); it("should confirm that when a function is attached to an object, 'this' refers to the object", function () { @@ -44,8 +44,8 @@ describe("About Objects", function () { } }; - expect(currentYear).toBe(FILL_ME_IN); - expect(megalomaniac.calculateAge()).toBe(FILL_ME_IN); + expect(currentYear).toBe(2022); + expect(megalomaniac.calculateAge()).toBe(52); }); describe("'in' keyword", function () { @@ -62,48 +62,47 @@ describe("About Objects", function () { var hasBomb = "theBomb" in megalomaniac; - expect(hasBomb).toBe(FILL_ME_IN); + expect(hasBomb).toBe(true); }); it("should not have the detonator however", function () { var hasDetonator = "theDetonator" in megalomaniac; - expect(hasDetonator).toBe(FILL_ME_IN); + expect(hasDetonator).toBe(false); }); }); it("should know that properties can be added and deleted", function () { - var megalomaniac = { mastermind : "Agent Smith", henchman: "Agent Smith" }; + var megalomaniac = { mastermind: "Agent Smith", henchman: "Agent Smith" }; - expect("secretary" in megalomaniac).toBe(FILL_ME_IN); + expect("secretary" in megalomaniac).toBe(false); megalomaniac.secretary = "Agent Smith"; - expect("secretary" in megalomaniac).toBe(FILL_ME_IN); + expect("secretary" in megalomaniac).toBe(true); delete megalomaniac.henchman; - expect("henchman" in megalomaniac).toBe(FILL_ME_IN); + expect("henchman" in megalomaniac).toBe(false); }); it("should use prototype to add to all objects", function () { - function Circle(radius) - { - this.radius = radius; - } + function Circle(radius) { + this.radius = radius; + } - var simpleCircle = new Circle(10); - var colouredCircle = new Circle(5); - colouredCircle.colour = "red"; + var simpleCircle = new Circle(10); + var colouredCircle = new Circle(5); + colouredCircle.colour = "red"; - expect(simpleCircle.colour).toBe(FILL_ME_IN); - expect(colouredCircle.colour).toBe(FILL_ME_IN); + expect(simpleCircle.colour).toBe(undefined); + expect(colouredCircle.colour).toBe("red"); - Circle.prototype.describe = function () { - return "This circle has a radius of: " + this.radius; - }; + Circle.prototype.describe = function () { + return "This circle has a radius of: " + this.radius; + }; - expect(simpleCircle.describe()).toBe(FILL_ME_IN); - expect(colouredCircle.describe()).toBe(FILL_ME_IN); + expect(simpleCircle.describe()).toBe("This circle has a radius of: 10"); + expect(colouredCircle.describe()).toBe("This circle has a radius of: 5"); }); });