0

I am trying to create a shopping cart using vanilla javascript and OOJS concepts.

let inventory = [
    { item: "apples", price: 19.95, qty: 50 },
    { item: "oranges", price: 20.99, qty: 40 },
    { item: "pineapples", price: 40.0, 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) {
    // write code here.
    this.inventory = this.inventory.filter(function(el) {
      if (
        el.item == item &&
        el.price == price &&
        el.qty == qty
      ) return false;
      return true;
    });
  };

  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().

Problem how do I remove a particular item from the cart? That item could be first, last or somewhere in the middle of the array.

Nauman Tanwir
  • 782
  • 2
  • 8
  • 32
  • [Splice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) – Mark Mar 10 '19 at 07:39
  • @MarkMeyer My query is not exactly the same as the one you have mentioned. My question also has some other related problems as well which all will be neglected now since you have marked it as DUPLICATE. – Nauman Tanwir Mar 10 '19 at 07:51
  • 1
    I removed my vote, but this is one of the reasons why it is best to ask one specific question. The main question (as determined by the title) is clearly a dupe (many times over). – Mark Mar 10 '19 at 08:13
  • You are right. I will ask each query separately. – Nauman Tanwir Mar 10 '19 at 08:26

1 Answers1

2

Use filter to get the item from inventory and using splice remove that element from the inventory

let inventory = [
    { item: "apples", price: 19.95, qty: 50 },
    { item: "oranges", price: 20.99, qty: 40 },
    { item: "pineapples", price: 40.0, 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 });
    console.log(this.totalItems)
  };

  MyBasket.prototype.removeItems = function(item, price, qty) {
    // write code here.
    var 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('s',23,43);
cart.removeItems('lemons',10.12,100);
ellipsis
  • 11,498
  • 2
  • 13
  • 33