-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'];
  }
}
4
  • where are you stuck? Commented Jun 6, 2018 at 11:45
  • maybe this will help stackoverflow.com/questions/14234646/adding-elements-to-object/… Commented Jun 6, 2018 at 11:46
  • 1
    your code already looks broken. class variables are not declared Commented Jun 6, 2018 at 11:46
  • The question, as it stands, doesn't actually ask a question. Commented Jun 6, 2018 at 11:47

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.