Adding the Same Product ID But Not Increasing Its Quantity

Viewed 285

When you add product with the same Product Id it must increase its quantity and not be able to add a new row. I've tried the comments. Please check it below the js script. But it is still not working.

let Cart = [];


function viewCart() {

  let tr = document.createElement('tr');
  for (cart of Cart) {
    tr.innerHTML = `<td>${ cart.id }</td>
                      <td>${ cart.desc }</td>
                      <td>${ cart.qty }</td>
                      <td>${ cart.price }</td>
                      <td>${ cart.qty * cart.price }</td>`;

  }
  cartsTable.appendChild(tr);
}

function AddtoCart(productid, description, quantity, price) {
  let inputs = {
    id: productid,
    desc: description,
    qty: quantity,
    price: price
  };
  Cart.push(inputs);
  viewCart()
}
<script src="script.js"></script>

<input type="button" value="Laptop" onclick="AddtoCart('132','Macbook Pro', 1, 100000)" />
<input type="button" value="Phone" onclick="AddtoCart('456','Iphone 5S', 2, 20000)" />
<input type="button" value="Camera" onclick="AddtoCart('789','Nikon 3D00', 1, 40000)" />

<table border="1|1" id="cartsTable">
  <tr>
    <th>Product ID</th>
    <th>Product Description</th>
    <th>Quantity</th>
    <th>Price</th>
    <th>Total</th>
  </tr>
</table>

1 Answers
Related