-Enigmos-

baggage.js

Nov 15th, 2021 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function baggage(input) {
  2.     let index = 0;
  3.     let freeSpace = Number(input[index]);
  4.     index++;
  5.     let command = input[index];
  6.     index++;
  7.     let luggage = 0;
  8.     let counter = 0;
  9.     let enoughtSpaceLeft = true;
  10.  
  11.     while (command !== "End") {
  12.         let suitCaseSpace = Number(command);
  13.         luggage++;
  14.  
  15.         if (luggage % 3 === 0 && counter !== 0) {
  16.             suitCaseSpace = suitCaseSpace + (suitCaseSpace * 0.10);
  17.         }
  18.  
  19.         if (suitCaseSpace > freeSpace) {
  20.             enoughtSpaceLeft = false;
  21.             console.log("No more space!");
  22.             console.log(`Statistic: ${counter} suitcases loaded.`);
  23.             break;
  24.         }
  25.  
  26.         counter++;
  27.         freeSpace -= suitCaseSpace;
  28.  
  29.         command = input[index];
  30.         index++;
  31.     }
  32.  
  33.     if (enoughtSpaceLeft) {
  34.         console.log("Congratulations! All suitcases are loaded!");
  35.         console.log(`Statistic: ${counter} suitcases loaded.`);
  36.     }
  37. }
  38.  
  39. baggage(["550", "100", "252", "72", "End"]);
  40. baggage(["700.5", "180", "340.6", "126", "220"]);
  41. baggage(["1200.2", "260", "380.5", "125.6", "305", "End"]);
Add Comment
Please, Sign In to add comment