JAVASCRIPT
17
product.js
Guest on 18th May 2022 12:28:10 AM
// setting variables
let cart;
let total = 0
let output = ''
//if there is something in the cart, it will store the data
if (JSON.parse(localStorage.getItem("cartkey")) !== null) {
cart = JSON.parse(localStorage.getItem("cartkey"));
//empty list
} else {
cart = []
}
//creating a function which adds the product and quantity to the cart
function addToCart() {
alert("Order Sumbited")
Products = document.getElementById("product").value;
Quanity = document.getElementById("quantity").value;
//pushing products and quantity into the cart
cart.push([Products, Quanity]);
localStorage.setItem('cartkey', JSON.stringify(cart))
showPrice();
}
//creating a function for the final price
function showPrice() {
// creating a for loop
for (let i = 0; i < cart.length; i++) {
//the product times the quantity to get the total cost without tax
total += parseInt(cart[i][0]) * parseInt(cart[i][1])
}
// getting the tax that needs to be added by multiplying the total price by the tax rate
let tax = total * 0.13
//getting actual total price by adding the tax
let trueTotal = total + tax
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>`
document.getElementById('priceArea').innerHTML = output
}