Advertisement
Spocoman

01. Dishwasher

Dec 28th, 2021 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. РЕШЕНИЕ С FOR:
  2.  
  3. function dishwasher(input) {
  4.     let soupMl = Number(input[0]) * 750;
  5.     let dishes = 0;
  6.     let pots = 0;
  7.  
  8.     for (let i = 1; i < input.length; i++) {
  9.         let command = input[i];
  10.         if (command === 'End') {
  11.             console.log('Detergent was enough!');
  12.             console.log(`${dishes} dishes and ${pots} pots were washed.`);
  13.             console.log(`Leftover detergent ${soupMl} ml.`);
  14.             break;
  15.         }
  16.  
  17.         let charge = Number(command);
  18.  
  19.         if (i % 3 !== 0) {
  20.             dishes += charge;
  21.             soupMl -= charge * 5;
  22.         } else {
  23.             pots += charge;
  24.             soupMl -= charge * 15;
  25.         }
  26.  
  27.         if (soupMl < 0) {
  28.             console.log(`Not enough detergent, ${Math.abs(soupMl)} ml. more necessary!`);
  29.             break;
  30.         }
  31.     }
  32. }
  33.  
  34. РЕШЕНИЕ С WHILE:
  35.  
  36. function dishwasher(input) {
  37.     let soupMl = Number(input[0]) * 750;
  38.     let dishes = 0;
  39.     let pots = 0;
  40.     let counter = 1;
  41.     let command;
  42.  
  43.     while ((command = input[counter++]) !== 'End') {
  44.         let charge = Number(command);
  45.  
  46.         if (counter % 3 !== 1) {
  47.             dishes += charge;
  48.             soupMl -= charge * 5;
  49.         } else {
  50.             pots += charge;
  51.             soupMl -= charge * 15;
  52.         }
  53.  
  54.         if (soupMl < 0) {
  55.             console.log(`Not enough detergent, ${Math.abs(soupMl)} ml. more necessary!`);
  56.             return;
  57.         }
  58.     }
  59.  
  60.         console.log('Detergent was enough!');
  61.         console.log(`${dishes} dishes and ${pots} pots were washed.`);
  62.         console.log(`Leftover detergent ${soupMl} ml.`);
  63. }
  64.  
  65. РЕШЕНИЕ С FOR И ТЕРНАРЕН ОПЕРАТОР ЛЕКО ТАРИКАТСКАТА:
  66.  
  67. function dishwasher(input) {
  68.     let soupMl = Number(input[0]) * 750;
  69.     let dishes = 0;
  70.     let pots = 0;
  71.  
  72.     for (let i = 1; i < input.length && soupMl >= 0 && input[i] !== 'End'; i++) {
  73.         i % 3 !== 0 ? dishes += Number(input[i]) : pots += Number(input[i]);
  74.         soupMl -= Number(input[i]) * (i % 3 !== 0 ? 5 : 15);
  75.     }
  76.  
  77.     console.log(soupMl < 0 ? `Not enough detergent, ${Math.abs(soupMl)} ml. more necessary!` :
  78.         `Detergent was enough!\n${dishes} dishes and ${pots} pots were washed.\nLeftover detergent ${soupMl} ml.`);
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement