Assessment wanted for my JSON skill test

Hi, I wanted assessment with this code:

const section = document.querySelector('section');

let para1 = document.createElement('p');
let para2 = document.createElement('p');

let motherInfo = 'The mother cats are called ';
let kittenInfo;

fetch('sample.json')
.then(response => response.text())
.then(text => displayCatInfo(text))

function displayCatInfo(catString) {
  let total = 0;
  let male = 0;

// My code here
cats = JSON.parse(catString);
for (cat of cats) {
  if (cats.indexOf(cat) == cats.length - 1) motherInfo += `and ${cat.name}.`;
  else motherInfo += `${cat.name}, `;

  total += cat.kittens.length;
  for (kitten of cat.kittens) if (kitten.gender === "m") male++;
}
female = total - male;
kittenInfo = `The total number of kittens is ${total}, of which ${male} are male and ${female} female.`;

// Don't edit the code below here!

  para1.textContent = motherInfo;
  para2.textContent = kittenInfo;
}

section.appendChild(para1);
section.appendChild(para2);

The link the test is referring to is: Test your skills: JSON. Thank you in advance!

Hi @jurggjon and welcome to the community! :wave:

Congratulations on your work! Your code works as expected. Nice usage of the for...of loop. :medal_sports:

Two remarks:

  • You should use const in your variable definitions (e.g. const cats = ...) otherwise they end up in the global scope.
  • About code style: I recommend writing loops and ifs with curly braces and on new lines even when they are only one line long. It improves readability and makes it less error-prone. Of course this is just an opinion as your code works with those one-liners.

I hope this feedback helps. Feel free to ask questions or come back with more tasks to be assessed. :slightly_smiling_face:

Happy coding!
Michael

PS: Your email address is visible in your post. I think it somehow ended up in the optional “name” field of your profile. I recommend removing it.