From 29ff35b65c6488c31efa8d159d7dfaf754525687 Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Wed, 17 Jan 2024 20:12:32 +0300 Subject: [PATCH 01/16] Fulfilled Array tasks. --- koans/AboutArrays.js | 60 ++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/koans/AboutArrays.js b/koans/AboutArrays.js index f9b33f8cc..cd93897f1 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,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]); }); }); From 1c431388205c67cf83691cfb432214628ea472cf Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Wed, 17 Jan 2024 20:13:10 +0300 Subject: [PATCH 02/16] Fulfilled Expects tasks. --- koans/AboutExpects.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/koans/AboutExpects.js b/koans/AboutExpects.js index 7d1a827cb..1edd1ff5b 100644 --- a/koans/AboutExpects.js +++ b/koans/AboutExpects.js @@ -4,12 +4,12 @@ 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(); @@ -17,7 +17,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. @@ -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. @@ -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); }); }); From 8789da55dfefc9ead4063981ec74a5ca89c2301a Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Wed, 17 Jan 2024 20:13:25 +0300 Subject: [PATCH 03/16] Fulfilled Function tasks. --- koans/AboutFunctions.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/koans/AboutFunctions.js b/koans/AboutFunctions.js index 569100806..b78545f0b 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 () { @@ -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 () { @@ -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 = []; @@ -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 () { @@ -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!"); }); }); From 0e4673c2011147eaaea4e455199ebe32e08a7828 Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Wed, 17 Jan 2024 20:13:35 +0300 Subject: [PATCH 04/16] Fulfilled Objects tasks. --- koans/AboutObjects.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/koans/AboutObjects.js b/koans/AboutObjects.js index 9f9ed93cc..ad2dcbc71 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(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); }); }); @@ -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 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(2024); + expect(megalomaniac.calculateAge()).toBe(54); }); describe("'in' keyword", function () { @@ -62,27 +62,27 @@ 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" }; - 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); }); @@ -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 a24c99ce65fc828ab6a69cb2343de0a57abe4ebe Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Wed, 17 Jan 2024 20:13:48 +0300 Subject: [PATCH 05/16] Fulfilled Mutability tasks. --- koans/AboutMutability.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/koans/AboutMutability.js b/koans/AboutMutability.js index fd9b69af2..e8b5a5307 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"); }); }); From 6d7cba79f7d1f44f38771351053e52ce3f6c9b8f Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Wed, 17 Jan 2024 20:14:02 +0300 Subject: [PATCH 06/16] Fulfilled Inheritance tasks. --- koans/AboutInheritance.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/koans/AboutInheritance.js b/koans/AboutInheritance.js index 5dba545b0..f8520989c 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"); }); }); @@ -67,19 +67,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"); }); }); From 1e2eea5ee42d5988d6d5ea3a9976fdd94d7e70c5 Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Wed, 17 Jan 2024 20:14:49 +0300 Subject: [PATCH 07/16] Fulfilled Underscore functions tasks. --- koans/AboutHigherOrderFunctions.js | 36 +++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/koans/AboutHigherOrderFunctions.js b/koans/AboutHigherOrderFunctions.js index 07c416792..3c0800344 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 () { @@ -29,8 +29,8 @@ describe("About Higher Order Functions", function () { 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 () { @@ -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 () { @@ -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 () { @@ -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() { @@ -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); }); }); From 1feb9e085910ef0b6282e8bd95d2b331a569d242 Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Fri, 19 Jan 2024 22:04:35 +0300 Subject: [PATCH 08/16] Applying What we have learnt + first extra task: find the largest prime factor of a composite number --- koans/AboutApplyingWhatWeHaveLearnt.js | 191 +++++++++++++++---------- 1 file changed, 114 insertions(+), 77 deletions(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index eccc93763..9ed65d7f2 100644 --- a/koans/AboutApplyingWhatWeHaveLearnt.js +++ b/koans/AboutApplyingWhatWeHaveLearnt.js @@ -1,113 +1,150 @@ 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} + ]; + }); + + /*********************************************************************************/ + + 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 products; + expect(productsICanEat.length).toBe(1); + }); - 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 } - ]; - }); + it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () { - /*********************************************************************************/ + var productsICanEat = []; - it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (imperative)", function () { + /* solve using filter() & all() / any() */ + productsICanEat = _(products).filter(function (product) { + return !product.containsNuts && !_(product.ingredients).any(function (ingredient) { + return ingredient === "mushrooms" + }); + }); - var i,j,hasMushrooms, productsICanEat = []; + expect(productsICanEat.length).toBe(1); + }); - 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); - }); + it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (imperative)", function () { - it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () { + var sum = 0; + for (var i = 1; i < 1000; i += 1) { + if (i % 3 === 0 || i % 5 === 0) { + sum += i; + } + } - var productsICanEat = []; + expect(sum).toBe(233168); + }); - /* solve using filter() & all() / any() */ + it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () { - expect(productsICanEat.length).toBe(FILL_ME_IN); - }); + var sum = _(_.range(1, 1000)) + .chain() + .filter(function (num) { + return num % 3 === 0 || num % 5 === 0; + }) + .reduce(function (sum, num) { + return sum + num; + }).value(); /* try chaining range() and reduce() */ - /*********************************************************************************/ + expect(233168).toBe(sum); + }); - it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (imperative)", function () { + /*********************************************************************************/ + it("should count the ingredient occurrence (imperative)", function () { + var ingredientCount = {"{ingredient name}": 0}; - var sum = 0; - for(var i=1; i<1000; i+=1) { - if (i % 3 === 0 || i % 5 === 0) { - sum += i; - } - } + 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(sum).toBe(FILL_ME_IN); - }); + expect(ingredientCount['mushrooms']).toBe(2); + }); - it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () { + it("should count the ingredient occurrence (functional)", function () { + var ingredientCount = {"{ingredient name}": 0}; - var sum = FILL_ME_IN; /* try chaining range() and reduce() */ + /* chain() together map(), flatten() and reduce() */ + _(products).chain() + .map(function (product) { return product.ingredients;}) + .flatten() + .reduce(function (memo, ingredient) { memo[`${ingredient}`] = (memo[`${ingredient}`] || 0) + 1; return memo}, ingredientCount) + .value(); - expect(233168).toBe(FILL_ME_IN); - }); + expect(ingredientCount['mushrooms']).toBe(2); + }); - /*********************************************************************************/ - 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; + it("should find the largest prime factor of a composite number", function () { + function isPrime(number) { + for(let i= number-1; i>1; i--){ + if(number%i===0) return false; + } + return true; } - } - - expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN); - }); - - 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); - }); + function biggestPrimeFactor(number) { + for(let i=number;i>1;i--){ + if(number%i===0 && isPrime(i)){ + return i; + } + } + return 1; + } - /*********************************************************************************/ - /* UNCOMMENT FOR EXTRA CREDIT */ - /* - it("should find the largest prime factor of a composite number", function () { + expect(biggestPrimeFactor(6857)).toBe(6857); + }); - }); - it("should find the largest palindrome made from the product of two 3 digit numbers", function () { + it("should find the largest palindrome made from the product of two 3 digit numbers", function () { + function isPoly() { - }); + } + }); - it("should find the smallest number divisible by each of the numbers 1 to 20", function () { + /*********************************************************************************/ + /* UNCOMMENT FOR EXTRA CREDIT */ + /* + it("should find the smallest number divisible by each of the numbers 1 to 20", function () { - }); + }); - it("should find the difference between the sum of the squares and the square of the sums", function () { + it("should find the difference between the sum of the squares and the square of the sums", function () { - }); + }); - it("should find the 10001st prime", function () { + it("should find the 10001st prime", function () { - }); - */ + }); + */ }); From 1e852142c11d47fd2caa685b70bcb39ba49006c5 Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Fri, 19 Jan 2024 22:20:22 +0300 Subject: [PATCH 09/16] Extra task 2: find the largest palindrome made from the product of two 3 digit numbers --- koans/AboutApplyingWhatWeHaveLearnt.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index 9ed65d7f2..7ca6210bf 100644 --- a/koans/AboutApplyingWhatWeHaveLearnt.js +++ b/koans/AboutApplyingWhatWeHaveLearnt.js @@ -126,9 +126,31 @@ describe("About Applying What We Have Learnt", function () { it("should find the largest palindrome made from the product of two 3 digit numbers", function () { - function isPoly() { + function isPoly(number) { + let strNumber = `${number}` + let coefficient = strNumber.length % 2 === 0 ? 0 : 1; + let strNumber1 = strNumber.slice(0, (strNumber.length - coefficient) / 2); + let strNumber2 = strNumber + .slice((strNumber.length + coefficient) / 2, strNumber.length) + .split("") + .reverse() + .join(""); + return strNumber1 === strNumber2; + } + function biggestPoly() { + let result = 1; + for(let i=999;i>99;i--){ + for(let j=i-1;j>99;j--){ + if(isPoly(i*j) && result<(i*j)){ + result = i*j; + } + } + } + return result; } + + expect((biggestPoly())).toBe(906609); }); /*********************************************************************************/ From a0c54ac33a6aba56c11c383454802237a7cf623b Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Mon, 22 Jan 2024 21:52:50 +0300 Subject: [PATCH 10/16] Extra task 3: find the smallest number divisible by each of the numbers 1 to 20 --- koans/AboutApplyingWhatWeHaveLearnt.js | 31 +++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index 7ca6210bf..348cdf34d 100644 --- a/koans/AboutApplyingWhatWeHaveLearnt.js +++ b/koans/AboutApplyingWhatWeHaveLearnt.js @@ -153,14 +153,39 @@ describe("About Applying What We Have Learnt", function () { expect((biggestPoly())).toBe(906609); }); - /*********************************************************************************/ - /* UNCOMMENT FOR EXTRA CREDIT */ - /* + it("should find the smallest number divisible by each of the numbers 1 to 20", function () { + function isDividedBy(rangeStart, rangeEnd, number) { + console.log(`number: ${number}`); + let result = true; + _(_.range(rangeStart, rangeEnd+1)).forEach(function (factor) { + if(number % factor !== 0) { + console.log(factor); + result = false; + } + }); + return result; + } + + function gcd(number1, number2) { + return number2 === 0 ? number1 : gcd(number2, number1%number2); + } + function lcm(number1, number2) { + return number1/gcd(number1, number2)*number2; + } + + let expectedNumber = 2; + for(let i=2;i<=20;i++){ + expectedNumber = lcm(expectedNumber, i); + } + expect(isDividedBy(1, 20, expectedNumber)).toBe(true); }); + /*********************************************************************************/ + /* UNCOMMENT FOR EXTRA CREDIT */ + /* it("should find the difference between the sum of the squares and the square of the sums", function () { }); From cf47c27eb4bd07519be0e8a8d3220a1de2dadded Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Tue, 23 Jan 2024 22:38:52 +0300 Subject: [PATCH 11/16] Extra task 4 and 5 --- koans/AboutApplyingWhatWeHaveLearnt.js | 72 +++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 7 deletions(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index 348cdf34d..32a97b5db 100644 --- a/koans/AboutApplyingWhatWeHaveLearnt.js +++ b/koans/AboutApplyingWhatWeHaveLearnt.js @@ -155,13 +155,10 @@ describe("About Applying What We Have Learnt", function () { it("should find the smallest number divisible by each of the numbers 1 to 20", function () { - function isDividedBy(rangeStart, rangeEnd, number) { - console.log(`number: ${number}`); let result = true; _(_.range(rangeStart, rangeEnd+1)).forEach(function (factor) { if(number % factor !== 0) { - console.log(factor); result = false; } }); @@ -183,15 +180,76 @@ describe("About Applying What We Have Learnt", function () { expect(isDividedBy(1, 20, expectedNumber)).toBe(true); }); - /*********************************************************************************/ - /* UNCOMMENT FOR EXTRA CREDIT */ - /* + it("should find the difference between the sum of the squares and the square of the sums", function () { + function diff() { + let sumOfSquare = 0; + let squareOfSums = 0; + for(let i=0; i< arguments.length; i++){ + sumOfSquare += arguments[i]*arguments[i]; + squareOfSums += arguments[i]; + } + squareOfSums *= squareOfSums; + return squareOfSums-sumOfSquare; + } + + function mathDiff(n) { + return Math.abs((3*n*n + 2*n)*(1-n*n)/12); + } + expect(diff(1,2,3,4,5)).toBe(mathDiff(5)); }); it("should find the 10001st prime", function () { + function isPrime(number) { + for(let i= number-1; i>1; i--){ + if(number%i===0) return false; + } + return true; + } + function prime(limit) { + let result = []; + let number = 2; + while (result.length Date: Wed, 24 Jan 2024 14:21:51 +0300 Subject: [PATCH 12/16] Fix format of File AboutApplyingWhatWeHaveLearnt.js --- koans/AboutApplyingWhatWeHaveLearnt.js | 181 ++++++++++++++++++++++--- 1 file changed, 161 insertions(+), 20 deletions(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index eccc93763..62810b152 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,6 +40,11 @@ describe("About Applying What We Have Learnt", function() { var productsICanEat = []; /* solve using filter() & all() / any() */ + productsICanEat = _(products).filter(function (product) { + return !product.containsNuts && !_(product.ingredients).any(function (ingredient) { + return ingredient === "mushrooms" + }); + }); expect(productsICanEat.length).toBe(FILL_ME_IN); }); @@ -55,14 +60,21 @@ 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)) + .chain() + .filter(function (num) { + return num % 3 === 0 || num % 5 === 0; + }) + .reduce(function (sum, num) { + return sum + num; + }).value(); /* try chaining range() and reduce() */ - expect(233168).toBe(FILL_ME_IN); + expect(233168).toBe(sum); }); /*********************************************************************************/ @@ -75,39 +87,168 @@ 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() */ + _(products).chain() + .map(function (product) { return product.ingredients;}) + .flatten() + .reduce(function (memo, ingredient) { memo[`${ingredient}`] = (memo[`${ingredient}`] || 0) + 1; return memo}, ingredientCount) + .value(); - expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN); + expect(ingredientCount['mushrooms']).toBe(2); }); - /*********************************************************************************/ - /* UNCOMMENT FOR EXTRA CREDIT */ - /* - it("should find the largest prime factor of a composite number", function () { + it("should find the largest prime factor of a composite number", function () { + function isPrime(number) { + for(let i= number-1; i>1; i--){ + if(number%i===0) return false; + } + return true; + } - }); + function biggestPrimeFactor(number) { + for(let i=number;i>1;i--){ + if(number%i===0 && isPrime(i)){ + return i; + } + } + return 1; + } - it("should find the largest palindrome made from the product of two 3 digit numbers", function () { + expect(biggestPrimeFactor(6857)).toBe(6857); + }); + + + it("should find the largest palindrome made from the product of two 3 digit numbers", function () { + function isPoly(number) { + let strNumber = `${number}` + let coefficient = strNumber.length % 2 === 0 ? 0 : 1; + let strNumber1 = strNumber.slice(0, (strNumber.length - coefficient) / 2); + let strNumber2 = strNumber + .slice((strNumber.length + coefficient) / 2, strNumber.length) + .split("") + .reverse() + .join(""); + return strNumber1 === strNumber2; + } - }); + function biggestPoly() { + let result = 1; + for(let i=999;i>99;i--){ + for(let j=i-1;j>99;j--){ + if(isPoly(i*j) && result<(i*j)){ + result = i*j; + } + } + } + return result; + } - it("should find the smallest number divisible by each of the numbers 1 to 20", function () { + expect((biggestPoly())).toBe(906609); + }); - }); + it("should find the smallest number divisible by each of the numbers 1 to 20", function () { + function isDividedBy(rangeStart, rangeEnd, number) { + let result = true; + _(_.range(rangeStart, rangeEnd+1)).forEach(function (factor) { + if(number % factor !== 0) { + result = false; + } + }); + return result; + } - it("should find the difference between the sum of the squares and the square of the sums", function () { + function gcd(number1, number2) { + return number2 === 0 ? number1 : gcd(number2, number1%number2); + } + function lcm(number1, number2) { + return number1/gcd(number1, number2)*number2; + } - }); + let expectedNumber = 2; + for(let i=2;i<=20;i++){ + expectedNumber = lcm(expectedNumber, i); + } - it("should find the 10001st prime", function () { + expect(isDividedBy(1, 20, expectedNumber)).toBe(true); + }); - }); - */ + + it("should find the difference between the sum of the squares and the square of the sums", function () { + function diff() { + let sumOfSquare = 0; + let squareOfSums = 0; + for(let i=0; i< arguments.length; i++){ + sumOfSquare += arguments[i]*arguments[i]; + squareOfSums += arguments[i]; + } + squareOfSums *= squareOfSums; + return squareOfSums-sumOfSquare; + } + + function mathDiff(n) { + return Math.abs((3*n*n + 2*n)*(1-n*n)/12); + } + + expect(diff(1,2,3,4,5)).toBe(mathDiff(5)); + }); + + it("should find the 10001st prime", function () { + function isPrime(number) { + for(let i= number-1; i>1; i--){ + if(number%i===0) return false; + } + return true; + } + function prime(limit) { + let result = []; + let number = 2; + while (result.length Date: Wed, 24 Jan 2024 14:23:16 +0300 Subject: [PATCH 13/16] Fix format of File AboutApplyingWhatWeHaveLearnt.js --- koans/AboutApplyingWhatWeHaveLearnt.js | 150 ++++++++++++------------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index 62810b152..fecc4c1fa 100644 --- a/koans/AboutApplyingWhatWeHaveLearnt.js +++ b/koans/AboutApplyingWhatWeHaveLearnt.js @@ -2,106 +2,106 @@ var _; //globals 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 } - ]; - }); + 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 } + ]; + }); - /*********************************************************************************/ + /*********************************************************************************/ - it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (imperative)", 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 = []; + 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; - } + 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]); } - if (!hasMushrooms) productsICanEat.push(products[i]); } - } - expect(productsICanEat.length).toBe(1); - }); + 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 () { + 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(function (product) { - return !product.containsNuts && !_(product.ingredients).any(function (ingredient) { - return ingredient === "mushrooms" - }); - }); + /* solve using filter() & all() / any() */ + productsICanEat = _(products).filter(function (product) { + return !product.containsNuts && !_(product.ingredients).any(function (ingredient) { + return ingredient === "mushrooms" + }); + }); - expect(productsICanEat.length).toBe(FILL_ME_IN); - }); + expect(productsICanEat.length).toBe(FILL_ME_IN); + }); - /*********************************************************************************/ + /*********************************************************************************/ - it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (imperative)", 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) { - if (i % 3 === 0 || i % 5 === 0) { - sum += i; - } - } + var sum = 0; + for(var i=1; i<1000; i+=1) { + if (i % 3 === 0 || i % 5 === 0) { + sum += i; + } + } - expect(sum).toBe(233168); - }); + expect(sum).toBe(233168); + }); - it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () { + it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () { - var sum = _(_.range(1, 1000)) - .chain() - .filter(function (num) { - return num % 3 === 0 || num % 5 === 0; - }) - .reduce(function (sum, num) { - return sum + num; - }).value(); /* try chaining range() and reduce() */ + var sum = _(_.range(1, 1000)) + .chain() + .filter(function (num) { + return num % 3 === 0 || num % 5 === 0; + }) + .reduce(function (sum, num) { + return sum + num; + }).value(); /* try chaining range() and reduce() */ - expect(233168).toBe(sum); - }); + expect(233168).toBe(sum); + }); - /*********************************************************************************/ - it("should count the ingredient occurrence (imperative)", function () { - var ingredientCount = { "{ingredient name}": 0 }; + /*********************************************************************************/ + 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(2); - }); + expect(ingredientCount['mushrooms']).toBe(2); + }); - it("should count the ingredient occurrence (functional)", function () { - var ingredientCount = { "{ingredient name}": 0 }; + it("should count the ingredient occurrence (functional)", function () { + var ingredientCount = { "{ingredient name}": 0 }; - /* chain() together map(), flatten() and reduce() */ - _(products).chain() - .map(function (product) { return product.ingredients;}) - .flatten() - .reduce(function (memo, ingredient) { memo[`${ingredient}`] = (memo[`${ingredient}`] || 0) + 1; return memo}, ingredientCount) - .value(); + /* chain() together map(), flatten() and reduce() */ + _(products).chain() + .map(function (product) { return product.ingredients;}) + .flatten() + .reduce(function (memo, ingredient) { memo[`${ingredient}`] = (memo[`${ingredient}`] || 0) + 1; return memo}, ingredientCount) + .value(); - expect(ingredientCount['mushrooms']).toBe(2); - }); + expect(ingredientCount['mushrooms']).toBe(2); + }); it("should find the largest prime factor of a composite number", function () { function isPrime(number) { From 0b3056bd33d0a58685de18be07afe909f317f819 Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Wed, 24 Jan 2024 14:28:20 +0300 Subject: [PATCH 14/16] Fix format of File AboutApplyingWhatWeHaveLearnt.js --- koans/AboutApplyingWhatWeHaveLearnt.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index fecc4c1fa..c18784bdb 100644 --- a/koans/AboutApplyingWhatWeHaveLearnt.js +++ b/koans/AboutApplyingWhatWeHaveLearnt.js @@ -46,7 +46,7 @@ describe("About Applying What We Have Learnt", function() { }); }); - expect(productsICanEat.length).toBe(FILL_ME_IN); + expect(productsICanEat.length).toBe(1); }); /*********************************************************************************/ From 76595a59c893e88e5f3f9b05fe0e311291b2bf71 Mon Sep 17 00:00:00 2001 From: Sergey Akkuratov Date: Wed, 24 Jan 2024 14:31:41 +0300 Subject: [PATCH 15/16] Revert changes of file AboutApplyingWhatWeHaveLearnt.js --- koans/AboutApplyingWhatWeHaveLearnt.js | 165 ++----------------------- 1 file changed, 12 insertions(+), 153 deletions(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index c18784bdb..848ec2a0a 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(1); + expect(productsICanEat.length).toBe(FILL_ME_IN); }); it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () { @@ -40,13 +40,8 @@ describe("About Applying What We Have Learnt", function() { var productsICanEat = []; /* solve using filter() & all() / any() */ - productsICanEat = _(products).filter(function (product) { - return !product.containsNuts && !_(product.ingredients).any(function (ingredient) { - return ingredient === "mushrooms" - }); - }); - expect(productsICanEat.length).toBe(1); + expect(productsICanEat.length).toBe(FILL_ME_IN); }); /*********************************************************************************/ @@ -60,21 +55,14 @@ describe("About Applying What We Have Learnt", function() { } } - expect(sum).toBe(233168); + expect(sum).toBe(FILL_ME_IN); }); it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () { - var sum = _(_.range(1, 1000)) - .chain() - .filter(function (num) { - return num % 3 === 0 || num % 5 === 0; - }) - .reduce(function (sum, num) { - return sum + num; - }).value(); /* try chaining range() and reduce() */ + var sum = FILL_ME_IN; /* try chaining range() and reduce() */ - expect(233168).toBe(sum); + expect(233168).toBe(FILL_ME_IN); }); /*********************************************************************************/ @@ -87,168 +75,39 @@ describe("About Applying What We Have Learnt", function() { } } - expect(ingredientCount['mushrooms']).toBe(2); + expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN); }); it("should count the ingredient occurrence (functional)", function () { var ingredientCount = { "{ingredient name}": 0 }; /* chain() together map(), flatten() and reduce() */ - _(products).chain() - .map(function (product) { return product.ingredients;}) - .flatten() - .reduce(function (memo, ingredient) { memo[`${ingredient}`] = (memo[`${ingredient}`] || 0) + 1; return memo}, ingredientCount) - .value(); - expect(ingredientCount['mushrooms']).toBe(2); + expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN); }); + /*********************************************************************************/ + /* UNCOMMENT FOR EXTRA CREDIT */ + /* it("should find the largest prime factor of a composite number", function () { - function isPrime(number) { - for(let i= number-1; i>1; i--){ - if(number%i===0) return false; - } - return true; - } - function biggestPrimeFactor(number) { - for(let i=number;i>1;i--){ - if(number%i===0 && isPrime(i)){ - return i; - } - } - return 1; - } - - expect(biggestPrimeFactor(6857)).toBe(6857); }); - it("should find the largest palindrome made from the product of two 3 digit numbers", function () { - function isPoly(number) { - let strNumber = `${number}` - let coefficient = strNumber.length % 2 === 0 ? 0 : 1; - let strNumber1 = strNumber.slice(0, (strNumber.length - coefficient) / 2); - let strNumber2 = strNumber - .slice((strNumber.length + coefficient) / 2, strNumber.length) - .split("") - .reverse() - .join(""); - return strNumber1 === strNumber2; - } - - function biggestPoly() { - let result = 1; - for(let i=999;i>99;i--){ - for(let j=i-1;j>99;j--){ - if(isPoly(i*j) && result<(i*j)){ - result = i*j; - } - } - } - return result; - } - expect((biggestPoly())).toBe(906609); }); - it("should find the smallest number divisible by each of the numbers 1 to 20", function () { - function isDividedBy(rangeStart, rangeEnd, number) { - let result = true; - _(_.range(rangeStart, rangeEnd+1)).forEach(function (factor) { - if(number % factor !== 0) { - result = false; - } - }); - return result; - } - function gcd(number1, number2) { - return number2 === 0 ? number1 : gcd(number2, number1%number2); - } - function lcm(number1, number2) { - return number1/gcd(number1, number2)*number2; - } - let expectedNumber = 2; - for(let i=2;i<=20;i++){ - expectedNumber = lcm(expectedNumber, i); - } - - expect(isDividedBy(1, 20, expectedNumber)).toBe(true); }); - it("should find the difference between the sum of the squares and the square of the sums", function () { - function diff() { - let sumOfSquare = 0; - let squareOfSums = 0; - for(let i=0; i< arguments.length; i++){ - sumOfSquare += arguments[i]*arguments[i]; - squareOfSums += arguments[i]; - } - squareOfSums *= squareOfSums; - return squareOfSums-sumOfSquare; - } - - function mathDiff(n) { - return Math.abs((3*n*n + 2*n)*(1-n*n)/12); - } - expect(diff(1,2,3,4,5)).toBe(mathDiff(5)); }); it("should find the 10001st prime", function () { - function isPrime(number) { - for(let i= number-1; i>1; i--){ - if(number%i===0) return false; - } - return true; - } - function prime(limit) { - let result = []; - let number = 2; - while (result.length Date: Wed, 24 Jan 2024 14:41:35 +0300 Subject: [PATCH 16/16] Reformat code from original Repo. --- koans/AboutApplyingWhatWeHaveLearnt.js | 314 ++++++++++++++++++------- 1 file changed, 227 insertions(+), 87 deletions(-) diff --git a/koans/AboutApplyingWhatWeHaveLearnt.js b/koans/AboutApplyingWhatWeHaveLearnt.js index 848ec2a0a..594f7e304 100644 --- a/koans/AboutApplyingWhatWeHaveLearnt.js +++ b/koans/AboutApplyingWhatWeHaveLearnt.js @@ -2,112 +2,252 @@ var _; //globals 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 } - ]; - }); - - /*********************************************************************************/ - - 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]); - } - } - - expect(productsICanEat.length).toBe(FILL_ME_IN); - }); - - it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () { + var products; - var productsICanEat = []; + 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 } + ]; + }); - /* solve using filter() & all() / any() */ + /*********************************************************************************/ - expect(productsICanEat.length).toBe(FILL_ME_IN); - }); + it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (imperative)", 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) { - if (i % 3 === 0 || i % 5 === 0) { - sum += i; - } - } + var i,j,hasMushrooms, productsICanEat = []; - expect(sum).toBe(FILL_ME_IN); - }); - - 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); - }); - - /*********************************************************************************/ - it("should count the ingredient occurrence (imperative)", function () { - var ingredientCount = { "{ingredient name}": 0 }; - - for (i = 0; i < products.length; i+=1) { + 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) { - ingredientCount[products[i].ingredients[j]] = (ingredientCount[products[i].ingredients[j]] || 0) + 1; + if (products[i].ingredients[j] === "mushrooms") { + hasMushrooms = true; + } } + if (!hasMushrooms) productsICanEat.push(products[i]); } + } - expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN); - }); - - it("should count the ingredient occurrence (functional)", function () { - var ingredientCount = { "{ingredient name}": 0 }; + expect(productsICanEat.length).toBe(1); + }); - /* chain() together map(), flatten() and reduce() */ + it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () { - expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN); - }); + var productsICanEat = []; - /*********************************************************************************/ - /* UNCOMMENT FOR EXTRA CREDIT */ - /* - it("should find the largest prime factor of a composite number", function () { + /* solve using filter() & all() / any() */ + productsICanEat = _(products).filter(function (product) { + return !product.containsNuts && !_(product.ingredients).any(function (ingredient) { + return ingredient === "mushrooms" + }); + }); - }); + expect(productsICanEat.length).toBe(1); + }); - it("should find the largest palindrome made from the product of two 3 digit numbers", function () { + /*********************************************************************************/ - }); + it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (imperative)", function () { - it("should find the smallest number divisible by each of the numbers 1 to 20", function () { + var sum = 0; + for(var i=1; i<1000; i+=1) { + if (i % 3 === 0 || i % 5 === 0) { + sum += i; + } + } + expect(sum).toBe(233168); + }); - }); + it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () { - it("should find the difference between the sum of the squares and the square of the sums", function () { + var sum = _(_.range(1, 1000)) + .chain() + .filter(function (num) { + return num % 3 === 0 || num % 5 === 0; + }) + .reduce(function (sum, num) { + return sum + num; + }).value(); /* try chaining range() and reduce() */ - }); + expect(233168).toBe(sum); + }); - it("should find the 10001st prime", function () { + /*********************************************************************************/ + it("should count the ingredient occurrence (imperative)", function () { + var ingredientCount = { "{ingredient name}": 0 }; - }); - */ -}); \ No newline at end of file + 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(2); + }); + + it("should count the ingredient occurrence (functional)", function () { + var ingredientCount = { "{ingredient name}": 0 }; + + /* chain() together map(), flatten() and reduce() */ + _(products).chain() + .map(function (product) { return product.ingredients;}) + .flatten() + .reduce(function (memo, ingredient) { memo[`${ingredient}`] = (memo[`${ingredient}`] || 0) + 1; return memo}, + ingredientCount) + .value(); + + expect(ingredientCount['mushrooms']).toBe(2); + }); + + it("should find the largest prime factor of a composite number", function () { + function isPrime(number) { + for(let i= number-1; i>1; i--){ + if(number%i===0) return false; + } + return true; + } + + function biggestPrimeFactor(number) { + for(let i=number;i>1;i--){ + if(number%i===0 && isPrime(i)){ + return i; + } + } + return 1; + } + + expect(biggestPrimeFactor(6857)).toBe(6857); + }); + + it("should find the largest palindrome made from the product of two 3 digit numbers", function () { + function isPoly(number) { + let strNumber = `${number}` + let coefficient = strNumber.length % 2 === 0 ? 0 : 1; + let strNumber1 = strNumber.slice(0, (strNumber.length - coefficient) / 2); + let strNumber2 = strNumber + .slice((strNumber.length + coefficient) / 2, strNumber.length) + .split("") + .reverse() + .join(""); + return strNumber1 === strNumber2; + } + + function biggestPoly() { + let result = 1; + for(let i=999;i>99;i--){ + for(let j=i-1;j>99;j--){ + if(isPoly(i*j) && result<(i*j)){ + result = i*j; + } + } + } + return result; + } + + expect((biggestPoly())).toBe(906609); + }); + + it("should find the smallest number divisible by each of the numbers 1 to 20", function () { + function isDividedBy(rangeStart, rangeEnd, number) { + let result = true; + _(_.range(rangeStart, rangeEnd+1)).forEach(function (factor) { + if(number % factor !== 0) { + result = false; + } + }); + return result; + } + + function gcd(number1, number2) { + return number2 === 0 ? number1 : gcd(number2, number1%number2); + } + function lcm(number1, number2) { + return number1/gcd(number1, number2)*number2; + } + + let expectedNumber = 2; + for(let i=2;i<=20;i++){ + expectedNumber = lcm(expectedNumber, i); + } + + expect(isDividedBy(1, 20, expectedNumber)).toBe(true); + }); + + it("should find the difference between the sum of the squares and the square of the sums", function () { + function diff() { + let sumOfSquare = 0; + let squareOfSums = 0; + for(let i=0; i< arguments.length; i++){ + sumOfSquare += arguments[i]*arguments[i]; + squareOfSums += arguments[i]; + } + squareOfSums *= squareOfSums; + return squareOfSums-sumOfSquare; + } + + function mathDiff(n) { + return Math.abs((3*n*n + 2*n)*(1-n*n)/12); + } + + expect(diff(1,2,3,4,5)).toBe(mathDiff(5)); + }); + + it("should find the 10001st prime", function () { + function isPrime(number) { + for(let i= number-1; i>1; i--){ + if(number%i===0) return false; + } + return true; + } + function prime(limit) { + let result = []; + let number = 2; + while (result.length