Reputation: 153
I am have a cart with items where each item, when clicked, will update the item count and price.
return {
itemCount: 0,
price: 0,
itemAdd: function () {
this.itemCount += 1;
if (this.itemCount <= 5) {
this.price = 500 + (110 * this.itemCount)
} else if (this.itemCount > 5, this.itemCount <= 10) {
this.price = 1000 + (105 * this.itemCount)
} else if (this.itemCount > 10) {
this.price = 1500 + (90 * this.itemCount)
}
document.getElementById("itemPrice").innerText = this.price;
document.getElementById("itemCount").innerText = this.itemCount;
},
}
}
When I click on one item twice, the price and itemCount is updated accordingly. However, when I click on a new item, the itemCount and price "reset" and start from zero. When I go back and click on the first item again, it continues counting from 2. What am I not getting right here?
Upvotes: 0
Views: 54
Reputation: 501
Now update to this..
let price = 0;
let itemCount = 0;
const getData = () => {
return {
items: [],
itemAdd: function() {
itemCount += 1;
if (itemCount <= 5) {
price = 500 + 110 * itemCount;
} else if ((itemCount > 5, itemCount <= 10)) {
price = 1000 + 105 * itemCount;
} else if (itemCount > 10) {
price = 1500 + 90 * itemCount;
}
document.getElementById("itemPrice").innerHTML = price;
document.getElementById("itemCount").innerHTML = itemCount;
}
};
};
Upvotes: 1