Advertisement
Guest User

Piccolo

a guest
Nov 22nd, 2018
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Here is the exercise:
  2.  
  3. "3. Piccolo
  4. Write function that:
  5. • Records a car number for every car that enters the  parking lot
  6. • Removes a car number when the car goes out
  7. • Input will be array of strings in format [direction, carNumber]
  8. Print the output with all car numbers which are in the parking lot sorted in ascending by number
  9.  
  10.  
  11. Examples
  12. Input   Output
  13. ['IN, CA2844AA',
  14. 'IN, CA1234TA',
  15. 'OUT, CA2844AA',
  16. 'IN, CA9999TT',
  17. 'IN, CA2866HI',
  18. 'OUT, CA1234TA',
  19. 'IN, CA2844AA',
  20. 'OUT, CA2866HI',
  21. 'IN, CA9876HH',
  22. 'IN, CA2822UU'] CA2822UU
  23. CA2844AA
  24. CA9876HH
  25. CA9999TT
  26. ['IN, CA2844AA',
  27. 'IN, CA1234TA',
  28. 'OUT, CA2844AA',
  29. 'OUT, CA1234TA']    Parking Lot is Empty"
  30.  
  31. and here is my solution:
  32.  
  33. function piccolo(arr) {
  34.     let cars = [];
  35.  
  36.     for (let carDetails of arr) {
  37.        let [direction, number] = carDetails.split(', ');
  38.        if (direction === 'IN') {
  39.            cars.push(number);
  40.        } else if (direction === 'OUT') {
  41.            if (cars.includes(number)) {
  42.             let index = cars.indexOf(number);
  43.             cars.splice(index, 1);
  44.            }
  45.        }
  46.     }
  47.  
  48.     if (cars.length > 0) {
  49.         let sorted = cars.sort((a, b) => a.localeCompare(b));
  50.         for (let carNum of sorted) {
  51.             console.log(carNum);
  52.         }
  53.     } else {
  54.         console.log('Parking Lot is Empty');
  55.     }
  56.    
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement