0

var cart = [];
var Item = function(name, price, count) {
  this.name = name
  this.price = price
  this.count = count
};

function addItemtocart(name, price, count) {
  var item = new Item(name, price, count);
  for (var i in cart) {
    if (name == cart[i].name) {
      cart[i].count = cart[i].count + count;
      return;
    }
  }
  cart.push(item);
};

function remItemfromcart(name) {
  for (var i in cart) {
    if (name == cart[i].name) {
      cart[i].count = cart[i].count - 1;
      return;
    }
  }
};

addItemtocart('papaya', 2, 5);
console.log(cart);
console.log(cart[0].count);
remItemfromcart('papaya');

I got this output

[Item]0: Itemcount: 4name: "papaya"price: 2__proto__: Objectlength: 1__proto__: Array(0)

index.html:41 5

here the console log output is surprising to me. I called the remItemfromcart after calling the console log. Probably a dumb question - please help me here to understand this

Barmar
  • 741,623
  • 53
  • 500
  • 612
DJB
  • 1
  • 1
    The console is a live view of the object, not a snapshot. If you change the object before expanding the object in the console, you'll see the updated contents. – Barmar Jun 02 '17 at 21:47
  • can stringify object if you want to see a snapshot .. `console.log(JSON.stringify(cart, null, 4));` – charlietfl Jun 02 '17 at 21:50

0 Answers0