Assessment wanted for JSON skill test- the only one

Hello, please I’ll like for you to assess my submission, your tips and suggestions will help me a lot.
Thanks for helping me improve !

https://github.com/ikennarichard/mdn-active-learning/blob/main/test-1.js

Hi @kenna and welcome to the community :wave:

Well done! :tada:

Here are some tips to make the code simpler:

  • When you count genders you could just use male++ and female++.
  • The kittenInfo line can go outside the inner loop. It’s fine to assign the string once after we are finished counting.
  • If you use a traditional loop for the outer loop you could directly edit the motherInfo instead of using an additional loop.

Do you have an answer for this?

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.

Keep up the great work! :+1:

Michael

Hello @mikoMK thanks a lot for the superpowers. For the question, i think is because we dont want textcontent set to the initial values. not sure. Thanks agian! for the review

Yes, that’s right. The interesting point is why it would be empty. After all, the assignment is placed after the displayCatInfo() function. So one may assume that the assignment also happens after the function run.
This is not the case because fetch() is an async function. After asking the server for the JSON file it hands over the control to the rest of the code while it is waiting for the result. If the assignments were outside the displayCatInfo() function, they would then be filled with the initial values. When the JSON arrives from the server, it would already be to late and any changes to motherInfo and kittenInfo wouldn’t have an effect.

That’s the fun with async code. :grin: Very useful, but needs some time to fully understand.

1 Like

:exploding_head: Well said Michael, your the best!

1 Like