Exercise: Associative Arrays, 3. Piccolo
Здравейте, къде е грешката в кода ми, за да ми дава 20/100?
Реших я с Map след това и стана 100/100.
Ще се радвам за обратен отговор от някого.
Link Judge: https://judge.softuni.org/Contests/Compete/Index/1306#2
Код 20/100:
function valet(carList) {
let parking = [];
for (const car of carList) {
let [action, carPlate] = car.split(', ');
if (action == 'IN') {
parking.push(carPlate);
} else if (action == 'OUT') {
parking.splice(parking.indexOf(carPlate), 1);
}
}
if (parking.length > 0) {
for (const car of parking.sort()) {
console.log(car);
}
} else {
console.log('Parking Lot is Empty');
}
}
Код с Map 100/100 за референция:
function valet(carList) {
let cars = new Map;
carList.forEach(car => {
let [action, carPlate] = car.split(', ');
if (action == 'IN') {
cars.set(carPlate, action);
} else if (action == 'OUT') {
cars.delete(carPlate);
}
});
let sortedCars = Array.from(cars).sort((a, b) => a[0].localeCompare(b[0]));
if (sortedCars.length == 0) {
console.log('Parking Lot is Empty');
} else {
sortedCars.forEach(car => {
console.log(car[0]);
});
}
}