docs.rodeo

MDN Web Docs mirror

Test your skills: JSON

The aim of this skill test is to assess whether you’ve understood our Working with JSON article.

[!NOTE] You can try solutions by downloading the code and putting it in an online editor such as CodePen or JSFiddle. If there is an error, it will be logged in the results panel on the page or into the browser’s JavaScript console to help you.

If you get stuck, you can reach out to us in one of our communication channels.

JSON 1

The one and only task in this article concerns accessing JSON data and using it in your page. JSON data about some mother cats and their kittens is available in sample.json. The JSON is loaded into the page as a text string and made available in the catString parameter of the displayCatInfo() function.

To complete the task:

  1. Click “Play” in the code block below to edit the example in the MDN Playground.
  2. Fill in the missing parts of the displayCatInfo() function to store:
    • The names of the three mother cats, separated by commas, in the motherInfo variable.
    • The total number of kittens, and how many are male and female, in the kittenInfo variable.

The values of these variables are then printed to the screen inside paragraphs.

Some hints/questions:

[!CALLOUT]

You can also download the starting point for this task to work in your own editor or in an online editor.

If you make a mistake, you can clear your work using the Reset button in the MDN Playground. If you get really stuck, you can view the solution below the live output.

<p class="one"></p>
<p class="two"></p>
p {
  color: purple;
  margin: 0.5em 0;
}

* {
  box-sizing: border-box;
}
const para1 = document.querySelector(".one");
const para2 = document.querySelector(".two");
let motherInfo = "The mother cats are called ";
let kittenInfo;
const requestURL =
  "https://mdn.github.io/learning-area/javascript/oojs/tasks/json/sample.json";

fetch(requestURL)
  .then((response) => response.text())
  .then((text) => displayCatInfo(text));

// Don't edit the code above here!

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

  // Add your code here

  // Don't edit the code below here!

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

{{ EmbedLiveSample("json-1", "100%", 60) }} 

Click here to show the solution

Your finished JavaScript should look something like this:

// ...
// Don't edit the code above here!

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

  const cats = JSON.parse(catString);

  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}.`;
    }
  }

  kittenInfo = `There are ${total} kittens in total, ${male} males and ${
    total - male
  } females.`;

  // Don't edit the code below here!

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

In this article

View on MDN