-1

How can I check if the keys from pizzaRecipe are in the walmart object and calculate the values so I can get the price of what it will cost to shop the pizzaRecipe ingredients in walmart.

var walmart = {
    cheese: 9,
    corn: 2,
    wine: 7,
    parsley: 1,
    coconut: 3,
    tomato: 4,
    potato: 6,
    tomato: 2
};

var pizzaRecipe = {
    cheese: 2,
    tomato: 4,
    parsley: 1
};

function costRecipeInStore(recipe, store) {};
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Iterate over the the keys of object `pizzaRecipe`, use the key to get the corresponding values in `walmart` and `pizzaRecipe`, multiply the values and add the result to a running total. [Iterate through object properties](https://stackoverflow.com/q/8312459/218196), [Dynamically access object property using variable](https://stackoverflow.com/q/4244896/218196), [Arithmetic operators - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators). If you have a specific problem with this please let us know. – Felix Kling Mar 11 '20 at 11:26

1 Answers1

-1

You can try this following:

function costRecipeInStore(recipe, store) {
     let cost = 0;
     for (let obj in recipe) {
         if (store.hasOwnProperty(obj)) {
             cost += recipe[obj] * store[obj]
         }
     }
     console.log(cost)
};
Abhishek Kulkarni
  • 1,747
  • 1
  • 6
  • 8