Alright, so I'm working on the final problem of javascript-koans. The code and dataset I'm given are as follows:
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("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();
});
I think I understand some of what's going on: We're iterating through the products array to iterate through the ingredients array of each product, taking an ingredient, and using bracket notation to call it as a property from the ingredientCount object. But around here is where I'm losing it, because then we set it equal to itself or zero, then add one regardless. Can someone correct me on what I have wrong there and explain what I'm missing? How/where does calling the ingredientCount variable with 'mushrooms' in bracket notation establish 'mushrooms' in this expression? And how are we incrementing the ingredientCount's {ingredient name} property without explicitly referencing it? Is there some kind of implicit assignment or something going on?
Also, the test runner returns an error letting me know the expected result should be 2.