JAVASCRIPT   17

product.js

Guest on 18th May 2022 12:28:10 AM

  1.  
  2. // setting variables
  3. let cart;
  4. let total = 0
  5. let output = ''
  6. //if there is something in the cart, it will store the data
  7. if (JSON.parse(localStorage.getItem("cartkey")) !== null) {
  8.     cart = JSON.parse(localStorage.getItem("cartkey"));
  9.     //empty list
  10. } else {
  11.     cart = []
  12. }
  13.  
  14. //creating a function which adds the product and quantity to the cart
  15. function addToCart() {
  16.     alert("Order Sumbited")
  17.     Products = document.getElementById("product").value;
  18.     Quanity = document.getElementById("quantity").value;
  19.     //pushing products and quantity into the cart
  20.     cart.push([Products, Quanity]);
  21.  
  22.     localStorage.setItem('cartkey', JSON.stringify(cart))
  23.     showPrice();
  24. }
  25. //creating a function for the final price
  26. function showPrice() {
  27.  
  28.     // creating a for loop
  29.     for (let i = 0; i < cart.length; i++) {
  30.         //the  product times the quantity to get the total cost without tax
  31.         total += parseInt(cart[i][0]) * parseInt(cart[i][1])
  32.  
  33.     }
  34.     // getting the tax that needs to be added by multiplying the total price by the tax rate
  35.     let tax = total * 0.13
  36.     //getting actual total price by adding the tax
  37.     let trueTotal = total + tax
  38.  
  39.     output = `<br> <h2 style="font-size: large"> Total Price without tax: $${total.toFixed(2)} </h2> <br> <h2 style="font-size: large"> Price with tax: $${trueTotal.toFixed(2)} </h2>`
  40.     document.getElementById('priceArea').innerHTML = output
  41.  
  42. }

Raw Paste


Login or Register to edit or fork this paste. It's free.