Movatterモバイル変換


[0]ホーム

URL:


  1. Learn
  2. Core learning modules
  3. JavaScript
  4. Test: JavaScript tests index
  5. Test: JSON

Test your skills: JSON

The aim of this skill test is to help you assess whether you've understood ourWorking with JSON article.

Note:To get help, read ourTest your skills usage guide. You can also reach out to us using one of ourcommunication 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 insample.json. The JSON is loaded into the page as a text string and made available in thecatString parameter of thedisplayCatInfo() function.

To complete the task, fill in the missing parts of thedisplayCatInfo() function to store:

  • The names of the three mother cats, separated by commas, in themotherInfo variable.
  • The total number of kittens, and how many are male and female, in thekittenInfo variable.

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

Some hints/questions:

  • The JSON data is provided as text inside thedisplayCatInfo() function. You'll need to parse it into JSON before you can get any data out of it.
  • You'll probably want to use an outer loop to loop through the cats and add their names to themotherInfo variable string, and an inner loop to loop through all the kittens, add up the total of all/male/female kittens, and add those details to thekittenInfo variable string.
  • The last mother cat name should have an "and" before it, and a full stop after it. How do you make sure this can work, no matter how many cats are in the JSON?
  • Why are thepara1.textContent = motherInfo; andpara2.textContent = kittenInfo; lines inside thedisplayCatInfo() function, and not at the end of the script? This has something to do with async code.
<p></p><p></p>
p {  color: purple;  margin: 0.5em 0;}* {  box-sizing: border-box;}
js
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;}
Click here to show the solution

Your finished JavaScript should look something like this:

js
// ...// 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;}

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp