← Back to the exercise

Functions and Parameters

The answer to the exercise in Functions and Parameters.

describeShow only declared one parameter, so the kind and the genres were passed and then dropped. A default fills in "show" when no kind is given. A rest parameter — which has to come last — collects the genres. Adding an array to a string joins its items with commas.

function describeShow(title, kind = "show", ...genres) { // was: (title)
  if (genres.length === 0) {
    return title + " (" + kind + ")";
  }
  return title + " (" + kind + "), " + genres; // new
}

console.log(describeShow("Severance"));
console.log(describeShow("Andor", "series", "Drama", "War"));