Liliana797979

the lift - fundamentals

Jun 17th, 2021
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function theLift(arr) {
  2.     let people = Number(arr[0]);
  3.     let wagons = arr[1].split(" ").map(Number);
  4.  
  5.     //пълним вагоните -> max 4:
  6.  
  7.     while (people !== 0 || isFree(wagons)) {
  8.  
  9.         wagons = wagons.map(w => {  //0 2 0 => 4 2 0 => 4 4 0
  10.             let n = 4 - w;
  11.             if (n > people) {
  12.                 n = people;
  13.             }
  14.             people -= n;
  15.             w += n;
  16.             return w;
  17.         });
  18.        
  19.     }
  20.     if (isFree(wagons)) {
  21.         console.log(`The lift has empty spots!`);
  22.     }
  23.     if (people > 0) {
  24.         console.log(`There isn't enough space! ${people} people in a queue!`);
  25.    }
  26.    console.log(wagons.join(" "));
  27.  
  28.    function isFree(array) {
  29.        let filtered = array.filter(x => x < 4);
  30.        return filtered.length > 0;
  31.    }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment