Created
December 1, 2013 21:35
-
-
Save dlmanning/7741148 to your computer and use it in GitHub Desktop.
Solutions to one of the javascript koans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(1); | |
}); | |
it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () { | |
var productsICanEat = []; | |
/* solve using filter() & all() / any() */ | |
productsICanEat = _(products).chain() | |
.filter(function (item) { | |
return !item.containsNuts && !_(item.ingredients).any(function (ingredients) { | |
return ingredients === 'mushrooms'; | |
}); | |
}) | |
.value(); | |
expect(productsICanEat.length).toBe(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment