Trouble Accessing Value of Property

Hello,

I am trying to return the total cost of party supplies for an event, however the return I am getting is isNaN. Is the problem how I am accessing ‘supplies[j]’?

Regards :slight_smile:

const supplyList = { 
  plates: 2, 
  cups: 1,
  forks: 1,
  partyHats: 4 };

function orderSupplies(supplies, guests) {
  let totalCost = 0;
  for (let i = 0; i < guests; i++) {
    for (let j = 0; j < Object.keys(supplies).length; j++) {
      totalCost += supplies[j];
    }
  }
  return totalCost;
}

Hey! I have a question so I can understand a little better! Could you provide some more context around the supplyList and the number of supplies that you need per guest and if this changes at all? also could you let me know how much those individual supplies cost so we can consider this too? Many thanks, Tom

Hey there,

The cost of each item is the value to each items key (2 plates, 1 cup etc…).
I am simply trying to add the total cost of the SupplyList multiplied by the number of guests attending.

Cheers :slight_smile:

…Ah… I’m supposing that as object properties do not have indexes you cannot iterate over them?

Hey @leedsleedsleedsleeds
Try this code! feel free to refactor :slight_smile: If you have any q’s let me know!

const supplyList = { 
    plates: 2, 
    cups: 1,
    forks: 1,
    partyHats: 4 };
  
  let totalSupplyList = (supplyList.plates + supplyList.cups + supplyList.forks + supplyList.partyHats)
  console.log(totalSupplyList)

  function totalCost(total, guests){
    let totalCost = total * guests
        console.log(`The total cost is £${totalCost}`)
  }

  console.log(totalCost(totalSupplyList, 5))

Many thanks,
Tom

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.