Advertisement
Spocoman

Suitcases Load

Mar 15th, 2022 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function suitcaseLoad(input) {
  2.     let size = Number(input[0]);
  3.     let counter = 1;
  4.            
  5.     while (true) {
  6.         let suitcase = input[counter];
  7.  
  8.         if (suitcase == "End") {
  9.             console.log("Congratulations! All suitcases are loaded!");
  10.             break;
  11.         }
  12.                    
  13.         let box = Number(suitcase);
  14.                
  15.         if (counter % 3 == 0) {
  16.             box *= 1.1;
  17.         }
  18.                
  19.         if (box > size) {
  20.             console.log("No more space!");
  21.             break;
  22.         }                
  23.         size -= box;
  24.         counter++;
  25.     }
  26.     console.log(`Statistic: ${--counter} suitcases loaded.`);
  27. }
  28.  
  29. РЕШЕНИЕ С FOR И ТЕРНАРЕН ОПЕРАТОР ЛЕКО ТАРИКАТСКАТА:
  30.  
  31. function suitcaseLoad(input) {
  32.     let size = Number(input[0]);
  33.  
  34.     for (let i = 1; i < input.length; i++) {
  35.  
  36.         if (input[i] == "End") {
  37.             console.log(`Congratulations! All suitcases are loaded!\nStatistic: ${--i} suitcases loaded.`);
  38.             break;
  39.         }
  40.  
  41.         let box = Number(input[i]) * (i % 3 == 0 ? 1.1 : 1);
  42.  
  43.         if (box > size) {
  44.             console.log(`No more space!\nStatistic: ${--i} suitcases loaded.`);
  45.             break;
  46.         }
  47.         size -= box;
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement