SKGreyHound

The Lift - Mid Exam

Jan 10th, 2021
1,764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function theLift(input) {
  2.  
  3.     let ppl = Number(input.shift());
  4.     let wagons = input.shift().split(' ');
  5.     let wagonSpaceCount = 0;
  6.  
  7.     for (let i = 0; i < wagons.length; i++) {
  8.  
  9.         let currWagon = Number(wagons[i]);
  10.         if (currWagon < 4) {
  11.             if (ppl >= 4) {
  12.  
  13.                 if (currWagon != 0) {
  14.                     ppl -= (4 - currWagon);
  15.                     currWagon = 4;
  16.                     wagons[i] = currWagon;
  17.                     emptySpots = false;
  18.                 } else {
  19.                     currWagon = 4;
  20.                     ppl -= currWagon;
  21.                     wagons[i] = currWagon;
  22.                 }
  23.  
  24.             } else {
  25.                 currWagon += ppl;
  26.                 ppl = 0;
  27.                 wagons[i] = currWagon;
  28.                 emptySpots = true;
  29.             }
  30.         }
  31.     }
  32.     if (ppl != 0) {
  33.         console.log(`There isn't enough space! ${ppl} people in a queue!`);
  34.        console.log(wagons.join(' '));
  35.    } else {
  36.        for (let wagon of wagons) {
  37.            
  38.            if (wagon === 4) {
  39.                wagonSpaceCount++;
  40.            }
  41.        }
  42.        if (wagonSpaceCount != wagons.length) {
  43.            console.log("The lift has empty spots!");
  44.            console.log(`${wagons.join(' ')}`)
  45.        } else {
  46.            console.log(`${wagons.join(' ')}`)
  47.        }
  48.    }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment