Advertisement
teofarov13

Untitled

Feb 14th, 2023
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function lift(arr) {
  2.  
  3.     let peopleWaiting = Number(arr.shift());
  4.     let currState = arr.shift().split(' ').map(Number);
  5.     let maxCapacity = 4;
  6.     let filledLift = [];
  7.     for (let i = 0; i < currState.length; i++) {
  8.         let curr = currState[i];
  9.  
  10.         if (curr === 0 && peopleWaiting >= maxCapacity) {
  11.             curr += maxCapacity;
  12.             peopleWaiting -= maxCapacity;
  13.             filledLift.push(curr);
  14.         } else if (curr !== 0 && peopleWaiting >= maxCapacity) {
  15.             let notEmpty = maxCapacity - curr;
  16.             let filled = curr + notEmpty;
  17.             peopleWaiting -= notEmpty;
  18.             filledLift.push(filled);
  19.         } else if (curr === 0 && peopleWaiting < maxCapacity || curr !== 0 && peopleWaiting < maxCapacity) {
  20.             if (peopleWaiting === 0) {
  21.                 break;
  22.             }
  23.             curr += peopleWaiting;
  24.             peopleWaiting -= curr;
  25.             filledLift.push(curr);
  26.            
  27.         }
  28.     }
  29.     let elCount = 0;
  30.     for (let el of filledLift) {
  31.         elCount++
  32.         if (el === maxCapacity && elCount === filledLift.length) {
  33.             console.log(`There isn't enough space! ${peopleWaiting} people in a queue!`);
  34.            console.log(filledLift.join(' '));
  35.        } else if (peopleWaiting === 0 && elCount === filledLift.length) {
  36.            console.log(`The lift has empty spots!`);
  37.            console.log(filledLift.join(' '));
  38.        } else if (peopleWaiting === 0 && el === maxCapacity && elCount === filledLift.length) {
  39.            console.log(filledLift.join(' '));
  40.        }
  41.    }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement