Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function lift(arr) {
- let peopleWaiting = Number(arr.shift());
- let currState = arr.shift().split(' ').map(Number);
- let maxCapacity = 4;
- let filledLift = [];
- for (let i = 0; i < currState.length; i++) {
- let curr = currState[i];
- if (curr === 0 && peopleWaiting >= maxCapacity) {
- curr += maxCapacity;
- peopleWaiting -= maxCapacity;
- filledLift.push(curr);
- } else if (curr !== 0 && peopleWaiting >= maxCapacity) {
- let notEmpty = maxCapacity - curr;
- let filled = curr + notEmpty;
- peopleWaiting -= notEmpty;
- filledLift.push(filled);
- } else if (curr === 0 && peopleWaiting < maxCapacity || curr !== 0 && peopleWaiting < maxCapacity) {
- if (peopleWaiting === 0) {
- break;
- }
- curr += peopleWaiting;
- peopleWaiting -= curr;
- filledLift.push(curr);
- }
- }
- let elCount = 0;
- for (let el of filledLift) {
- elCount++
- if (el === maxCapacity && elCount === filledLift.length) {
- console.log(`There isn't enough space! ${peopleWaiting} people in a queue!`);
- console.log(filledLift.join(' '));
- } else if (peopleWaiting === 0 && elCount === filledLift.length) {
- console.log(`The lift has empty spots!`);
- console.log(filledLift.join(' '));
- } else if (peopleWaiting === 0 && el === maxCapacity && elCount === filledLift.length) {
- console.log(filledLift.join(' '));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement