From 73401ac0104dd1e7d1cc3d6be72a18baa9423c10 Mon Sep 17 00:00:00 2001 From: CQL Date: Wed, 15 May 2013 16:12:27 -0700 Subject: [PATCH 1/6] Progress on koans. --- koans/AboutArrays.js | 54 +++++++++++++++++++++---------------------- koans/AboutExpects.js | 10 ++++---- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/koans/AboutArrays.js b/koans/AboutArrays.js index 6fe4313cc..fa4925a15 100644 --- a/koans/AboutArrays.js +++ b/koans/AboutArrays.js @@ -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 () { @@ -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 () { @@ -62,26 +62,26 @@ 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 () { diff --git a/koans/AboutExpects.js b/koans/AboutExpects.js index 52148b243..dde3947fd 100644 --- a/koans/AboutExpects.js +++ b/koans/AboutExpects.js @@ -2,12 +2,12 @@ describe("About Expects", function() { //We shall contemplate truth by testing reality, via spec expectations. it("should expect true", function() { - expect(false).toBeTruthy(); //This should be true + expect(true).toBeTruthy(); //This should be true }); //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(); @@ -15,7 +15,7 @@ describe("About Expects", function() { //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. @@ -24,7 +24,7 @@ describe("About Expects", function() { //Sometimes you need to be really exact 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. @@ -33,6 +33,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); }); }); From 031f016519e8e20ca34923a9f826a5f7a310245e Mon Sep 17 00:00:00 2001 From: quetzaluz Date: Wed, 15 May 2013 20:14:35 -0700 Subject: [PATCH 2/6] Progress on koans. --- koans/AboutArrays.js | 6 ++--- koans/AboutFunctions.js | 30 +++++++++++++------------ koans/AboutHigherOrderFunctions.js | 36 +++++++++++++++--------------- koans/AboutInheritance.js | 20 ++++++++--------- koans/AboutMutability.js | 16 ++++++------- koans/AboutObjects.js | 30 ++++++++++++------------- 6 files changed, 70 insertions(+), 68 deletions(-) diff --git a/koans/AboutArrays.js b/koans/AboutArrays.js index fa4925a15..11d8aa9be 100644 --- a/koans/AboutArrays.js +++ b/koans/AboutArrays.js @@ -88,10 +88,10 @@ describe("About 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/AboutFunctions.js b/koans/AboutFunctions.js index 77b379a3a..35a600ab5 100644 --- a/koans/AboutFunctions.js +++ b/koans/AboutFunctions.js @@ -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 () { @@ -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,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 () { @@ -49,7 +49,7 @@ describe("About Functions", function() { var increaseBy3 = makeIncreaseByFunction(3); var increaseBy5 = makeIncreaseByFunction(5); - expect(increaseBy3(10) + increaseBy5(10)).toBe(FILL_ME_IN); + expect(increaseBy3(10) + increaseBy5(10)).toBe(28); }); it("should allow extra function arguments", function () { @@ -58,13 +58,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 = []; @@ -74,7 +74,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 () { @@ -88,21 +88,23 @@ 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!"); }); it("should use function body as a string", function () { var add = new Function("a", "b", "return a + b;"); - expect(add(1, 2)).toBe(FILL_ME_IN); + expect(add(1, 2)).toBe(3); var multiply = function (a, b) { //An internal comment return a * b; }; - expect(multiply.toString()).toBe(FILL_ME_IN); - }); + //Wow, tried escaping the multiple lines with \n, had a lot of trouble here. + var sourceNoWhitespace = multiply.toString().replace(/\s+/g, ' '); + expect(sourceNoWhitespace).toBe("function (a, b) { //An internal comment return a * b; }"); +}); }); diff --git a/koans/AboutHigherOrderFunctions.js b/koans/AboutHigherOrderFunctions.js index c45b24a0e..c4dcc5543 100644 --- a/koans/AboutHigherOrderFunctions.js +++ b/koans/AboutHigherOrderFunctions.js @@ -11,17 +11,17 @@ 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 () { @@ -34,8 +34,8 @@ describe("About Higher Order Functions", function () { /* 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 () { @@ -47,8 +47,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 () { @@ -57,8 +57,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 () { @@ -67,18 +67,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() { @@ -88,7 +88,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); }); }); diff --git a/koans/AboutInheritance.js b/koans/AboutInheritance.js index 1646d8310..00f48fab1 100644 --- a/koans/AboutInheritance.js +++ b/koans/AboutInheritance.js @@ -25,20 +25,20 @@ describe("About inheritance", function() { }); it("should be able to call a method on the derived object", function() { - expect(this.swedishChef.cook()).toEqual(FILL_ME_IN); + 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); + 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); + 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); + expect(this.swedishChef.mood).toEqual("chillin"); }); }); @@ -71,19 +71,19 @@ describe("About Crockford's inheritance improvement", function() { }); it("should be able to call a method on the derived object", function() { - expect(this.gonzo.doTrick()).toEqual(FILL_ME_IN); + 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); + 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); + 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); + expect(this.gonzo.trick).toEqual("eat a tire"); }); }); diff --git a/koans/AboutMutability.js b/koans/AboutMutability.js index 1e4511758..32249c43e 100644 --- a/koans/AboutMutability.js +++ b/koans/AboutMutability.js @@ -4,7 +4,7 @@ describe("About Mutability", function() { 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 () { @@ -16,7 +16,7 @@ describe("About Mutability", function() { 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 () { @@ -30,13 +30,13 @@ describe("About Mutability", function() { }; var aPerson = new Person ("John", "Smith"); - expect(aPerson.getFullName()).toBe(FILL_ME_IN); + 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 () { @@ -54,15 +54,15 @@ describe("About Mutability", function() { 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"); //Interesting }); }); diff --git a/koans/AboutObjects.js b/koans/AboutObjects.js index 374944e80..c48e1526b 100644 --- a/koans/AboutObjects.js +++ b/koans/AboutObjects.js @@ -8,12 +8,12 @@ describe("About Objects", function () { }); it("should confirm objects are collections of properties", function () { - expect(meglomaniac.mastermind).toBe(FILL_ME_IN); + expect(meglomaniac.mastermind).toBe("Joker"); }); it("should confirm that properties are case sensitive", function () { - expect(meglomaniac.henchwoman).toBe(FILL_ME_IN); - expect(meglomaniac.henchWoman).toBe(FILL_ME_IN); + expect(meglomaniac.henchwoman).toBe("Harley"); + expect(meglomaniac.henchWoman).toBe(undefined); }); }); @@ -29,7 +29,7 @@ describe("About Objects", function () { }; var battleCry = meglomaniac.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(meglomaniac.calculateAge()).toBe(FILL_ME_IN); + expect(currentYear).toBe(2013); + expect(meglomaniac.calculateAge()).toBe(2013-1970); }); describe("'in' keyword", function () { @@ -62,27 +62,27 @@ describe("About Objects", function () { var hasBomb = "theBomb" in meglomaniac; - expect(hasBomb).toBe(FILL_ME_IN); + expect(hasBomb).toBe(true); }); it("should not have the detonator however", function () { var hasDetonator = "theDetonator" in meglomaniac; - expect(hasDetonator).toBe(FILL_ME_IN); + expect(hasDetonator).toBe(false); }); }); it("should know that properties can be added and deleted", function () { var meglomaniac = { mastermind : "Agent Smith", henchman: "Agent Smith" }; - expect("secretary" in meglomaniac).toBe(FILL_ME_IN); + expect("secretary" in meglomaniac).toBe(false); meglomaniac.secretary = "Agent Smith"; - expect("secretary" in meglomaniac).toBe(FILL_ME_IN); + expect("secretary" in meglomaniac).toBe(true); delete meglomaniac.henchman; - expect("henchman" in meglomaniac).toBe(FILL_ME_IN); + expect("henchman" in meglomaniac).toBe(false); }); @@ -96,14 +96,14 @@ describe("About Objects", function () { 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; }; - 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"); }); }); From 3493994b63eaf1d837d8ad5b67d381c0c894d472 Mon Sep 17 00:00:00 2001 From: quetzaluz Date: Thu, 16 May 2013 19:08:23 -0700 Subject: [PATCH 3/6] Progress on EC for javascript_koans. --- koans/AboutApplyingWhatWeHaveLearnt.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index e43eb746a..c98daba21 100644 --- a/koans/AboutApplyingWhatWeHaveLearnt.js +++ b/koans/AboutApplyingWhatWeHaveLearnt.js @@ -32,7 +32,7 @@ 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 () { @@ -40,8 +40,24 @@ describe("About Applying What We Have Learnt", function() { var productsICanEat = []; /* solve using filter() & all() / any() */ - - expect(productsICanEat.length).toBe(FILL_ME_IN); + + function filterBadIngredients () { + //takes variable args, but for this example it only filters for nuts and mushrooms. + for (var i = 0; i < products.length; i++) { + hasBadIngredient = false; + for (var j = 0; j < arguments.length; j++) { + if (arguments[j] === "nuts" && products[i].containsNuts === true) { + hasBadIngredient = true; + } else if (_(products[i]).filter(function (x){ return x === arguments[j]})) { + hasBadIngredient = true; + } + if (!hasBadIngredient) {productsICanEat.push(products[i])} + } + } + + hasBadIngredient("nuts", "mushrooms"); + expect(productsICanEat.length).toBe(1); + } }); /*********************************************************************************/ @@ -55,7 +71,7 @@ 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 () { From 34f38e30916cdc9ba992ac6787545d2130d8b73a Mon Sep 17 00:00:00 2001 From: quetzaluz Date: Thu, 16 May 2013 21:08:24 -0700 Subject: [PATCH 4/6] Range and reduce to sum factors. --- koans/AboutApplyingWhatWeHaveLearnt.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index c98daba21..48d19612c 100644 --- a/koans/AboutApplyingWhatWeHaveLearnt.js +++ b/koans/AboutApplyingWhatWeHaveLearnt.js @@ -75,10 +75,14 @@ describe("About Applying What We Have Learnt", function() { }); 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() */ - - expect(233168).toBe(FILL_ME_IN); + numbers = _.range(0, 1000) + var sum = _(numbers).chain() + .filter(function (x) {return x % 5 === 0 || x % 3 === 0}) + .reduce(function (memo, x) {return memo + x}, 0) + .value() + /* try chaining range() and reduce() */ + + expect(233168).toBe(sum); }); /*********************************************************************************/ From 23de63e50c1f37fb0d0af02ee4878578ce9db687 Mon Sep 17 00:00:00 2001 From: quetzaluz Date: Sat, 18 May 2013 16:53:33 -0700 Subject: [PATCH 5/6] Finally reached "Enlightenment," but EC still commented. --- koans/AboutApplyingWhatWeHaveLearnt.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index 48d19612c..af1c5abdc 100644 --- a/koans/AboutApplyingWhatWeHaveLearnt.js +++ b/koans/AboutApplyingWhatWeHaveLearnt.js @@ -95,15 +95,24 @@ 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 }; + var ingredientCount = /* chain() together map(), flatten() and reduce() */ - - expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN); + _(products).chain() + .map(function(name) { + console.log("Trying to return ingredient values only:" + name.ingredients); + return name.ingredients;}) + .flatten() + .reduce(function(ingredientCount, i) { + ingredientCount[i] = (ingredientCount[i] || 0) + 1; + console.log(ingredientCount); + return ingredientCount}, {}).value(); + + expect(ingredientCount['mushrooms']).toBe(2); }); /*********************************************************************************/ From 0f0c3a7a631d70846fae6472ae34fea011fe0663 Mon Sep 17 00:00:00 2001 From: quetzaluz Date: Tue, 21 May 2013 11:59:46 -0700 Subject: [PATCH 6/6] Commented in EC, will work on this more later. --- koans/AboutApplyingWhatWeHaveLearnt.js | 34 +++++++++++++++++++------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index af1c5abdc..e991789b2 100644 --- a/koans/AboutApplyingWhatWeHaveLearnt.js +++ b/koans/AboutApplyingWhatWeHaveLearnt.js @@ -75,8 +75,7 @@ describe("About Applying What We Have Learnt", function() { }); it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () { - numbers = _.range(0, 1000) - var sum = _(numbers).chain() + var sum = _(_.range(0, 1000)).chain() .filter(function (x) {return x % 5 === 0 || x % 3 === 0}) .reduce(function (memo, x) {return memo + x}, 0) .value() @@ -102,26 +101,43 @@ describe("About Applying What We Have Learnt", function() { var ingredientCount = /* chain() together map(), flatten() and reduce() */ - _(products).chain() - .map(function(name) { - console.log("Trying to return ingredient values only:" + name.ingredients); + _(products).chain() + .map(function(name) { return name.ingredients;}) .flatten() .reduce(function(ingredientCount, i) { ingredientCount[i] = (ingredientCount[i] || 0) + 1; - console.log(ingredientCount); return ingredientCount}, {}).value(); expect(ingredientCount['mushrooms']).toBe(2); }); - /*********************************************************************************/ +/*********************************************************************************/ /* UNCOMMENT FOR EXTRA CREDIT */ - /* + +/* Extra Credit 1 */ it("should find the largest prime factor of a composite number", function () { - + //Should factorize to 2^2x3x13 + var factorThis = _([156]).chain() + .reduce(function (factorThis, n) { + console.log("Trying to factor " + factorThis[n]); + var d = 2; //divisor for factoring + while (factorThis[n] > 1) { + while (factorThis[n]%d===0) { + factorThis.push(d); + factorThis[n] = factorThis[n] / d; + console.log("Factors added: " + factorThis[n] + ", " + d); + } + d = d + 1; + } + console.log(factorThis); + return factorThis; + }).value(); + expect(factorThis[-1]).toBe(13); }); + + /* it("should find the largest palindrome made from the product of two 3 digit numbers", function () { });