Advertisement
Guest User

Train Station

a guest
Jul 14th, 2019
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calculate(capacity, passengersArr){
  2. let outputArr = [];
  3.  
  4. let startWagon = 0;
  5.  
  6. for (const wagon of passengersArr) {
  7.    
  8.         if (startWagon + wagon > capacity){
  9.            
  10.             startWagon += wagon - capacity
  11.             outputArr.push(capacity)
  12.         } else{
  13.             outputArr.push(wagon + startWagon);
  14.             startWagon = 0;
  15.         }
  16.      
  17.     }
  18.     console.log(outputArr)
  19.     if(startWagon <= 0){
  20.         console.log(`All passengers aboard`)
  21.     } else {
  22.         console.log(`Could not fit ${startWagon} passengers`)
  23.     }
  24.    
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement