Test your skills: JSON 1

Hi, can someone have a look at my code. Its output not quite the right way.

Output:
The mother cats are called Lindy, Mina, and Antonia.

Their kittens are called Percy, Thea, Annis, Doris, Pickle, and Max.Bridget, Randolph, There are 8 kittens, 5 females and 3 males.

It should be like:

“Their kittens are called Percy, Thea, Annis, Doris, Pickle, Max, Bridget, and Randolph. There are 8 kittens, 5 females and 3 males.”

How can we correct the second sentence? Thanks alot.

https://codepen.io/vddroid/pen/rNOMoBY

Hi @vddroid!

This is a really good attempt at working this out. Our answer looks like this : https://github.com/mdn/learning-area/blob/master/javascript/oojs/tasks/json/marking.md#task-1

The question didn’t ask you to add the names of the kittens to the second sentence, just their total number and the numbers of males and females, so you are going the extra mile here — this is really cool.

The reason the second sentence is not structured correctly is that this bit gets run for each mother cat:

for (let j = 0; j < babies.length; j++) {
      total++;
      if (babies[j].gender === "m") {
        male++;
      }
      if (j === babies[j]["name"].length - 1) {
        kittenInfo += "and " + babies[j]["name"] + ".";
      } else {
        kittenInfo += babies[j]["name"] + ", ";
      }
    }

So for each mother cat, you get the structure “catName.” created for their last kitten. What you should do instead is add all the kittens to a separate variable (say, kittenNames), all with the structure "catName, ", and then outside the j loop, adjust the kittenNames string so that the last instance of ", " is changed to “.”. You should be able to do that with some simple string substitution techniques, methods like substring() and replace().

Then after all that is done, then add kittenNames to kittenInfo.

1 Like

Thanks for your help. I tried to call the kittens name while doing the mother’s, I thought it would be the same, but it’s not. I corrected the code, is it correct?.

https://codepen.io/vddroid/pen/rNOMoBY?editors=1010

The ouput is:
The mother cats are called Lindy, Mina, and Antonia.

Their kittens are called Percy, Thea, Annis, Doris, Pickle, Max, Bridget, Randolph. There are 8 kittens, 5 females and 3 males.

Is there a way to call the kittens name with ‘and’ in it like calling mother’s name using loop? or is cut and paste the only way?

@vddroid this is looking much better - nice work!

Is there a way to call the kittens name with ‘and’ in it like calling mother’s name using loop?

I’m not 100% sure what you mean here, but I’m guessing you want to update it so that the kitten output ends up being

Their kittens are called Percy, Thea, Annis, Doris, Pickle, Max, Bridget, and Randolph. There are 8 kittens, 5 females and 3 males.

Correct?

In which case, you could do what you’ve already done, and then use lastIndexOf() to return the last position of a comma in the string, and then do a replacement on this part. This s another step, and another substring, but hey, it would work.

1 Like

@chrisdavidmills

Yes, that’s what I meant. Thank you.The correct output now is:

Their kittens are called Percy, Thea, Annis, Doris, Pickle, Max, Bridget and Randolph. There are 8 kittens, 5 females and 3 males.

https://codepen.io/vddroid/pen/rNOMoBY?editors=1010

@vddroid excellent — well done!