chore(i18n,learn): processed translations (#44851)

This commit is contained in:
camperbot
2022-01-21 01:00:18 +05:30
committed by GitHub
parent f866718a3d
commit 5c868af2b8
1696 changed files with 159426 additions and 69 deletions

View File

@ -0,0 +1,167 @@
---
id: 587d7fae367417b2b2512be4
title: API から JSON データにアクセスする
challengeType: 6
forumTopicId: 301499
dashedName: access-the-json-data-from-an-api
---
# --description--
前回のチャレンジでは、freeCodeCamp Cat Photo API から JSON データを取得する方法を学びました。
次は、JSON フォーマットをよりよく理解するために、返されたデータを詳しく見ていきしょう。 JavaScript での以下の記法を思い出してください。
<blockquote>[ ] -> 角括弧は配列<br>{ } -> 中括弧はオブジェクト<br>を表し、" " -> 二重引用符は文字列を表します。 これらは JSON 内のキーの名前にも使用されます。</blockquote>
API が返すデータの構造を理解することは重要です。なぜなら、必要な値を取得する方法がそれによって変わるからです。
右側の `Get Message` ボタンをクリックして、freeCodeCamp Cat Photo API JSON を HTML に読み込んでください。
JSON データの最初と最後の文字は角括弧 `[ ]` です。 これは、返されたデータが配列であることを意味します。 JSON データ内の 2 番目の文字は中かっこ `{` で、ここからオブジェクトが開始します。 よく見ると、3 つの別々のオブジェクトがあることが分かります。 この JSON データは 3 つのオブジェクトの配列であり、各オブジェクトには猫の写真に関する情報が含まれています。
既に学んだように、オブジェクトにはカンマで区切られた「キーと値のペア」が含まれています。 猫の写真の例では、最初のオブジェクトに `"id":0` が含まれています。`id` はキー、`0` はそれに対応する値です。 同様に、`imageLink``altText`、および `codeNames` のそれぞれにキーがあります。 猫の写真の各オブジェクトはこれらの同じキーを持っていますが、値はそれぞれ異なります。
1 つ目のオブジェクトが持つもう一つの興味深い「キーと値のペア」は、`"codeNames":["Juggernaut","Mrs. Wallace","ButterCup"]` です。 ここでは `codeNames` がキーで、値は 3 つの文字列からなる配列です。 キーと、その値である配列とのペア以外に、オブジェクトの配列を持つことも可能です。
配列やオブジェクト内のデータにアクセスする方法を思い出してください。 配列はブラケット記法を使用して、アイテムの特定のインデックスにアクセスします。 オブジェクトはブラケット記法またはドット記法を使用して、与えられたプロパティの値にアクセスします。 最初の猫の写真の `altText` プロパティを出力する例を下に示します。エディタ内の解析された JSON データが、`json` と呼ばれる変数に保存されていることに注意してください。
```js
console.log(json[0].altText);
```
コンソールに、`A white cat wearing a green helmet shaped melon on its head.` という文字列が表示されます。
# --instructions--
`id` 2 の猫について、`codeNames` 配列内の 2 番目の値をコンソールに出力してください。 値にアクセスするには、(変数 `json` に保存されている) オブジェクトにブラケット記法とドット記法を使用する必要があります。
# --hints--
ブラケット記法とドット記法を使用して適切なコード名にアクセスし、コンソールに `Loki` を出力する必要があります。
```js
assert(
code.match(
/console\s*\.\s*log\s*\(\s*json\s*\[2\]\s*(\.\s*codeNames|\[\s*('|`|")codeNames\2\s*\])\s*\[\s*1\s*\]\s*\)/g
)
);
```
# --seed--
## --seed-contents--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('getMessage').onclick = function(){
const req = new XMLHttpRequest();
req.open("GET",'/json/cats.json', true);
req.send();
req.onload=function(){
const json = JSON.parse(req.responseText);
document.getElementsByClassName('message')[0].innerHTML = JSON.stringify(json);
// Add your code below this line
// Add your code above this line
};
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message box">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```
# --solutions--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('getMessage').onclick = function(){
const req = new XMLHttpRequest();
req.open("GET",'/json/cats.json', true);
req.send();
req.onload=function(){
const json = JSON.parse(req.responseText);
document.getElementsByClassName('message')[0].innerHTML = JSON.stringify(json);
// Add your code below this line
console.log(json[2].codeNames[1]);
// Add your code above this line
};
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```

View File

@ -0,0 +1,140 @@
---
id: 587d7fad367417b2b2512be2
title: クリックイベントでテキストを変更する
challengeType: 6
forumTopicId: 301500
dashedName: change-text-with-click-events
---
# --description--
クリックイベントが発生したとき、JavaScript を使用して HTML 要素を更新できます。
例えばユーザーが `Get Message` ボタンをクリックすると、`message` クラスを持つその要素のテキストが `Here is the message` に変わるとします。
これを行うには、クリックイベント内に次のコードを追加します。
```js
document.getElementsByClassName('message')[0].textContent="Here is the message";
```
# --instructions--
`message` 要素内のテキストを `Here is the message` に変更するために、`onclick` イベントハンドラ内にコードを追加してください。
# --hints--
`document.getElementsByClassName` メソッドを使用して `message` クラスを持つ要素を選択し、その `textContent` を、与えられた文字列に設定する必要があります。
```js
assert(
code.match(
/document\s*\.getElementsByClassName\(\s*?('|")message\1\s*?\)\[0\]\s*\.textContent\s*?=\s*?('|")Here is the message\2/g
)
);
```
# --seed--
## --seed-contents--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('getMessage').onclick = function(){
// Add your code below this line
// Add your code above this line
}
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message box">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```
# --solutions--
```html
<script>
document.addEventListener('DOMContentLoaded',function(){
document.getElementById('getMessage').onclick = function(){
// Add your code below this line
document.getElementsByClassName('message')[0].textContent = "Here is the message";
// Add your code above this line
}
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```

View File

@ -0,0 +1,199 @@
---
id: 587d7fae367417b2b2512be5
title: JSON データを HTML に変換する
challengeType: 6
forumTopicId: 16807
dashedName: convert-json-data-to-html
---
# --description--
JSON API からデータを取得したので、そのデータを HTML で表示できます。
猫の写真のオブジェクトが配列に格納されているので、`forEach` メソッドを使用してデータをループ処理することができます。 各アイテムに到達すると、HTML 要素を変更することができます。
まず、`let html = "";` で html 変数を宣言します。
次に、JSON をループ処理し、キー名を `strong` タグでラップする変数に HTML を追加し、その後に値を追加します。 ループが終わったら、それをレンダリングします。
これを行うコードを次に示します。
```js
let html = "";
json.forEach(function(val) {
const keys = Object.keys(val);
html += "<div class = 'cat'>";
keys.forEach(function(key) {
html += "<strong>" + key + "</strong>: " + val[key] + "<br>";
});
html += "</div><br>";
});
```
**注:** このチャレンジでは、新しい HTML 要素をページに追加する必要があるので、`textContent` に頼ることはできません。 代わりに、`innerHTML` を使用する必要があります。これにより、サイトがクロスサイトスクリプティング攻撃に対して脆弱になる可能性があります。
# --instructions--
`forEach` メソッドを追加して JSON データをループ処理し、それを表示するための HTML 要素を作成してください。
JSON の例を示します。
```json
[
{
"id":0,
"imageLink":"https://s3.amazonaws.com/freecodecamp/funny-cat.jpg",
"altText":"A white cat wearing a green helmet shaped melon on its head. ",
"codeNames":[ "Juggernaut", "Mrs. Wallace", "Buttercup"
]
}
]
```
# --hints--
`html` 変数にデータを保存する必要があります。
```js
assert(__helpers.removeWhiteSpace(code).match(/html(\+=|=html\+)/g))
```
`forEach` メソッドを使用して、API から取得した JSON データをループ処理する必要があります。
```js
assert(code.match(/json\.forEach/g));
```
`strong` タグでキー名をラップする必要があります。
```js
assert(code.match(/<strong>.+<\/strong>/g));
```
# --seed--
## --seed-contents--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('getMessage').onclick = function(){
const req = new XMLHttpRequest();
req.open("GET",'/json/cats.json',true);
req.send();
req.onload = function(){
const json = JSON.parse(req.responseText);
let html = "";
// Add your code below this line
// Add your code above this line
document.getElementsByClassName('message')[0].innerHTML = html;
};
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message box">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```
# --solutions--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('getMessage').onclick = function(){
const req = new XMLHttpRequest();
req.open("GET",'/json/cats.json',true);
req.send();
req.onload = function(){
const json = JSON.parse(req.responseText);
let html = "";
// Add your code below this line
json.forEach(function(val) {
var keys = Object.keys(val);
html += "<div class = 'cat'>";
keys.forEach(function(key) {
html += "<strong>" + key + "</strong>: " + val[key] + "<br>";
});
html += "</div><br>";
});
// Add your code above this line
document.getElementsByClassName('message')[0].innerHTML = html;
};
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```

View File

@ -0,0 +1,98 @@
---
id: 587d7faf367417b2b2512be8
title: ユーザーの GPS 座標を見つけるために位置情報データを取得する
challengeType: 6
forumTopicId: 18188
dashedName: get-geolocation-data-to-find-a-users-gps-coordinates
---
# --description--
もう一つ便利な点は、ユーザーの現在位置にアクセスできることです。 すべてのブラウザには、この情報を提供できるナビゲータが組み込まれています。
ナビゲータはユーザーの現在の経度と緯度を取得します。
このサイトがあなたの現在位置を知ることについて、許可するか、拒否するかを尋ねるメッセージが表示されます。 コードが正しければ、そのいずれでもチャレンジを完了できます。
「許可」を選択すると、出力先の携帯電話に表示されるテキストが緯度と経度に変わります。
これを行うコードを次に示します。
```js
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position) {
document.getElementById('data').innerHTML="latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude;
});
}
```
まず、`navigator.geolocation` オブジェクトが存在するかどうかを確認します。 存在する場合、そのオブジェクトの `getCurrentPosition` メソッドが呼び出され、ユーザーの位置に対する非同期リクエストが開始されます。 リクエストが成功すると、メソッド内のコールバック関数が実行されます。 この関数は、ドット記法を使用して `position` オブジェクトの緯度と経度の値にアクセスし、HTMLを更新します。
# --instructions--
サンプルコードを `script` タグ内に追加してユーザーの現在位置を確認し、それを HTML に挿入してください。
# --hints--
`navigator.geolocation` を使用してユーザーの現在位置にアクセスする必要があります。
```js
assert(code.match(/navigator\.geolocation\.getCurrentPosition/g));
```
`position.coords.latitude` を使用して、ユーザーの緯度位置を表示する必要があります。
```js
assert(code.match(/position\.coords\.latitude/g));
```
`position.coords.longitude` を使用して、ユーザーの経度位置を表示する必要があります。
```js
assert(code.match(/position\.coords\.longitude/g));
```
`id="data"` を持つ `div` 要素内にユーザーの位置を表示する必要があります。
```js
assert(
code.match(/document\.getElementById\(\s*?('|")data\1\s*?\)\.innerHTML/g)
);
```
# --seed--
## --seed-contents--
```html
<script>
// Add your code below this line
// Add your code above this line
</script>
<h4>You are here:</h4>
<div id="data">
</div>
```
# --solutions--
```html
<script>
// Add your code below this line
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
document.getElementById('data').innerHTML = "latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude;
});
}
// Add your code above this line
</script>
<h4>You are here:</h4>
<div id="data">
</div>
</section>
```

View File

@ -0,0 +1,174 @@
---
id: 5ccfad82bb2dc6c965a848e5
title: JavaScript fetch メソッドで JSON を取得する
challengeType: 6
forumTopicId: 301501
dashedName: get-json-with-the-javascript-fetch-method
---
# --description--
外部データをリクエストするもう一つの方法は、JavaScript `fetch()` メソッドを使用することです。 これは `XMLHttpRequest` と同様のメソッドですが、構文はそれよりも理解しやすいと考えられています。
次のコードは、`/json/cats.json` に対して GET リクエストを行います。
```js
fetch('/json/cats.json')
.then(response => response.json())
.then(data => {
document.getElementById('message').innerHTML = JSON.stringify(data);
})
```
コードを細かく見ていきましょう。
最初の行ではリクエストを実行しています。 `fetch(URL)` は、指定された URL に対して `GET` リクエストを行います。 このメソッドはプロミスを返します。
プロミスが返された後、リクエストが成功していれば `then` メソッドが実行されます。このメソッドは、レスポンスを取り JSON フォーマットに変換します。
`then` メソッドもプロミスを返し、それは次の `then` メソッドで処理されます。 2 番目の `then` の中の引数が、あなたが探している JSON オブジェクトです!
ここで、このメソッドは `document.getElementById()` を使用して、データを受け取る要素を選択します。 次に、リクエストから返された JSON オブジェクトで作成された文字列を挿入することにより、要素の HTML コードを変更します。
# --instructions--
`GET` リクエストを作成して freeCodeCamp Cat Photo API に送信するように、コードを更新してください。 ただし今回は、`XMLHttpRequest` の代わりに `fetch` メソッドを使用してください。
# --hints--
`fetch``GET` リクエストを行う必要があります。
```js
assert(code.match(/fetch\s*\(\s*('|")\/json\/cats\.json\1\s*\)/g));
```
`then` を使用して応答を JSON に変換する必要があります。
```js
assert(
code.match(
/\.then\s*\(\s*\(?(?<var>\w+)\)?\s*=>\s*\k<var>\s*\.json\s*\(\s*\)\s*\)/g
)
);
```
`then` を使用して、他の `then` によって JSON に変換されたデータを処理する必要があります 。
```js
assert(__helpers.removeWhiteSpace(code).match(/\.then\(\(?\w+\)?=>{[^}]*}\)/g));
```
`message` という id を持つ要素を取得し、内部の HTML を JSON データの文字列に変更する必要があります。
```js
assert(
__helpers.removeWhiteSpace(code).match(
/document\.getElementById\(('|")message\1\)\.innerHTML=JSON\.stringify\(?\w+\)/g
)
);
```
# --seed--
## --seed-contents--
```html
<script>
document.addEventListener('DOMContentLoaded',function(){
document.getElementById('getMessage').onclick= () => {
// Add your code below this line
// Add your code above this line
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p id="message" class="box">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```
# --solutions--
```html
<script>
document.addEventListener('DOMContentLoaded',function(){
document.getElementById('getMessage').onclick= () => {
fetch('/json/cats.json')
.then(response => response.json())
.then(data => {
document.getElementById('message').innerHTML=JSON.stringify(data);
})
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p id="message" class="box">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```

View File

@ -0,0 +1,196 @@
---
id: 587d7fae367417b2b2512be3
title: JavaScript XMLHttpRequest メソッドで JSON を取得する
challengeType: 6
forumTopicId: 301502
dashedName: get-json-with-the-javascript-xmlhttprequest-method
---
# --description--
外部ソースにデータをリクエストすることもできます。 ここで API の出番です。
API (アプリケーション・プログラミング・インターフェイス) はコンピュータが相互通信に使用する道具であるということを思い起こしましょう。 このチャレンジでは、AJAX と呼ばれる技術で API からデータを取得し、そのデータを使用して HTML を更新する方法を学びます。
ほとんどのウェブ API は、JSON と呼ばれるフォーマットでデータを転送します。 JSON は JavaScript Object Notation の略です。
JSON 構文は JavaScript オブジェクトのリテラル表記と非常によく似ています。 JSON にはオブジェクトのプロパティとその現在値があり、それらは「`{`」と「`}`」の間に挟まれています。
これらのプロパティとその値は、しばしば「キーと値のペア」(key-value pairs) と呼ばれます。
ただし、API によって送信された JSON は `bytes` (バイト列) として送信され、アプリケーションはそれを `string` (文字列) として受信します。 これらは JavaScript オブジェクトに変換できますが、デフォルトでは JavaScript オブジェクトではありません。 `JSON.parse` メソッドは文字列を解析し、その文字列によって記述された JavaScript オブジェクトを構築します。
freeCodeCamp の Cat Photo API に JSON をリクエストできます。 これを行うには、次のコードをクリックイベントに追加します。
```js
const req = new XMLHttpRequest();
req.open("GET",'/json/cats.json',true);
req.send();
req.onload = function(){
const json = JSON.parse(req.responseText);
document.getElementsByClassName('message')[0].innerHTML = JSON.stringify(json);
};
```
このコードが何をしているのか、一つずつ見ていきましょう。 JavaScript `XMLHttpRequest` オブジェクトには、データ転送に使用される数多くのプロパティとメソッドがあります。 最初に、`XMLHttpRequest` オブジェクトのインスタンスが作成され、`req` 変数に保存されます。 次に、`open` メソッドがリクエストを初期化します。この例では API にデータを要求するので、`GET` リクエストです。 `open` の 2 番目の引数は、データ要求先の API の URL です。 3 番目の引数はブール値で、`true` であれば非同期リクエストになります。 `send` メソッドがリクエストを送信します。 最後に `onload` イベントハンドラが、返されたデータを解析し、`JSON.stringify` メソッドを適用して JavaScript オブジェクトを文字列に変換します。 次に、この文字列がメッセージテキストとして挿入されます。
# --instructions--
`GET` リクエストを作成して freeCodeCamp Cat Photo API に送信するために、コードを更新してください。 次に、`Get Message` ボタンをクリックしてください。 AJAX 関数により、テキスト `The message will go here` が、API からの生の JSON 出力に置き換えられます。
# --hints--
新しい `XMLHttpRequest` を作成する必要があります。
```js
assert(code.match(/new\s+?XMLHttpRequest\(\s*?\)/g));
```
`open` メソッドを使用して、freeCodeCamp Cat Photo API への `GET` リクエストを初期化する必要があります。
```js
assert(
code.match(
/\.open\(\s*?('|")GET\1\s*?,\s*?('|")\/json\/cats\.json\2\s*?,\s*?true\s*?\)/g
)
);
```
`send` メソッドを使用してリクエストを送信する必要があります。
```js
assert(code.match(/\.send\(\s*\)/g));
```
`onload` イベントハンドラを関数に設定する必要があります。
```js
assert(
code.match(/\.onload\s*=\s*(function|\(\s*?\))\s*?(\(\s*?\)|\=\>)\s*?{/g)
);
```
`JSON.parse` メソッドを使用して `responseText` を解析する必要があります 。
```js
assert(code.match(/JSON\s*\.parse\(\s*.*\.responseText\s*\)/g));
```
`message` クラスを持つ要素を取得し、内部の HTML を JSON データの文字列に変更する必要があります。
```js
assert(
code.match(
/document\s*\.getElementsByClassName\(\s*?('|")message\1\s*?\)\[0\]\s*\.innerHTML\s*?=\s*?JSON\.stringify\(.+?\)/g
)
);
```
# --seed--
## --seed-contents--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('getMessage').onclick = function(){
// Add your code below this line
// Add your code above this line
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message box">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```
# --solutions--
```html
<script>
document.addEventListener('DOMContentLoaded',function(){
document.getElementById('getMessage').onclick = function(){
const req = new XMLHttpRequest();
req.open('GET', '/json/cats.json', true);
req.send();
req.onload = () => {
const json = JSON.parse(req.responseText);
document.getElementsByClassName('message')[0].innerHTML = JSON.stringify(json);
};
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message box">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```

View File

@ -0,0 +1,140 @@
---
id: 587d7fad367417b2b2512be1
title: onclick プロパティを使用してクリックイベントを JavaScript で処理する
challengeType: 6
forumTopicId: 301503
dashedName: handle-click-events-with-javascript-using-the-onclick-property
---
# --description--
ページの読み込みが完了するまで、コードが実行されないようにしましょう。 その目的のために、`DOMContentLoaded` という名前のドキュメントに JavaScript イベントをアタッチすることができます。 これを行うコードを次に示します。
```js
document.addEventListener('DOMContentLoaded', function() {
});
```
`DOMContentLoaded` 関数の中に入るイベントハンドラを実装できます。 `getMessage` という id を持つ要素をユーザーがクリックしたときにトリガーする `onclick` イベントハンドラを実装するには、次のコードを追加します。
```js
document.getElementById('getMessage').onclick = function(){};
```
# --instructions--
`getMessage` という id を持つ要素に対する `DOMContentLoaded` 関数の中に、クリックイベントハンドラを追加してください。
# --hints--
`document.getElementById` メソッドを使用して `getMessage` 要素を選択する必要があります。
```js
assert(code.match(/document\s*\.getElementById\(\s*?('|")getMessage\1\s*?\)/g));
```
`onclick` イベントハンドラを追加する必要があります。
```js
assert(typeof document.getElementById('getMessage').onclick === 'function');
```
# --seed--
## --seed-contents--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
// Add your code below this line
// Add your code above this line
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message box">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```
# --solutions--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
// Add your code below this line
document.getElementById('getMessage').onclick = function(){ };
// Add your code above this line
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message box">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```

View File

@ -0,0 +1,206 @@
---
id: 587d7faf367417b2b2512be9
title: JavaScript の XMLHttpRequest メソッドでデータをポストする
challengeType: 6
forumTopicId: 301504
dashedName: post-data-with-the-javascript-xmlhttprequest-method
---
# --description--
以前の例では、外部リソースからデータを受け取りました。 そのリソースが AJAX リクエストをサポートし、あなたが URL を知っていれば、外部リソースにデータを送信することもできます。
JavaScriptの `XMLHttpRequest` メソッドは、データをサーバーにポストするためにも使用されます。 次に例を示します。
```js
const xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 201){
const serverResponse = JSON.parse(xhr.response);
document.getElementsByClassName('message')[0].textContent = serverResponse.userName + serverResponse.suffix;
}
};
const body = JSON.stringify({ userName: userName, suffix: ' loves cats!' });
xhr.send(body);
```
これらのメソッドのいくつかは見覚えがあるでしょう。 このコードでは、`open` メソッドがリクエストを、与えられた外部リソースの URL への `POST` として初期化し、`true` ブール値を使用してそれを非同期にしています。 `setRequestHeader` メソッドは HTTP リクエストヘッダーの値を設定します。これには送信者とリクエストに関する情報が格納されています。 それは `open` メソッドより後、かつ、`send` メソッドより前に呼び出される必要があります。 この 2 つのパラメータは、ヘッダーの名前と、ヘッダーのボディーとして設定する値です。 次に、`onreadystatechange` イベントリスナーがリクエスト状態の変化を処理します。 `readyState``4` は操作の完了を意味し、`status``201` はリクエストが成功したことを意味します。 ドキュメントの HTML を更新することができます。 最後に、`send` メソッドが `body` 値を持つリクエストを送信し、ユーザーによって `userName` キーが `input` フィールドに与えられます。
# --instructions--
API エンドポイントに `POST` リクエストを行うように、コードを更新してください。 次に、入力フィールドに自分の名前を入力し、`Send Message` をクリックしてください。 AJAX 関数によって、`Reply from Server will be here.` がサーバーからのデータに置き換えられる必要があります。 自分の名前に `loves cats` というテキストが追加されたものが表示されるように、レスポンスをフォーマットしてください。
# --hints--
新しい `XMLHttpRequest` を作成する必要があります。
```js
assert(code.match(/new\s+?XMLHttpRequest\(\s*?\)/g));
```
`open` メソッドを使用してサーバーへの `POST` リクエストを初期化する必要があります。
```js
assert(code.match(/\.open\(\s*?('|")POST\1\s*?,\s*?url\s*?,\s*?true\s*?\)/g));
```
`setRequestHeader` メソッドを使用する必要があります。
```js
assert(
code.match(
/\.setRequestHeader\(\s*?('|")Content-Type\1\s*?,\s*?('|")application\/json;\s*charset=UTF-8\2\s*?\)/g
)
);
```
`onreadystatechange` イベントハンドラを関数に設定する必要があります。
```js
assert(code.match(/\.onreadystatechange\s*?=/g));
```
`message` クラスを持つ要素を取得し、`textContent``userName loves cats` に変更する必要があります。
```js
assert(
code.match(
/document\.getElementsByClassName\(\s*?('|")message\1\s*?\)\[0\]\.textContent\s*?=\s*?.+?\.userName\s*?\+\s*?.+?\.suffix/g
)
);
```
`send` メソッドを使用する必要があります。
```js
assert(code.match(/\.send\(\s*?body\s*?\)/g));
```
# --seed--
## --seed-contents--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('sendMessage').onclick = function(){
const userName = document.getElementById('name').value;
const url = 'https://jsonplaceholder.typicode.com/posts';
// Add your code below this line
// Add your code above this line
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Friends</h1>
<p class="message box">
Reply from Server will be here
</p>
<p>
<label for="name">Your name:
<input type="text" id="name"/>
</label>
<button id="sendMessage">
Send Message
</button>
</p>
```
# --solutions--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('sendMessage').onclick = function(){
const userName = document.getElementById('name').value;
const url = 'https://jsonplaceholder.typicode.com/posts';
// Add your code below this line
const xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 201){
const serverResponse = JSON.parse(xhr.response);
document.getElementsByClassName('message')[0].textContent = serverResponse.userName + serverResponse.suffix;
}
};
const body = JSON.stringify({ userName: userName, suffix: ' loves cats!' });
xhr.send(body);
// Add your code above this line
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Friends</h1>
<p class="message">
Reply from Server will be here
</p>
<p>
<label for="name">Your name:
<input type="text" id="name"/>
</label>
<button id="sendMessage">
Send Message
</button>
</p>
```

View File

@ -0,0 +1,171 @@
---
id: 587d7fae367417b2b2512be7
title: JSON の事前フィルタリングにより必要なデータを取得する
challengeType: 6
forumTopicId: 18257
dashedName: pre-filter-json-to-get-the-data-you-need
---
# --description--
FreeCodeCamp Cat Photo API から取得した猫の写真をすべてレンダリングしたいわけではない場合、写真をループ処理する前に JSON の事前フィルタリングを行えます。
JSON データが配列に格納されている場合、`filter` メソッドを使用して、`id` キーの値が `1` である猫を除外することができます。
これを行うコードを次に示します。
```js
json = json.filter(function(val) {
return (val.id !== 1);
});
```
# --instructions--
JSON データに `filter` を実行して `id` 値が `1` である猫を除去するように、コードを追加してください。
# --hints--
`filter` メソッドを使用する必要があります。
```js
assert(code.match(/json\.filter/g));
```
# --seed--
## --seed-contents--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('getMessage').onclick = function(){
const req = new XMLHttpRequest();
req.open("GET",'/json/cats.json', true);
req.send();
req.onload=function(){
let json = JSON.parse(req.responseText);
let html = "";
// Add your code below this line
// Add your code above this line
json.forEach(function(val) {
html += "<div class = 'cat'>"
html += "<img src = '" + val.imageLink + "' " + "alt='" + val.altText + "'>"
html += "</div>"
});
document.getElementsByClassName('message')[0].innerHTML = html;
};
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message box">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```
# --solutions--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('getMessage').onclick = function(){
const req = new XMLHttpRequest();
req.open("GET",'/json/cats.json', true);
req.send();
req.onload = function(){
let json = JSON.parse(req.responseText);
let html = "";
// Add your code below this line
json = json.filter(function(val) {
return (val.id !== 1);
});
// Add your code above this line
json.forEach(function(val) {
html += "<div class = 'cat'>"
html += "<img src = '" + val.imageLink + "' " + "alt='" + val.altText + "'>"
html += "</div>"
});
document.getElementsByClassName('message')[0].innerHTML = html;
};
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```

View File

@ -0,0 +1,163 @@
---
id: 587d7fae367417b2b2512be6
title: データソースからの画像をレンダリングする
challengeType: 6
forumTopicId: 18265
dashedName: render-images-from-data-sources
---
# --description--
直前のいくつかのチャレンジでは、JSON 配列内の各オブジェクトに、猫の画像の URL である値を持つ `imageLink` キーが含まれていました。
これらのオブジェクトをループ処理する場合、この `imageLink` プロパティを使用すれば `img` 要素にこの画像を表示できます。
これを行うコードを次に示します。
```js
html += "<img src = '" + val.imageLink + "' " + "alt='" + val.altText + "'>";
```
# --instructions--
`img` タグ内で `imageLink` プロパティと `altText` プロパティを使用するように、コードを追加してください。
# --hints--
`imageLink` プロパティを使用して画像を表示する必要があります 。
```js
assert(code.match(/val\.imageLink/g));
```
画像の `alt` 属性値には `altText` を使用する必要があります。
```js
assert(code.match(/val\.altText/g));
```
# --seed--
## --seed-contents--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('getMessage').onclick = function(){
const req=new XMLHttpRequest();
req.open("GET",'/json/cats.json',true);
req.send();
req.onload = function(){
const json = JSON.parse(req.responseText);
let html = "";
json.forEach(function(val) {
html += "<div class = 'cat'>";
// Add your code below this line
// Add your code above this line
html += "</div><br>";
});
document.getElementsByClassName('message')[0].innerHTML=html;
};
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message box">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```
# --solutions--
```html
<script>
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('getMessage').onclick = function(){
const req = new XMLHttpRequest();
req.open("GET",'/json/cats.json',true);
req.send();
req.onload = function(){
const json = JSON.parse(req.responseText);
let html = "";
json.forEach(function(val) {
html += "<div class = 'cat'>";
// Add your code below this line
html += "<img src = '" + val.imageLink + "' " + "alt='" + val.altText + "'>";
// Add your code above this line
html += "</div><br>";
});
document.getElementsByClassName('message')[0].innerHTML = html;
};
};
});
</script>
<style>
body {
text-align: center;
font-family: "Helvetica", sans-serif;
}
h1 {
font-size: 2em;
font-weight: bold;
}
.box {
border-radius: 5px;
background-color: #eee;
padding: 20px 5px;
}
button {
color: white;
background-color: #4791d0;
border-radius: 5px;
border: 1px solid #4791d0;
padding: 5px 10px 8px 10px;
}
button:hover {
background-color: #0F5897;
border: 1px solid #0F5897;
}
</style>
<h1>Cat Photo Finder</h1>
<p class="message">
The message will go here
</p>
<p>
<button id="getMessage">
Get Message
</button>
</p>
```