Reputation:
I am trying to create a shopping cart using vanilla javascript and OOJS concepts.
let inventory = [
{ item: "apples", price: 19, qty: 50 },
{ item: "oranges", price: 20, qty: 40 },
{ item: "pineapples", price: 40, qty: 60 },
{ item: "lemons", price: 10.12, qty: 100 }
];
function MyBasket(inventory) {
this.totalItems = [];
}
MyBasket.prototype.addItems = function(item, price, qty) {
this.totalItems.push({ item: item, price: price, qty: qty });
};
MyBasket.prototype.removeItems = function(item, price, qty) {
let a =inventory.filter(e=>e.item==item);
inventory.splice(inventory.indexOf(a),1);
console.log(inventory)
};
MyBasket.prototype.updateInventory = function() {
cart.totalItems.forEach(i => {
const item = inventory.find(o => o.item === i.item);
if (item) item.qty -= i.qty;
});
}
MyBasket.prototype.cartItems = function() {
return this.totalItems;
};
MyBasket.prototype.totalAmount = function() {
return this.totalItems.reduce((acc, item) => {
return acc + item.price * item.qty;
}, 0);
};
var cart = new MyBasket();
cart.addItems("apples", 19, 2);
cart.addItems("oranges", 20, 3);
cart.addItems("lemons", 5, 4);
cart.updateInventory();
console.log("updated inventory", inventory);
cart.removeItems('lemons',10.12,100);
console.log("cart items", cart.cartItems());
console.log("total Amount", cart.totalAmount());
cart.updateInventory();
console.log("updated inventory", inventory);
Above is the js code for the app. Later I will create the UI for it and use an eventListener
that will call my parent Class MyBasket().
I have an updateInventory()
which basically compares the cartItems to the inventory and then subtracts the corresponding quantity of that item from the inventory. Whenever there is any change in the inventory, this function should check and give the updated inventory. For example, after adding items to the cart or after removing items from the cart.
Problem is with the updateInventory()
: when I run updateInventory()
after adding items, it contains duplicated values. It should only contain the updated list.
Now, if I remove some items the cart, the inventory should be updated. now when I run the updateInventory()
, it again subtracts the quantity. this results in wrong inventory information.
Upvotes: 1
Views: 447
Reputation: 12152
Instead of array
make it a set
. While using reduce
and other array functions you can convert the set
to array
again using
Array.from()
let inventory = [
{ item: "apples", price: 19, qty: 50 },
{ item: "oranges", price: 20, qty: 40 },
{ item: "pineapples", price: 40, qty: 60 },
{ item: "lemons", price: 10.12, qty: 100 }
];
function MyBasket(inventory) {
this.totalItems = new Set();
}
MyBasket.prototype.addItems = function(item, price, qty) {
this.totalItems.add({ item: item, price: price, qty: qty });
};
MyBasket.prototype.removeItems = function(item, price, qty) {
let a =inventory.filter(e=>e.item==item);
inventory.splice(inventory.indexOf(a),1);
console.log(inventory)
};
MyBasket.prototype.updateInventory = function() {
this.totalItems=Array.from(cart.totalItems);
cart.totalItems.forEach(i => {
const item = inventory.find(o => o.item === i.item);
if (item) item.qty -= i.qty;
});
}
MyBasket.prototype.cartItems = function() {
return this.totalItems;
};
MyBasket.prototype.totalAmount = function() {
this.totalItems=Array.from(cart.totalItems);
return this.totalItems.reduce((acc, item) => {
return acc + item.price * item.qty;
}, 0);
};
var cart = new MyBasket();
cart.addItems("apples", 19, 2);
cart.addItems("oranges", 20, 3);
cart.addItems("lemons", 5, 4);
cart.updateInventory();
console.log("updated inventory", inventory);
cart.removeItems('lemons',10.12,100);
console.log("cart items", cart.cartItems());
console.log("total Amount", cart.totalAmount());
cart.updateInventory();
console.log("updated inventory", inventory);
Upvotes: 3