Advertisement
ErolKZ

Untitled

Jun 18th, 2021
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1.  
  2. function solve(input) {
  3.  
  4.  
  5. let capacity = Number(input[0]);
  6.  
  7. let countLoaded = 0;
  8.  
  9. let result = 0;
  10.  
  11. let sum = 0;
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18. for (let i = 1; i < input.length + 1; i++) {
  19.  
  20.  
  21. if (input[i] !== 'End' && input[i] !== undefined) {
  22.  
  23. sum = +input[i];
  24. countLoaded += 1;
  25.  
  26. }
  27.  
  28.  
  29. result = countLoaded / 3;
  30.  
  31.  
  32. if (Number.isInteger(result) && i > 1) {
  33.  
  34. sum = sum + (+input[i] + (+input[i] * (10 / 100)));
  35.  
  36. }
  37.  
  38.  
  39. if (input[i] !== undefined) {
  40.  
  41. capacity = capacity - sum;
  42.  
  43. }
  44.  
  45.  
  46. if (input[i] === 'End' || capacity <= 0 && input[i] === input[input.length - 2] && input[input.length - 1] === 'End' || input[i] === undefined) {
  47.  
  48. console.log(`Congratulations! All suitcases are loaded!`)
  49. break;
  50.  
  51. } else if (capacity <= 0 && input[i] !== 'End' && i > 1) {
  52.  
  53. console.log(`No more space!`);
  54. break;
  55. }
  56.  
  57.  
  58.  
  59.  
  60. }
  61.  
  62. console.log(`Statistic: ${countLoaded} suitcases loaded.`);
  63.  
  64.  
  65. }
  66.  
  67.  
  68.  
  69. solve([
  70.  
  71. '1700.5',
  72. '180',
  73. '340.6',
  74. '126',
  75. '222'
  76.  
  77.  
  78. // '1500.2',
  79. // '260',
  80. // '380.5',
  81. // '125.6',
  82. // '305',
  83. // '700',
  84. // 'End'
  85.  
  86. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement