Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 107 additions & 77 deletions koans/AboutApplyingWhatWeHaveLearnt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,142 @@ 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;
}
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]);
}
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 () {
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() */
/* solve using filter() & all() / any() */

expect(productsICanEat.length).toBe(FILL_ME_IN);
});
// 1) Решение с помошью filter() & all()
var productsWithoutNuts = products.filter(p => !p.containsNuts);

/*********************************************************************************/
productsWithoutNuts.forEach(pwn => {
if (_(pwn.ingredients).all(i => i !== "mushrooms")) {
productsICanEat.push(pwn);
}
})

// 2) Решение с помошью any()
// for (const p of products) {
// if (p.containsNuts || _(p.ingredients).any(i => i === "mushrooms")) {
// continue;
// }

// productsICanEat.push(p);
// }

it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (imperative)", function () {
expect(productsICanEat.length).toBe(1);
});

var sum = 0;
for(var i=1; i<1000; i+=1) {
if (i % 3 === 0 || i % 5 === 0) {
sum += i;
}
}
/*********************************************************************************/

expect(sum).toBe(FILL_ME_IN);
});
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;
}
}

it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () {
expect(sum).toBe(233168);
});

var sum = FILL_ME_IN; /* try chaining range() and reduce() */
it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function() {

expect(233168).toBe(FILL_ME_IN);
});
var sum = _.range(1000).reduce((sum, x) => (x % 3 === 0 || x % 5 === 0) ? sum + x : sum); /* try chaining range() and reduce() */

/*********************************************************************************/
it("should count the ingredient occurrence (imperative)", function () {
var ingredientCount = { "{ingredient name}": 0 };
expect(233168).toBe(sum);
});

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 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;
}
}
}

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 };

it("should count the ingredient occurrence (functional)", 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;
}
}

/* chain() together map(), flatten() and reduce() */
/* chain() together map(), flatten() and reduce() */
var mushroomsCount = _(products)
.chain()
.map(p => p.ingredients)
.flatten()
.reduce((sum, x) => x === 'mushrooms' ? sum + 1 : sum, 0)
.value();

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

/*********************************************************************************/
/* UNCOMMENT FOR EXTRA CREDIT */
/*
it("should find the largest prime factor of a composite number", function () {
/*********************************************************************************/
/* UNCOMMENT FOR EXTRA CREDIT */
/*
it("should find the largest prime factor of a composite number", function () {

});
});

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 () {

});
});

it("should find the smallest number divisible by each of the numbers 1 to 20", function () {
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 () {

});
*/
});
});
*/
});
Loading