I have this function in JS that should return 'not sold'/'sold to {name}'/'unknown ticket id' depending on the value/existence of the "ticketId" key in the "tickets" object, but it just returns 'unknown ticket id' for any 'ticketId' value regardless of whether it is actually null or has a value assigned to it in the 'tickets' object.
export function ticketStatus(tickets, ticketId) {
if (tickets["ticketId"] === null) {
return 'not sold';
} else if (tickets["ticketId"] === undefined) {
return 'unknown ticket id';
} else {
return ('sold to ' tickets["ticketId"]);
}
}
VSCode says that the "ticketId" parameter is declared but never read in the function. I'm guessing that's why the function keeps returning the undefined option. In that case, why is 'ticketId' not being read as it should?
CodePudding user response:
"ticketId"
is a string, not the variable as you intended.
You probably meant tickets[ticketId]
:
export function ticketStatus(tickets, ticketId) {
if (tickets[ticketId] === null) {
return "not sold";
} else if (tickets[ticketId] === undefined) {
return "unknown ticket id";
} else {
return "sold to " tickets[ticketId];
}
}