Advertisement
PowerCell46

Computer Store JS

Mar 31st, 2023
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function computerStore(array) {
  2.     let index = 0;
  3.     let currentPartPrice = array[index];
  4.     let sum = 0;
  5.     let allTaxes = 0;
  6.     let allParts = 0;
  7.  
  8.     while (currentPartPrice !== "special" && currentPartPrice !== "regular") {
  9.         currentPartPrice = Number(array[index]);
  10.         if (currentPartPrice < 0) {
  11.             console.log("Invalid price!");
  12.             index++;
  13.             currentPartPrice = (array[index]);
  14.             continue;
  15.         }
  16.  
  17.         let taxesForThePart = (currentPartPrice / 100) * 20;
  18.         allTaxes += taxesForThePart;
  19.         allParts += currentPartPrice;
  20.  
  21.         let finalPriceForThePart = currentPartPrice + taxesForThePart;
  22.         sum += finalPriceForThePart;
  23.        
  24.         index++;
  25.         currentPartPrice = array[index];
  26.     }
  27.  
  28.     switch (currentPartPrice) {
  29.         case "regular":
  30.             sum = sum; break;
  31.         case "special":
  32.             sum = sum - sum / 10; break;
  33.     }
  34.  
  35.     if (sum > 0) {
  36.         console.log("Congratulations you've just bought a new computer!");
  37.         console.log(`Price without taxes: ${allParts.toFixed(2)}$`);
  38.         console.log(`Taxes: ${allTaxes.toFixed(2)}$`);
  39.         console.log("-----------");
  40.         console.log(`Total price: ${sum.toFixed(2)}$`);
  41.     } else if (sum === 0) {
  42.         console.log("Invalid order!");
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement