Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is this function not reading one of my parameters?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

"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];
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading