Assessment needed for MDN JSON active learning
Very well done. Everything works as expected. ![]()
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...elsewith their ownmotherInfo +=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
totalwith the length of the kitten array (lines 28 -29) you could also just addtotal++inside the inner loop above or below theifstatement. -
Simplification: Since we don’t use the index of the inner loop for any calculation, you could also use a
for...ofloop:
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;andpara2.textContent = kittenInfo;lines inside thedisplayCatInfo()function, and not at the end of the script? This has something to do with async code.
See you,
Michael