CARVIEW |
Select Language
HTTP/2 200
content-type: text/html
x-guploader-uploadid: AAwnv3LadK8BqrbPI_-Vo1CQfHKF0YNBJGZwZ0QhtHa-YpTAoQbU6xbrDkrwea6N9v90KBruTW_T9z4
cache-control: public, max-age=3600
expires: Fri, 10 Oct 2025 12:08:01 GMT
last-modified: Fri, 10 Oct 2025 01:10:22 GMT
etag: W/"504d7964812daf6035998dec50d266c7"
x-goog-generation: 1760058622507756
x-goog-metageneration: 1
x-goog-stored-content-encoding: identity
x-goog-stored-content-length: 186547
x-goog-meta-goog-reserved-file-mtime: 1760057027
x-goog-hash: crc32c=myJ1Uw==, md5=UE15ZIEtr2A1mY3sUNJmxw==
x-goog-storage-class: STANDARD
accept-ranges: none
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
alt-svc: clear
referrer-policy: strict-origin-when-cross-origin
x-content-type-options: nosniff
strict-transport-security: max-age=63072000
content-security-policy: default-src 'self'; script-src 'report-sample' 'self' 'wasm-unsafe-eval' https://www.google-analytics.com/analytics.js https://www.googletagmanager.com/gtag/js assets.codepen.io production-assets.codepen.io https://js.stripe.com 'sha256-XNBp89FG76amD8BqrJzyflxOF9PaWPqPqvJfKZPCv7M=' 'sha256-YCNoU9DNiinACbd8n6UPyB/8vj0kXvhkOni9/06SuYw=' 'sha256-PZjP7OR6mBEtnvXIZfCZ5PuOlxoDF1LDZL8aj8c42rw='; script-src-elem 'report-sample' 'self' 'wasm-unsafe-eval' https://www.google-analytics.com/analytics.js https://www.googletagmanager.com/gtag/js assets.codepen.io production-assets.codepen.io https://js.stripe.com 'sha256-XNBp89FG76amD8BqrJzyflxOF9PaWPqPqvJfKZPCv7M=' 'sha256-YCNoU9DNiinACbd8n6UPyB/8vj0kXvhkOni9/06SuYw=' 'sha256-PZjP7OR6mBEtnvXIZfCZ5PuOlxoDF1LDZL8aj8c42rw='; style-src 'report-sample' 'self' 'unsafe-inline'; object-src 'none'; base-uri 'self'; connect-src 'self' developer.allizom.org bcd.developer.allizom.org bcd.developer.mozilla.org updates.developer.allizom.org updates.developer.mozilla.org https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com https://incoming.telemetry.mozilla.org https://observatory-api.mdn.allizom.net https://observatory-api.mdn.mozilla.net https://api.github.com/search/issues stats.g.doubleclick.net https://api.stripe.com; font-src 'self'; frame-src 'self' interactive-examples.mdn.mozilla.net interactive-examples.mdn.allizom.net mdn.github.io live-samples.mdn.mozilla.net live-samples.mdn.allizom.net *.mdnplay.dev *.mdnyalp.dev *.play.test.mdn.allizom.net https://v2.scrimba.com https://scrimba.com jsfiddle.net www.youtube-nocookie.com codepen.io survey.alchemer.com https://js.stripe.com; img-src 'self' data: *.githubusercontent.com *.googleusercontent.com *.gravatar.com mozillausercontent.com firefoxusercontent.com profile.stage.mozaws.net profile.accounts.firefox.com developer.mozilla.org mdn.dev interactive-examples.mdn.mozilla.net interactive-examples.mdn.allizom.net wikipedia.org upload.wikimedia.org https://mdn.github.io/shared-assets/ https://mdn.dev/ https://*.google-analytics.com https://*.googletagmanager.com www.gstatic.com; manifest-src 'self'; media-src 'self' archive.org videos.cdn.mozilla.net https://mdn.github.io/shared-assets/; child-src 'self'; worker-src 'self';
x-frame-options: DENY
origin-trial: AxVILwizhbMjxFeHOn1P3R8niO1RJY/smaK4B4d1rLzc1gTaxtXMSaTi+FoigYgCw40uFRDwFcEAeqDR+vVLOW4AAABfeyJvcmlnaW4iOiJodHRwczovL2RldmVsb3Blci5tb3ppbGxhLm9yZyIsImZlYXR1cmUiOiJQcml2YXRlQXR0cmlidXRpb25WMiIsImV4cGlyeSI6MTc0MjA3OTYwMH0=
x-cloud-trace-context: 5fe9b639807728c30eee1fc3605f075e
date: Fri, 10 Oct 2025 11:08:01 GMT
server: Google Frontend
via: 1.1 google
vary: Accept-Encoding
content-encoding: gzip
x-cache: miss
スキルテスト: JSON - ウェブ開発の学習 | MDN
Toggle sidebar
このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。
スキルテスト: JSON
このスキルテストの目的は、JSON の操作の記事を理解できたかどうかを評価することです。
メモ: 手助けが必要な場合は、スキルテスト使用ガイドをお読みください。また、コミュニケーションチャネルのいずれかを使用して、私たちに連絡することもできます。
JSON 1
この記事の唯一の課題は、JSON データにアクセスして、それをページ内で使用することに関するものです。いくつかの母猫とその子猫に関する JSON データが sample.json で提供されています。JSON は文字列としてページに読み込まれ、displayCatInfo()
関数の catString
引数で利用できるようになります。
この課題を完成させるには、格納するために displayCatInfo()
関数の不足部分を埋めてください。
- 母猫 3 匹の名前をカンマで区切って
motherInfo
という変数に入れてください。 kittenInfo
変数には、子猫の総数、およびオスとメスの数を格納してください。
そして、これらの変数の値が、段落の中で画面に表示されるようにします。
ヒントや質問をいくつか示します。
- JSON データは
displayCatInfo()
関数の中でテキストとして提供されます。JSON から何かデータを取得する前に、JSON を解釈する必要があります。 - 外側のループを使用して猫をループ処理して、その名前を
motherInfo
変数文字列に追加し、内側のループを使用してすべての子猫をループ処理して、すべての子猫/オス/メスの合計を加算し、その詳細をkittenInfo
変数の文字列に追加するとよいでしょう。 - 最後の母猫の名前の前に "and" を、その後にピリオドを置く必要があります。どうすれば、JSON に何匹の猫がいても、このような動作をさせることができるでしょうか。
- なぜ
para1.textContent = motherInfo;
とpara2.textContent = kittenInfo;
の行はdisplayCatInfo()
関数の中にあり、スクリプトの終わりにはないのでしょうか。これは、非同期コードと関係があります。
<p class="one"></p>
<p class="two"></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));
// 以上のコードは編集しないでください。
function displayCatInfo(catString) {
let total = 0;
let male = 0;
// ここにコードを追加
// 以下のコードは編集しないでください。
para1.textContent = motherInfo;
para2.textContent = kittenInfo;
}
ここをクリックすると、模範解答を表示します。
最終的な JavaScript は次のようになります。
js
// ...
// 以上のコードは編集しないでください。
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 = `全部で ${total} 匹の子猫がいます。オスは ${male} 匹でメスは ${
total - male
} 匹です。`;
// 以下のコードは編集しないでください。
para1.textContent = motherInfo;
para2.textContent = kittenInfo;
}