daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Feb 23rd, 2019 76 in 2 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function train (wagons){
  2.     let passengersInWagons = wagons
  3.     .shift()
  4.     .split(' ')
  5.     .map(Number);
  6.    
  7.     let maxWagonCapacity = Number(wagons.shift());
  8.    
  9.     for(let commands of wagons){
  10.         let currentCommand =commands.split(' ');
  11.  
  12.         if (currentCommand[0]==='Add'){
  13.         passengersInWagons.push(+currentCommand[1]);
  14.         }else{
  15.             for (let i = 0; i < passengersInWagons.length; i++) {
  16.                 if (passengersInWagons[i] + Number(currentCommand[0] <= maxWagonCapacity)){
  17.                     passengersInWagons[i] += Number(currentCommand[0]);
  18.                   break;
  19.                 }
  20.             }
  21.         }      
  22.     }
  23.         console.log(passengersInWagons.join(' '));
  24.        
  25.  
  26. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top