Skip to content

Instantly share code, notes, and snippets.

@dlmanning
Created December 1, 2013 21:35
Show Gist options
  • Save dlmanning/7741148 to your computer and use it in GitHub Desktop.
Save dlmanning/7741148 to your computer and use it in GitHub Desktop.
Solutions to one of the javascript koans
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