Advertisement
Lulunga

Associative Arrays 03. Piccolo

Jul 9th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function piccolo(arr) {
  2.     let cars = [];
  3.  
  4.     for (let carDetails of arr) {
  5.         let [direction, number] = carDetails.split(', ');
  6.         if (direction === 'IN') {
  7.             if (!cars.includes(number)) {
  8.                 cars.push(number);
  9.             }
  10.         } else if (direction === 'OUT') {
  11.             if (cars.includes(number)) {
  12.                 let index = cars.indexOf(number);
  13.                 cars.splice(index, 1);
  14.             }
  15.         }
  16.     }
  17.  
  18.     if (cars.length > 0) {
  19.         let sorted = cars.sort((a, b) => a.localeCompare(b));
  20.         for (let carNum of sorted) {
  21.             console.log(carNum);
  22.         }
  23.     } else {
  24.         console.log('Parking Lot is Empty');
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement