Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda