codingforeveryone/js

new-member-34: Calculate cost of basket

Opened this issue · 4 comments

Calculate cost of basket

Write a function, priceOfBasket, that returns the total cost of a basket of items.

Input will be:

  • an object containing keys (items available in the shop) and values (their prices): {banana: 3, orange: 4, pear: 5, apple: 2}
  • an array of strings representing the individual items bought: ['banana', 'orange', 'apple', 'banana', 'orange', 'banana']

Output should be:

  • an integer representing the total cost of the individual items bought: 19

I tried to work this out but I can't see to access my newBasket array to log the object prices:


var totalCost = 0;
// an object containing keys (items available in the shop) and values (their prices):
// {banana: 3, orange: 4, pear: 5, apple: 2}
var itemPrices = {
banana: 3,
orange: 4,
pear: 5,
apple: 2
};
// an array of strings representing the individual items bought:
// ['banana', 'orange', 'apple', 'banana', 'orange', 'banana']
var newBasket = ['banana', 'orange', 'apple', 'banana', 'orange', 'banana'];

for(var i in itemPrices){
itemPrices.newBasket[i] += totalCost;
}

// Write a function, priceOfBasket, that returns the total cost of a basket of items.
// Output: an integer representing the total cost of the individual items bought: 19
var priceofBasket = function(Basket) {
return totalCost;
}


Hi Seyi - sorry for the slow reply! I haven't been receiving GitHub notifications for some reason.

A few thoughts on your code:

  • When you're using a variable like newBasket[i] to retrieve a dictionary value, you need to use bracket notation rather than dot notation. So itemPrices[newBasket[i]] rather than itemPrices.newBasket[i].
  • Your code currently adds the total cost to the value in itemPrices - I suspect it should be the other way around.
  • Your code currently iterates over itemPrices - if you're trying to add a cost for each item in newBasket, you may want to iterate over that array instead.
  • Currently all your logic is outside the priceOfBasket function, and only works with your hard-coded values. Try writing the function so that any price list object and basket array can be passed in.

I hope that helps. Good luck!

Thank you, I've got it now. Do I submit the answer here?

var totalCost = 0;
var itemPrices = {
banana: 3,
orange: 4,
pear: 5,
apple: 2
};

var newBasket = ['banana', 'orange', 'apple', 'banana', 'orange', 'banana'];

var priceofBasket = function(Basket) {
for(var i in newBasket){
totalCost += itemPrices[newBasket[i]];
}

return totalCost;
}

priceOfBasket(newBasket);

Great work Seyi! Almost there: I'd suggest one more tweak.

Your function currently only takes Basket as an argument, but that argument is never used within the function (it's still referencing the global variable 'newBasket', declared outside the function, which is why it's still working).

Ideally your function should take two arguments (basket and itemPrices) and only work with those two arguments (and anything else you declare within the function) in its logic). So start with var priceOfBasket = function (basket, itemPrices) and work from there.

When you're happy with your solution, you'll need to create a new file and push it to the appropriate directory in the repo. You can see the directions in the readme: https://github.com/codingforeveryone/js/blob/master/README.md

Good luck!