Advertisement
Spocoman

07. Moving

Dec 28th, 2021 (edited)
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. РЕШЕНИЯ С WHILE:
  2.  
  3. function moving(input) {
  4.     let width = Number(input[0]);
  5.     let length = Number(input[1]);
  6.     let height = Number(input[2]);
  7.     let volume = width * length * height;
  8.     let index = 3;
  9.  
  10.     while (true) {
  11.         let box = input[index++];
  12.         if (box === "Done") {
  13.             console.log(`${volume} Cubic meters left.`);
  14.             break;
  15.         }
  16.         volume -= Number(box);
  17.         if (volume < 0) {
  18.             console.log(`No more free space! You need ${Math.abs(volume)} Cubic meters more.`);
  19.             break;
  20.         }
  21.     }
  22. }
  23.  
  24. Решение със shift():
  25.  
  26. function moving(input) {
  27.     let width = Number(input.shift());
  28.     let length = Number(input.shift());
  29.     let height = Number(input.shift());
  30.     let volume = width * length * height;
  31.  
  32.     while (true) {
  33.         let box = input.shift();
  34.         if (box === "Done") {
  35.             console.log(`${volume} Cubic meters left.`);
  36.             break;
  37.         }
  38.         volume -= Number(box);
  39.         if (volume < 0) {
  40.             console.log(`No more free space! You need ${Math.abs(volume)} Cubic meters more.`);
  41.             break;
  42.         }
  43.     }
  44. }
  45.  
  46. Решение с shift() и тернарен оператор леко тарикатската:)
  47.  
  48. function moving(input) {
  49.     let volume = Number(input.shift()) * Number(input.shift()) * Number(input.shift());
  50.  
  51.     while (input[0] !== "Done" && input.length !== 0) {
  52.         volume -= Number(input.shift());
  53.     }
  54.  
  55.     console.log(input[0] === "Done" ? `${volume} Cubic meters left.` :
  56.         `No more free space! You need ${Math.abs(volume)} Cubic meters more.`);
  57. }
  58.  
  59. РЕШЕНИЕ С FOR() ЛЕКО ТАРИКАТСКАТА:
  60.  
  61. function moving(input) {
  62.     let volume = Number(input[0]) * Number(input[1]) * Number(input[2]);
  63.  
  64.     for(let i = 3; i < input.length; i++) {
  65.         if (input[i] === "Done") {
  66.             console.log(`${volume} Cubic meters left.`);
  67.             break;
  68.         }
  69.         volume -= Number(input[i]);
  70.         if (volume < 0) {
  71.             console.log(`No more free space! You need ${Math.abs(volume)} Cubic meters more.`);
  72.             break;
  73.         }
  74.     }
  75. }
  76.  
  77. РЕШЕНИЕ БЕЗ ЦИКЪЛ:
  78.  
  79. function moving(input) {
  80.     let volume = Number(input.shift()) * Number(input.shift()) * Number(input.shift()) -
  81.         input.filter(a => a !== "Done").map(Number).reduce((a, b) => a + b, 0);
  82.  
  83.     console.log(volume >= 0 ? `${volume} Cubic meters left.` :
  84.         `No more free space! You need ${Math.abs(volume)} Cubic meters more.`);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement