TZinovieva

Computer Store JS Fundamentals

Feb 15th, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function computerStore(arr) {
  2.     let customerType = arr.pop();
  3.     let prices = arr.map(x => Number(x));
  4.  
  5.     let priceWithoutTaxes = 0;
  6.     let taxes = 0;
  7.     let totalItemPrice = 0;
  8.     let totalTaxes = 0;
  9.     let totalPrice = 0;
  10.     for (let i = 0; i < prices.length; i++) {
  11.         let price = prices[i];
  12.  
  13.         if (price < 0) {
  14.             console.log("Invalid price!");
  15.             continue;
  16.         }
  17.  
  18.         itemTaxes = price * 0.2;
  19.         totalItemPrice = price + itemTaxes;
  20.         totalTaxes += itemTaxes;
  21.         priceWithoutTaxes += price;
  22.         totalPrice += totalItemPrice;
  23.     }
  24.  
  25.     if (customerType === "special") {
  26.         totalPrice -= totalPrice * 0.1;
  27.     }
  28.  
  29.     if (totalPrice === 0) {
  30.         console.log("Invalid order!");
  31.     } else {
  32.         console.log("Congratulations you've just bought a new computer!");
  33.         console.log(`Price without taxes: ${priceWithoutTaxes.toFixed(2)}$`)
  34.         console.log(`Taxes: ${totalTaxes.toFixed(2)}$`);
  35.         console.log(`-----------`);
  36.         console.log(`Total price: ${totalPrice.toFixed(2)}$`);
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment