Exercise: Associative Arrays, 4. Party Time
Здравейте, исках да се опитам да реша тази задача с обект и получих 60/100 на Judge.
Знам, че може да се реши по-лесно с масиви, направих го, и ми даде 100/100.
Ще се радвам да разбера къде греша в кода с обекти.
Junge Link : https://judge.softuni.org/Contests/Compete/Index/1306#3
Код 60/100 с обект:
function creatingPartyList(list) {
let reservations = {};
let member = list.shift();
while (member != 'PARTY') {
if (isNaN(Number(member[0]))) {
reservations[member] = 'Regular';
} else {
reservations[member] = 'VIP';
}
member = list.shift();
}
let guests = list.slice();
guests.forEach(guest => {
if (reservations[guest]) { delete reservations[guest]; }
});
let regularGuestsNotComing = [];
let vipGuestsNotComing = [];
let guestsNotCome = Object.entries(reservations);
guestsNotCome.forEach(guest => {
if (guest[1] == 'VIP') {
vipGuestsNotComing.push(guest[0]);
} else {
regularGuestsNotComing.push(guest[0]);
}
});
console.log(regularGuestsNotComing.length + vipGuestsNotComing.length);
vipGuestsNotComing.forEach(guest => { console.log(guest); });
regularGuestsNotComing.forEach(guest => { console.log(guest) });
}
Код 100/100 с обикновени масиви:
function creatingPartyList(input) {
let vips = [];
let regulars = [];
let person = input.shift();
while (person != 'PARTY') {
if (isNaN(Number(person[0]))) {
regulars.push(person);
} else {
vips.push(person);
}
person = input.shift();
}
for (const comingGuest of input) {
if (vips.includes(comingGuest)) {
vips.splice(vips.indexOf(comingGuest), 1);
}
if (regulars.includes(comingGuest)) {
regulars.splice(regulars.indexOf(comingGuest), 1);
}
}
console.log(vips.concat(regulars).length);
vips.concat(regulars).forEach(guest => {
console.log(guest);
});
}