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