Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Can someone explain this for/in loop to me?
/*
  Write each function according to the instructions.
  

  When a function's parameters reference `cart`, it references an object that looks like the one that follows.
  {
    "Gold Round Sunglasses": { quantity: 1, priceInCents: 1000 },
    "Pink Bucket Hat": { quantity: 2, priceInCents: 1260 }
  }
*/

function calculateCartTotal(cart) {
  let total = 0;
  for (const item in cart){
    let quantity = Object.values(cart[item])[0];
    let price = Object.values(cart[item])[1];
        total += price * quantity;
  }
 return total; 
}

function printCartInventory(cart) {
  let inventory = "";
  for (const item in cart){
    inventory += `${Object.values(cart[item])[0]}x${item}\n`;
  }
  return inventory;
  
}

module.exports = {
  calculateCartTotal,
  printCartInventory,
};

The part that confuses me is the function calculateCartTotal. What I am confused about is how does this loop know to grab priceInCents? for example, if I was to add another value into the object called "weight: 24" assuming that it is 24 grams, how does the object value skip over quantity and weight and just grab priceInCents? Hopefully I am making sense on how I am confused and that someone has an explanation for me!

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

If you try to run below program then it will be easier for you to visualize everything.

What is happening is item is just the index of element and for an object we can either use the key name to access its value or its index.

You can read this doc to understand what Object.values() does.

function calculateCartTotal(cart) {
  let total = 0;
  for (const item in cart) {
    console.log(item)
    let quantity = Object.values(cart[item])[0];
    let price = Object.values(cart[item])[1];
        total += price * quantity;
  }
 return total; 
}

var cart = [
    {
        quantity: 2,
        price: 5,
        weight: 24
    },
    {
        quantity: 3,
        price: 10,
        weight: 90
    },
    {
        quantity: 7,
        price: 20,
        weight: 45
    },
    {
        quantity: 1,
        price: 100,
        weight: 67
    }
]

console.log(calculateCartTotal(cart))

OUTPUT:

0
1
2
3
280

Program 2 to demonstrate what is happening

function calculateCartTotal(cart) {
 console.log(Object.values(cart[2])[1])
 console.log(cart[2]['price'])
 console.log(cart[2].price)
}

var cart = [
    {
        quantity: 2,
        price: 5,
        weight: 24
    },
    {
        quantity: 3,
        price: 10,
        weight: 90
    },
    {
        quantity: 7,
        price: 20,
        weight: 45
    },
    {
        quantity: 1,
        price: 100,
        weight: 67
    }
]
calculateCartTotal(cart)

OUTPUT:

20
20
20
about 4 years ago · Santiago Gelvez Report

0

I am Deepak,🙂

I can explain (program 2 demonstration). See my friend,you will be able to see the first console.log i.e, console.log(Object.values(cart[2])[1])... So, object means the whole cart and .values means the only numbers that a particular object contained. Now, see the result i.e, 20. So, how this 20 will came?... Now, see the console.log that I have written before. In the brackets of .value cart of [2](it means that 2 is the position of that cart, that why it is written as cart[2] i.e, inside a cart 2nd position's object and after cart[2] this one number is there [1], it means inside the 2nd position's object i.e,

OBJECT below:- position of an objects var cart = [

    quantity: 2,           0 position
    price: 5,
    weight: 24
},
{
    quantity: 3,           1 position
    price: 10,
    weight: 90
},
{
    quantity: 7,           2 position
    price: 20,
    weight: 45
} ,
{
    quantity: 1,           3 position
    price: 100,
    weight: 67
}

]

console.log(calculateCartTotal(cart))

Now, match the console.log.
it says that console.log(OBJECT.values(cart[2])[1]);

In the cart, see the 2nd position's object i.e,

{ quantity: 7, 2 position price: 20, weight: 45 }

So, cart[2] means the whole object you will above. [1] means inside the object count the position from 0 onwards. So, in the

                          values

0 position quantity, 7,
1 position price, 20, 2 position weight. 45.

In the [1] position price: 20.

So, cart[2] means { quantity: 7, 2 position price: 20, weight: 45 }

And, [1] means price: 20.

So, your answer is 20.

Note: the numbers that is inside the square brackets will gives the position of an object or inside an object.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!