-2

im writting a ShoppingCart class to add and remove items from the cart code is meant to add and remove items from the shopping cart.

class ShoppingCart{
  constructor(){
    this.total = 0;
    this.items = {};
  }

  addItems(itemName, quantity, price){
    this.itemName = itemName;
    this.quantity = quantity;
    this.price = price;
    this.cost = this.quantity * this.price;

    this.total += this.cost;

    this.items['itemName'] = this.itemName;
    this.items['quantity'] = this.quantity;
  }

  removeItems(itemName, quantity, price){
    this.itemName = itemName;
    this.quantity = quantity;
    this.price = price;
    this.cost = this.quantity * this.price;

    this.total -= this.cost;
    delete this.items['itemName', 'quantity'];
  }
}
Dolapo
  • 9
  • 4

1 Answers1

0

Basically you need how to add and remove list items.

follow below approach and let me know if it works for you.

<ul id="todo"></ul>
<button onclick="addItemTodo('test')">Add</button>

js

function addItemTodo(text) {
list = document.getElementById("todo");

item = document.createElement('li');
item.innerText = text;

trash = document.createElement('button');
trash.classList.add('btn1');
trash.addEventListener("click", removeActivity);

icon_trash = document.createElement('i');
icon_trash.classList.add('fas', 'fa-trash-alt', 'fa-2x');
item.appendChild(trash);
trash.appendChild(icon_trash);

list.appendChild(item); }
 function removeActivity() {
var listItems = document.getElementsByTagName("li");
for (var i = 0; i < listItems.length; i++) {
    listItems[i].onclick = function() {
        this.parentNode.removeChild(this);
    }
  }
  }

Live demo

shahid
  • 484
  • 3
  • 10