https://codepen.io/Goodybag01/pen/PoypoOY

Assessment needed for MDN JSON active learning

Hi @Goodness_David

Very well done. Everything works as expected. :clap:

Here are three remarks. You can look at them as alternatives or personal taste. Nothing is wrong about how you approached the task.

  • Readability: The ternary operator inside the template string expression feels a bit like too much. I would rather use a distinct variable to save it and then include the variable. Another (maybe better) approach would be to use an if...else if...else with their own motherInfo += parts. This would make it possible to distinguish all cases (“name with comma”, “name with and”, “just name”). You won’t need lines 43 - 47 and therefore everything is in the same block.

  • Simplification: Instead of calculating the total with the length of the kitten array (lines 28 -29) you could also just add total++ inside the inner loop above or below the if statement.

  • Simplification: Since we don’t use the index of the inner loop for any calculation, you could also use a for...of loop:

for (const kitten of obj[i].kittens) {
  total++;
  if (kitten.gender === "m") {
    male++;
  }
}

Additional question: Do you have an explanation for the last point in the task description?

Why are the para1.textContent = motherInfo; and para2.textContent = kittenInfo; lines inside the displayCatInfo() function, and not at the end of the script? This has something to do with async code.

See you,
Michael