Hi, I’d like an assessment of my code.
I didn’t find the exercise difficult and I didn’t encounter problems with JSON, my request is more if my code is efficient, straightforward, not redundant, specifically about the loops I used and how I created the variables, thanks.
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;
let female = 0;
// Add your code here
const arr = JSON.parse(catString);
for (const [index, cat] of arr.entries()) {
if (index === arr.length - 1) motherInfo += `and ${cat.name}.`;
else motherInfo += `, ${cat.name} `;
total += cat.kittens.length;
for (const kitten of cat.kittens) {
kitten.gender === 'm' ? male++ : female++;
}
}
kittenInfo = `Kittens are a total of ${total}, of which ${male} males and ${female} females.`
// Don't edit the code below here!
para1.textContent = motherInfo;
para2.textContent = kittenInfo;
}
section.appendChild(para1);
section.appendChild(para2);
This is the link to the exercise.
Thank you a lot, I’m striving to always follow best practices and use the most appropriate syntax.