Assessment wanted for JSON skill test by Dinesh

Hello there :wave:,
I have just finished working with JSON skill test and I need an assessment for my code. Here are the Code pen link & Task link.

Thank you in Advance,
Dinesh

Hi @Dinesh_Sake

Well done, again. :clap:

Here are some comments:

  • You missed this task:

    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?

  • No need to count female kittens. You can calculate it from total and male.
  • To increase a value by one you could also use ++ (e.g. male++;)

I’m curious about your improvements. :slightly_smiling_face:

Happy coding,
Michael

1 Like

Hello sir, I’ve changed the code according to your comments, but I’m unable to insert “add” before the last word. I have tried several methods and none of them are working properly, I would really appreciate your help with this.

Thank you,
Dinesh

1 Like

The counting is fine now.

Regarding the sentence you need a if..else statement and it would help to use a traditional for loop, because we can use the index in the condition:

if (<condition>) {
  motherInfo += `${ cats[i].name }, `;
} else {
  motherInfo += `and ${ cats[i].name }.`;
}

The <condition> needs to be true as long as we are not in the last loop. To do this we need to compare the index with cats.length.

1 Like

Okay Sir, I have done the changes, please check for any errors.

Thank you,
Dinesh

Okay, now it works. :+1:

Another possibility instead of writing a second loop would have been rewriting for (const cat of cats){ to for (let i = 0; i < cats.length; i++){ and put all the code into the same loop.
By the way: You sliced the last two letters of the last cat. It should be “Antonia”.

This would be a single loop that does all the work:

for (let i = 0; i < cats.length; i++) {
  for (const kitten of cats[i].kittens) {
    total++;
    if (kitten.gender === 'm') {
      male++;
    }
  }

  if (i < (cats.length - 1)) {
    motherInfo += `${ cats[i].name }, `;
  } else {
    motherInfo += `and ${ cats[i].name }.`;
  }
}
1 Like