Assessment JSON test your skills

hi i was hoping someone could give me a little feed back on the JSON exercise please

https://jsfiddle.net/dowey/4j3o5pra/4/

Hi @tryingagain and welcome to the community :wave:

You wrote some great code and solved nearly everything correctly. Congratulations! :medal_sports:
I say “nearly” because there is a tiny thing missing:

The last mother cat name should have an “and” before it, and a full stop after it. How do you make sure this can work, no matter how many cats are in the JSON?

Do you have an idea how to solve this?

Have a nice day,
Michael

1 Like

Ahh! thank you. When possible could you please check my revised code. thanks.

https://jsfiddle.net/dowey/4j3o5pra/8/

Well done, @tryingagain!

This works as expected. Another (maybe simpler) solution would be to use nested if..else statements, where you check for the second to last and last element based on the length and i.

Pseudo code:

if (second to last) {
  add 'name and' to motherInfo
} else if (last) {
  add 'name' to motherInfo
} else { /* all others */
  add 'name, ' to motherInfo
}

Keep up the great work!
Michael

1 Like

oh yes - thanks, that’s a lot easier. Revised code below. thank you.

https://jsfiddle.net/dowey/4j3o5pra/10/

Nice improvement!

Another advice (no need to rewrite the example, again): This may be a case where a traditional for loop would be more fitting since you have direct access to the index. You could put the conditions part inside your second loop (where you already use a traditional loop over the whole array) and write easier conditions like if (i === catParsed.length -2).

1 Like