← Back to the exercise

Working with Arrays

The answer to the exercise in Working with Arrays.

Three changes, one per line:

  • find hands back the whole show, so read .title from it to print a label.
  • indexOf compares with ===, and no title in titles is a show object. Search for the title itself.
  • titles[-1] looks for a property named "-1", which does not exist. at(-1) counts back from the end.
const shows = [
  { id: 1, title: "Severance", watched: true },
  { id: 2, title: "The Bear", watched: false },
  { id: 3, title: "Shogun", watched: false },
];

const titles = ["Severance", "The Bear", "Shogun"];

console.log("Next up:", shows.find((show) => !show.watched).title); // added .title
console.log("Position:", titles.indexOf(shows[1].title)); // was: shows[1]
console.log("Last:", titles.at(-1)); // was: titles[-1]