Hi. Any assessment for would be appreciated. I tried this test with Glitch by uploading json file. Hope I did it right.
Thank you and have a good day!
Hi. Any assessment for would be appreciated. I tried this test with Glitch by uploading json file. Hope I did it right.
Thank you and have a good day!
Hi there @harryghgim,
Well done on this assessment — this is a fairly difficult one, and you have provided a perfect solution!
And you also presented it perfectly using Glitch. It is a good idea to do this, and avoid the potential for any CORS errors due to the JSON being on a different domain to the JS that loads it.
I don’t really have any significant suggestions for improvement here; one small thing that did spring to mind is that when you increase the values of the male
and female
variables using syntax like this:
male += 1;
If you are only incrementing by one, you can use the following shorthand to make it even shorter:
male ++;
Thank you @harryghgim for your solution, it was helpful.
I believe I improved it and made more elegant by adding .filter
which let me avoid another condition https://codepen.io/peargrape/pen/jOzmddg
// Add your code here
const displayCats = JSON.parse(catString);
for (let i=0; i<displayCats.length; i++) {
if (i<displayCats.length-1) {
motherInfo += `${displayCats[i].name}, `;
} else {
motherInfo += `and ${displayCats[i].name}.`;
}
total += displayCats[i].kittens.length;
male += displayCats[i].kittens.filter(kitten => kitten.gender == 'm').length;
}
kittenInfo = `The total amount of kittens is ${total} including ${male} males and ${total - male} females.`;
Thank you Michael! Glad to be a helpful part of something great!