krasizorbov

Car Wash

May 13th, 2020
1,445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function carWash(array = []) {
  2.   let sum = 0;
  3.   function addNumber(n) {
  4.     sum += n;
  5.     return sum;
  6.   }
  7.  
  8.   function addPercentage(percent) {
  9.     let value = (sum * percent) / 100;
  10.     sum += value;
  11.     return sum;
  12.   }
  13.  
  14.   function removePercentage(percent) {
  15.     let value = (sum * percent) / 100;
  16.     sum -= value;
  17.     return sum;
  18.   }
  19.  
  20.   let result = array.slice();
  21.   for (let i = 0; i < result.length; i++) {
  22.     let command = result[i];
  23.     switch (command) {
  24.       case "soap":
  25.         addNumber(10);
  26.         break;
  27.       case "water":
  28.         addPercentage(20);
  29.         break;
  30.       case "vacuum cleaner":
  31.         addPercentage(25);
  32.         break;
  33.       case "mud":
  34.         removePercentage(10);
  35.         break;
  36.       default:
  37.         break;
  38.     }
  39.   }
  40.   console.log(`The car is ${sum.toFixed(2)}% clean.`);
  41. }
  42. carWash(["soap", "soap", "vacuum cleaner", "mud", "soap", "water"]);
Advertisement
Add Comment
Please, Sign In to add comment