chore(i18n,learn): processed translations (#44851)
This commit is contained in:
@ -0,0 +1,89 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392ca
|
||||
title: インデックスによる配列データへのアクセス
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cBZQbTz'
|
||||
forumTopicId: 16158
|
||||
dashedName: access-array-data-with-indexes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<dfn>インデックス</dfn>を使用して配列内のデータにアクセスすることができます。
|
||||
|
||||
配列インデックスは文字列と同じくブラケット記法で記述します。異なっているのは、文字の代わりに配列内のエントリを指定する点です。 文字列と同様に、配列では <dfn>0 から始まる</dfn>インデックスを使用するため、配列の最初の要素のインデックスは `0` になります。
|
||||
|
||||
<br>
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
const array = [50, 60, 70];
|
||||
array[0];
|
||||
const data = array[1];
|
||||
```
|
||||
|
||||
ここで、`array[0]` は `50`、`data` の値は `60` となります。
|
||||
|
||||
**注:** 「`array [0]`」のように、配列名と角括弧 (ブラケット) の間にスペースを入れないでください。 JavaScript はこれを正しく処理できますが、このコードを読む他のプログラマーを混乱させる恐れがあります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`myData` という変数を作成し、ブラケット記法を使用して、`myArray` の最初の値と等しくなるように設定してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
変数 `myData` は、`myArray` の最初の値と等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
typeof myArray !== 'undefined' &&
|
||||
typeof myData !== 'undefined' &&
|
||||
myArray[0] === myData
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
ブラケット記法を使用して、変数 `myArray` 内のデータにアクセスする必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (code.match(/\s*=\s*myArray\[0\]/g)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined" && typeof myData !== "undefined"){(function(y,z){return 'myArray = ' + JSON.stringify(y) + ', myData = ' + JSON.stringify(z);})(myArray, myData);}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const myArray = [50, 60, 70];
|
||||
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [50, 60, 70];
|
||||
const myData = myArray[0];
|
||||
```
|
@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 56592a60ddddeae28f7aa8e1
|
||||
title: インデックスによる多次元配列へのアクセス
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ckND4Cq'
|
||||
forumTopicId: 16159
|
||||
dashedName: access-multi-dimensional-arrays-with-indexes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<dfn>多次元</dfn>配列は、*配列の配列*として考えることができます。 ブラケット (角括弧) を使用して配列にアクセスする場合、最初のブラケットのセットは、一番外側 (最初の階層) の配列の項目を参照します。 ブラケットを追加するたびに、その次の階層の項目を参照します。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
const arr = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
[7, 8, 9],
|
||||
[[10, 11, 12], 13, 14]
|
||||
];
|
||||
|
||||
arr[3];
|
||||
arr[3][0];
|
||||
arr[3][0][1];
|
||||
```
|
||||
|
||||
`arr[3]` は `[[10, 11, 12], 13, 14]`、`arr[3][0]` は `[10, 11, 12]`、`arr[3][0][1]` は `11` になります。
|
||||
|
||||
**注:** 「`array [0][0]`」のように、配列名と角括弧 (ブラケット) の間にスペースを入れないでください。また、こうした `array [0] [0]` という記述は使用できません。 JavaScript はこれを正しく処理できますが、このコードを読む他のプログラマーを混乱させる恐れがあります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
ブラケット記法を使用して、`myData` が `8` に等しくなるように、`myArray` から要素を取得してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myData` が `8` に等しくなるようにします。
|
||||
|
||||
```js
|
||||
assert(myData === 8);
|
||||
```
|
||||
|
||||
ブラケット記法を使用して、`myArray` から正しい値を読み取る必要があります。
|
||||
|
||||
```js
|
||||
assert(/myData=myArray\[2\]\[1\]/.test(__helpers.removeWhiteSpace(code)));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return "myData: " + myData + " myArray: " + JSON.stringify(myArray);})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const myArray = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
[7, 8, 9],
|
||||
[[10, 11, 12], 13, 14],
|
||||
];
|
||||
|
||||
const myData = myArray[0][0];
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [[10, 11, 12], 13, 14]];
|
||||
const myData = myArray[2][1];
|
||||
```
|
@ -0,0 +1,121 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244cd
|
||||
title: ネストされた配列へのアクセス
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cLeGDtZ'
|
||||
forumTopicId: 16160
|
||||
dashedName: accessing-nested-arrays
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
これまでの例で見てきたとおり、オブジェクトには、ネストされたオブジェクトとネストされた配列のどちらも含めることができます。 ネストされたオブジェクトへのアクセスと同様に、配列のブラケット記法のチェーンを使用して、ネストされた配列にアクセスできます。
|
||||
|
||||
次はネストされた配列にアクセスする方法の例です。
|
||||
|
||||
```js
|
||||
const ourPets = [
|
||||
{
|
||||
animalType: "cat",
|
||||
names: [
|
||||
"Meowzer",
|
||||
"Fluffy",
|
||||
"Kit-Cat"
|
||||
]
|
||||
},
|
||||
{
|
||||
animalType: "dog",
|
||||
names: [
|
||||
"Spot",
|
||||
"Bowser",
|
||||
"Frankie"
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
ourPets[0].names[1];
|
||||
ourPets[1].names[0];
|
||||
```
|
||||
|
||||
`ourPets[0].names[1]` は文字列 `Fluffy` になり、`ourPets[1].names[0]` は文字列 `Spot` になります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
ドット記法とブラケット記法を使用して、変数 `secondTree` が `myPlants` オブジェクトの `trees` リストの 2 番目のアイテムとなるように設定してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`secondTree` が文字列 `pine` に等しくなるようにします。
|
||||
|
||||
```js
|
||||
assert(secondTree === 'pine');
|
||||
```
|
||||
|
||||
ドット記法とブラケット記法を使用して `myPlants` にアクセスする必要があります。
|
||||
|
||||
```js
|
||||
assert(/=\s*myPlants\[1\].list\[1\]/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(x) {
|
||||
if(typeof x != 'undefined') {
|
||||
return "secondTree = " + x;
|
||||
}
|
||||
return "secondTree is undefined";
|
||||
})(secondTree);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const myPlants = [
|
||||
{
|
||||
type: "flowers",
|
||||
list: [
|
||||
"rose",
|
||||
"tulip",
|
||||
"dandelion"
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "trees",
|
||||
list: [
|
||||
"fir",
|
||||
"pine",
|
||||
"birch"
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
const secondTree = "";
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myPlants = [
|
||||
{
|
||||
type: "flowers",
|
||||
list: [
|
||||
"rose",
|
||||
"tulip",
|
||||
"dandelion"
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "trees",
|
||||
list: [
|
||||
"fir",
|
||||
"pine",
|
||||
"birch"
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
const secondTree = myPlants[1].list[1];
|
||||
```
|
@ -0,0 +1,100 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244cc
|
||||
title: ネストされたオブジェクトへのアクセス
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cRnRnfa'
|
||||
forumTopicId: 16161
|
||||
dashedName: accessing-nested-objects
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
オブジェクトの下位プロパティには、ドット記法またはブラケット記法によるチェーンでアクセスできます。
|
||||
|
||||
次はネストされたオブジェクトです。
|
||||
|
||||
```js
|
||||
const ourStorage = {
|
||||
"desk": {
|
||||
"drawer": "stapler"
|
||||
},
|
||||
"cabinet": {
|
||||
"top drawer": {
|
||||
"folder1": "a file",
|
||||
"folder2": "secrets"
|
||||
},
|
||||
"bottom drawer": "soda"
|
||||
}
|
||||
};
|
||||
|
||||
ourStorage.cabinet["top drawer"].folder2;
|
||||
ourStorage.desk.drawer;
|
||||
```
|
||||
|
||||
`ourStorage.cabinet["top drawer"].folder2` は文字列 `secrets`、`ourStorage.desk.drawer` は文字列 `stapler` となります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`myStorage` オブジェクトにアクセスし、`glove box` プロパティの内容を `gloveBoxContents` 変数に代入してください。 可能な限りすべてのプロパティにドット記法を使用し、それが使用できない場合はブラケット記法を使用してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`gloveBoxContents` が文字列 `maps` と等しくなるようにします。
|
||||
|
||||
```js
|
||||
assert(gloveBoxContents === 'maps');
|
||||
```
|
||||
|
||||
ドット記法とブラケット記法を使用して `myStorage` にアクセスする必要があります。
|
||||
|
||||
```js
|
||||
assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(x) {
|
||||
if(typeof x != 'undefined') {
|
||||
return "gloveBoxContents = " + x;
|
||||
}
|
||||
return "gloveBoxContents is undefined";
|
||||
})(gloveBoxContents);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const myStorage = {
|
||||
"car": {
|
||||
"inside": {
|
||||
"glove box": "maps",
|
||||
"passenger seat": "crumbs"
|
||||
},
|
||||
"outside": {
|
||||
"trunk": "jack"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const gloveBoxContents = undefined;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myStorage = {
|
||||
"car":{
|
||||
"inside":{
|
||||
"glove box":"maps",
|
||||
"passenger seat":"crumbs"
|
||||
},
|
||||
"outside":{
|
||||
"trunk":"jack"
|
||||
}
|
||||
}
|
||||
};
|
||||
const gloveBoxContents = myStorage.car.inside["glove box"];
|
||||
```
|
@ -0,0 +1,103 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c8
|
||||
title: ブラケット記法によるオブジェクトのプロパティへのアクセス
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cBvmEHP'
|
||||
forumTopicId: 16163
|
||||
dashedName: accessing-object-properties-with-bracket-notation
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
オブジェクトのプロパティにアクセスする 2 つ目の方法は、ブラケット記法 (`[]`) です。 アクセスしようとしているオブジェクトのプロパティの名前にスペースが含まれている場合は、ブラケット記法を使用する必要があります。
|
||||
|
||||
ただし、オブジェクトのプロパティにスペースが含まれていない場合もブラケット記法を使用できます。
|
||||
|
||||
次は、ブラケット記法を使用してオブジェクトのプロパティを読み取る例です。
|
||||
|
||||
```js
|
||||
const myObj = {
|
||||
"Space Name": "Kirk",
|
||||
"More Space": "Spock",
|
||||
"NoSpace": "USS Enterprise"
|
||||
};
|
||||
|
||||
myObj["Space Name"];
|
||||
myObj['More Space'];
|
||||
myObj["NoSpace"];
|
||||
```
|
||||
|
||||
`myObj["Space Name"]` は文字列 `Kirk`、`myObj['More Space']` は文字列 `Spock`、`myObj["NoSpace"]` は文字列 `USS Enterprise` となります。
|
||||
|
||||
スペースを含むプロパティ名には引用符 (シングルクォートまたはダブルクォート) が必要なことに注意してください。
|
||||
|
||||
# --instructions--
|
||||
|
||||
ブラケット記法を使用して、`testObj` のプロパティである `an entree` と `the drink` の値を読み取り、それぞれ `entreeValue` と `drinkValue` に割り当ててください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`entreeValue` は文字列である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof entreeValue === 'string');
|
||||
```
|
||||
|
||||
`entreeValue` の値は文字列 `hamburger`である必要があります。
|
||||
|
||||
```js
|
||||
assert(entreeValue === 'hamburger');
|
||||
```
|
||||
|
||||
`drinkValue` は文字列である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof drinkValue === 'string');
|
||||
```
|
||||
|
||||
`drinkValue` の値は文字列 `water`である必要があります。
|
||||
|
||||
```js
|
||||
assert(drinkValue === 'water');
|
||||
```
|
||||
|
||||
ブラケット記法を 2 回使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/testObj\s*?\[('|")[^'"]+\1\]/g).length > 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b) { return "entreeValue = '" + a + "', drinkValue = '" + b + "'"; })(entreeValue,drinkValue);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const testObj = {
|
||||
"an entree": "hamburger",
|
||||
"my side": "veggies",
|
||||
"the drink": "water"
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
const entreeValue = testObj; // Change this line
|
||||
const drinkValue = testObj; // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const testObj = {
|
||||
"an entree": "hamburger",
|
||||
"my side": "veggies",
|
||||
"the drink": "water"
|
||||
};
|
||||
const entreeValue = testObj["an entree"];
|
||||
const drinkValue = testObj['the drink'];
|
||||
```
|
@ -0,0 +1,100 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c7
|
||||
title: ドット記法によるオブジェクトのプロパティへのアクセス
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cGryJs8'
|
||||
forumTopicId: 16164
|
||||
dashedName: accessing-object-properties-with-dot-notation
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
配列の場合と同様に、オブジェクトのプロパティへのアクセス方法も、ドット記法 (`.`) とブラケット記法 (`[]`) の 2 通りがあります。
|
||||
|
||||
ドット記法は、アクセスしようとしているプロパティの名前があらかじめわかっている場合に使用します。
|
||||
|
||||
次は、ドット記法 (`.`) を使用してオブジェクトのプロパティを読み取る例です。
|
||||
|
||||
```js
|
||||
const myObj = {
|
||||
prop1: "val1",
|
||||
prop2: "val2"
|
||||
};
|
||||
|
||||
const prop1val = myObj.prop1;
|
||||
const prop2val = myObj.prop2;
|
||||
```
|
||||
|
||||
`prop1val` の値は文字列 `val1`、`prop2val` の値は文字列 `val2` となります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
ドット記法を使用して `testObj` のプロパティ値を取得してください。 変数 `hatValue` はオブジェクトのプロパティ `hat` と等しくなるように設定し、変数 `shirtValue` はオブジェクトのプロパティ `shirt` と等しくなるように設定してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`hatValue` は文字列である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof hatValue === 'string');
|
||||
```
|
||||
|
||||
`hatValue` の値は文字列 `ballcap`である必要があります。
|
||||
|
||||
```js
|
||||
assert(hatValue === 'ballcap');
|
||||
```
|
||||
|
||||
`shirtValue` は文字列である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof shirtValue === 'string');
|
||||
```
|
||||
|
||||
`shirtValue` の値は文字列 `jersey` である必要があります。
|
||||
|
||||
```js
|
||||
assert(shirtValue === 'jersey');
|
||||
```
|
||||
|
||||
ドット記法を 2 回使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/testObj\.\w+/g).length > 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b) { return "hatValue = '" + a + "', shirtValue = '" + b + "'"; })(hatValue,shirtValue);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const testObj = {
|
||||
"hat": "ballcap",
|
||||
"shirt": "jersey",
|
||||
"shoes": "cleats"
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
const hatValue = testObj; // Change this line
|
||||
const shirtValue = testObj; // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const testObj = {
|
||||
"hat": "ballcap",
|
||||
"shirt": "jersey",
|
||||
"shoes": "cleats"
|
||||
};
|
||||
|
||||
const hatValue = testObj.hat;
|
||||
const shirtValue = testObj.shirt;
|
||||
```
|
@ -0,0 +1,125 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c9
|
||||
title: 変数によるオブジェクトのプロパティへのアクセス
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cnQyKur'
|
||||
forumTopicId: 16165
|
||||
dashedName: accessing-object-properties-with-variables
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
オブジェクトに対するブラケット記法のもう一つの用例として、プロパティを変数の値として保存し、アクセスする方法があります。 この用法は、オブジェクトのプロパティを繰り返し処理する場合や、ルックアップテーブルにアクセスする場合にとても便利です。
|
||||
|
||||
次は変数を使用してプロパティにアクセスする例です。
|
||||
|
||||
```js
|
||||
const dogs = {
|
||||
Fido: "Mutt",
|
||||
Hunter: "Doberman",
|
||||
Snoopie: "Beagle"
|
||||
};
|
||||
|
||||
const myDog = "Hunter";
|
||||
const myBreed = dogs[myDog];
|
||||
console.log(myBreed);
|
||||
```
|
||||
|
||||
コンソールには文字列 `Doberman` が表示されます。
|
||||
|
||||
この用法の別の例として、プログラムの実行中にプロパティの名前を動的に取得することができます。次に例を示します。
|
||||
|
||||
```js
|
||||
const someObj = {
|
||||
propName: "John"
|
||||
};
|
||||
|
||||
function propPrefix(str) {
|
||||
const s = "prop";
|
||||
return s + str;
|
||||
}
|
||||
|
||||
const someProp = propPrefix("Name");
|
||||
console.log(someObj[someProp]);
|
||||
```
|
||||
|
||||
`someProp` の値は文字列 `propName` となり、文字列 `John` がコンソールに表示されます。
|
||||
|
||||
変数を使用してプロパティにアクセスする場合、変数名を引用符で*囲まない*ことに注意してください。使用するのは変数の*値*であって、*名前*ではありません。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`playerNumber` 変数を `16` に設定してください。 次に、変数を使用してプレイヤーの名前を参照し、それを `player` に割り当ててください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`playerNumber` は数値である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof playerNumber === 'number');
|
||||
```
|
||||
|
||||
変数 `player` は文字列である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof player === 'string');
|
||||
```
|
||||
|
||||
`player` の値は文字列 `Montana` である必要があります。
|
||||
|
||||
```js
|
||||
assert(player === 'Montana');
|
||||
```
|
||||
|
||||
ブラケット記法を使用して `testObj` にアクセスする必要があります。
|
||||
|
||||
```js
|
||||
assert(/testObj\s*?\[.*?\]/.test(code));
|
||||
```
|
||||
|
||||
値 `Montana` を、変数 `player` に直接、割り当てることはできません。
|
||||
|
||||
```js
|
||||
assert(!code.match(/player\s*=\s*"|\'\s*Montana\s*"|\'\s*;/gi));
|
||||
```
|
||||
|
||||
ブラケット記法で、変数`playerNumber` を使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof player !== "undefined"){(function(v){return v;})(player);}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const testObj = {
|
||||
12: "Namath",
|
||||
16: "Montana",
|
||||
19: "Unitas"
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
const playerNumber = 42; // Change this line
|
||||
const player = testObj; // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const testObj = {
|
||||
12: "Namath",
|
||||
16: "Montana",
|
||||
19: "Unitas"
|
||||
};
|
||||
const playerNumber = 16;
|
||||
const player = testObj[playerNumber];
|
||||
```
|
@ -0,0 +1,90 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d2
|
||||
title: JavaScript オブジェクトへの新しいプロパティの追加
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cQe38UD'
|
||||
forumTopicId: 301169
|
||||
dashedName: add-new-properties-to-a-javascript-object
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
プロパティの変更と同じ方法で、既存の JavaScript オブジェクトに新しいプロパティを追加することができます。
|
||||
|
||||
`ourDog` に `bark` プロパティを追加する方法を次に示します。
|
||||
|
||||
```js
|
||||
ourDog.bark = "bow-wow";
|
||||
```
|
||||
|
||||
または
|
||||
|
||||
```js
|
||||
ourDog["bark"] = "bow-wow";
|
||||
```
|
||||
|
||||
これで `ourDog.bark` を評価すると、`bow-wow` という鳴き声が得られます。
|
||||
|
||||
例:
|
||||
|
||||
```js
|
||||
const ourDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"]
|
||||
};
|
||||
|
||||
ourDog.bark = "bow-wow";
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
`myDog` に `bark` プロパティを追加して、"woof" などの犬の鳴き声を設定してください。 ドット記法またはブラケット記法のいずれも使用できます。
|
||||
|
||||
# --hints--
|
||||
|
||||
プロパティ `bark` を `myDog` に追加する必要があります。
|
||||
|
||||
```js
|
||||
assert(myDog.bark !== undefined);
|
||||
```
|
||||
|
||||
`myDog` の初期化に `bark`を追加しないでください。
|
||||
|
||||
```js
|
||||
assert(!/bark[^\n]:/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const myDog = {
|
||||
"name": "Happy Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"]
|
||||
};
|
||||
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myDog = {
|
||||
"name": "Happy Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"]
|
||||
};
|
||||
myDog.bark = "Woof Woof";
|
||||
```
|
@ -0,0 +1,62 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb3bdef
|
||||
title: JavaScript で 2 つの数値を足し算する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cM2KBAG'
|
||||
forumTopicId: 16650
|
||||
dashedName: add-two-numbers-with-javascript
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`Number` は数値データを表す JavaScript のデータ型です。
|
||||
|
||||
では、JavaScript で 2 つの数字の足し算をしてみましょう。
|
||||
|
||||
JavaScript では `+` 記号を加算演算子として 2 つの数字の間に置いて使用します。
|
||||
|
||||
**例:**
|
||||
|
||||
```js
|
||||
const myVar = 5 + 10;
|
||||
```
|
||||
|
||||
`myVar` の値はいま `15` になりました。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`0` を、和が `20` と等しくなるように変更してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`sum` は `20` と等しくなければいけません。
|
||||
|
||||
```js
|
||||
assert(sum === 20);
|
||||
```
|
||||
|
||||
`+` 演算子を使用してください。
|
||||
|
||||
```js
|
||||
assert(/\+/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'sum = '+z;})(sum);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const sum = 10 + 0;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const sum = 10 + 10;
|
||||
```
|
@ -0,0 +1,128 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244de
|
||||
title: switch ステートメントへのデフォルトオプションの追加
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c3JvVfg'
|
||||
forumTopicId: 16653
|
||||
dashedName: adding-a-default-option-in-switch-statements
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`switch` ステートメントでは、可能性のあるすべての値を `case` ステートメントとして指定することができない場合があります。 代わりに、`case` に一致するものが見つからない場合に実行される `default` を追加することができます。 これは、`if/else` チェーンの最後の `else` のようなものだと考えてください。
|
||||
|
||||
`default` ステートメントは最後の case として記述してください。
|
||||
|
||||
```js
|
||||
switch (num) {
|
||||
case value1:
|
||||
statement1;
|
||||
break;
|
||||
case value2:
|
||||
statement2;
|
||||
break;
|
||||
...
|
||||
default:
|
||||
defaultStatement;
|
||||
break;
|
||||
}
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
次の条件ごとに `answer` を設定した switch ステートメントを記述してください。
|
||||
`a` - `apple`
|
||||
`b` - `bird`
|
||||
`c` - `cat`
|
||||
`default` - `stuff`
|
||||
|
||||
# --hints--
|
||||
|
||||
`switchOfStuff("a")` は文字列 `apple` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(switchOfStuff('a') === 'apple');
|
||||
```
|
||||
|
||||
`switchOfStuff("b")` は文字列 `bird` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(switchOfStuff('b') === 'bird');
|
||||
```
|
||||
|
||||
`switchOfStuff("c")` は文字列 `cat` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(switchOfStuff('c') === 'cat');
|
||||
```
|
||||
|
||||
`switchOfStuff("d")` は文字列 `stuff` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(switchOfStuff('d') === 'stuff');
|
||||
```
|
||||
|
||||
`switchOfStuff(4)` は文字列 `stuff` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(switchOfStuff(4) === 'stuff');
|
||||
```
|
||||
|
||||
`if` または `else` ステートメントを使用することはできません。
|
||||
|
||||
```js
|
||||
assert(!/else/g.test(code) || !/if/g.test(code));
|
||||
```
|
||||
|
||||
`default` ステートメントを使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(switchOfStuff('string-to-trigger-default-case') === 'stuff');
|
||||
```
|
||||
|
||||
少なくとも 3 つの `break` ステートメントを含める必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/break/g).length > 2);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function switchOfStuff(val) {
|
||||
let answer = "";
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
return answer;
|
||||
}
|
||||
|
||||
switchOfStuff(1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function switchOfStuff(val) {
|
||||
let answer = "";
|
||||
|
||||
switch(val) {
|
||||
case "a":
|
||||
answer = "apple";
|
||||
break;
|
||||
case "b":
|
||||
answer = "bird";
|
||||
break;
|
||||
case "c":
|
||||
answer = "cat";
|
||||
break;
|
||||
default:
|
||||
answer = "stuff";
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
```
|
@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ed
|
||||
title: 文字列への変数の連結
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cbQmZfa'
|
||||
forumTopicId: 16656
|
||||
dashedName: appending-variables-to-strings
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
複数行の文字列<dfn>リテラル</dfn>から文字列を作成することができますが、それと同じように、加算代入 (`+=`) 演算子を使用して変数を文字列に連結することができます。
|
||||
|
||||
例:
|
||||
|
||||
```js
|
||||
const anAdjective = "awesome!";
|
||||
let ourStr = "freeCodeCamp is ";
|
||||
ourStr += anAdjective;
|
||||
```
|
||||
|
||||
`ourStr` の値は `freeCodeCamp is awesome!` となります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`someAdjective` に 3 文字以上の文字列を設定し、`+=` 演算子を使用して `myStr` に連結してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`someAdjective` には 3 文字以上の長さの文字列を設定する必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof someAdjective !== 'undefined' && someAdjective.length > 2);
|
||||
```
|
||||
|
||||
`+=` 演算子を使用して、`someAdjective` を `myStr` に連結する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/myStr\s*\+=\s*someAdjective\s*/).length > 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){
|
||||
var output = [];
|
||||
if(typeof someAdjective === 'string') {
|
||||
output.push('someAdjective = "' + someAdjective + '"');
|
||||
} else {
|
||||
output.push('someAdjective is not a string');
|
||||
}
|
||||
if(typeof myStr === 'string') {
|
||||
output.push('myStr = "' + myStr + '"');
|
||||
} else {
|
||||
output.push('myStr is not a string');
|
||||
}
|
||||
return output.join('\n');
|
||||
})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Change code below this line
|
||||
const someAdjective = "";
|
||||
let myStr = "Learning to code is ";
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const someAdjective = "neat";
|
||||
let myStr = "Learning to code is ";
|
||||
myStr += someAdjective;
|
||||
```
|
@ -0,0 +1,86 @@
|
||||
---
|
||||
id: 5ee127a03c3b35dd45426493
|
||||
title: ある変数の値を別の変数に割り当てる
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 418265
|
||||
dashedName: assigning-the-value-of-one-variable-to-another
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<dfn>代入</dfn>演算子を使ってある値を変数に割り当てたあと、その変数の値を<dfn>代入</dfn>演算子を使って更に別の変数へ割り当てることもできます。
|
||||
|
||||
```js
|
||||
var myVar;
|
||||
myVar = 5;
|
||||
var myNum;
|
||||
myNum = myVar;
|
||||
```
|
||||
|
||||
上記は値を持たない `myVar` 変数を宣言したあと、値 `5` を代入します。 次に、値を持たない `myNum` という名前の変数を宣言します。 そして、`myVar` の内容 (値 `5`) が変数 `myNum` に割り当てられます。 最終的に、`myNum` の値も `5` になりました。
|
||||
|
||||
# --instructions--
|
||||
|
||||
変数 `a` の内容を変数 `b` に割り当ててください。
|
||||
|
||||
# --hints--
|
||||
|
||||
指定のコメントより上にあるコードを変更しないでください。
|
||||
|
||||
```js
|
||||
assert(/var a;/.test(code) && /a = 7;/.test(code) && /var b;/.test(code));
|
||||
```
|
||||
|
||||
`b` の値は `7` でなければなりません。
|
||||
|
||||
```js
|
||||
assert(typeof b === 'number' && b === 7);
|
||||
```
|
||||
|
||||
`a` は `=` を使って `b` に割り当てられなければなりません。
|
||||
|
||||
```js
|
||||
assert(/b\s*=\s*a\s*/g.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
if (typeof a != 'undefined') {
|
||||
a = undefined;
|
||||
}
|
||||
if (typeof b != 'undefined') {
|
||||
b = undefined;
|
||||
}
|
||||
```
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a, b) {
|
||||
return 'a = ' + a + ', b = ' + b;
|
||||
})(a, b);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var a;
|
||||
a = 7;
|
||||
var b;
|
||||
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a;
|
||||
a = 7;
|
||||
var b;
|
||||
b = a;
|
||||
```
|
@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c3
|
||||
title: 戻り値の代入
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ce2pEtB'
|
||||
forumTopicId: 16658
|
||||
dashedName: assignment-with-a-returned-value
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
[代入演算子による値の格納](/learn/javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator)で説明したように、等号の右側の部分はすべて、値が代入される前に解決されます。 つまり、関数の戻り値を受け取って変数に代入することができます。
|
||||
|
||||
2 つの数値を足し算する関数 `sum` があらかじめ定義されているとします。次の
|
||||
|
||||
```js
|
||||
ourSum = sum(5, 12);
|
||||
```
|
||||
|
||||
は `sum` 関数を呼び出します。関数は `17` という値を返し、その値が `ourSum` 変数に代入されます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`7` を引数に取る `processArg` 関数を呼び出し、その戻り値を変数 `processed` に代入してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`processed` の値は `2` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(processed === 2);
|
||||
```
|
||||
|
||||
`processArg` を `processed` に代入する必要があります。
|
||||
|
||||
```js
|
||||
assert(/processed\s*=\s*processArg\(\s*7\s*\)/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){return "processed = " + processed})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
let processed = 0;
|
||||
|
||||
function processArg(num) {
|
||||
return (num + 3) / 5;
|
||||
}
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var processed = 0;
|
||||
|
||||
function processArg(num) {
|
||||
return (num + 3) / 5;
|
||||
}
|
||||
|
||||
processed = processArg(7);
|
||||
```
|
@ -0,0 +1,159 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d0
|
||||
title: JavaScript オブジェクトの作成
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cWGkbtd'
|
||||
forumTopicId: 16769
|
||||
dashedName: build-javascript-objects
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
これまでに `object` (オブジェクト) という言葉を耳にしたことがあるでしょうか。
|
||||
|
||||
オブジェクトは `arrays` (配列) に似ていますが、配列とは異なり、インデックスを使用してデータにアクセスしたりデータを変更したりする代わりに、`properties` (プロパティ) と呼ばれるものを通じてオブジェクトのデータにアクセスします。
|
||||
|
||||
オブジェクトはデータを構造化して格納する場合に便利であり、たとえば猫などの現実世界の物体 (オブジェクト) を表現することができます。
|
||||
|
||||
次は猫のオブジェクトの例です。
|
||||
|
||||
```js
|
||||
const cat = {
|
||||
"name": "Whiskers",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"enemies": ["Water", "Dogs"]
|
||||
};
|
||||
```
|
||||
|
||||
この例では、すべてのプロパティを `name`、`legs`、`tails` のように文字列として格納しています。 しかし、数値もプロパティとして使用することが可能です。 次のように、1 つの単語の文字列プロパティの場合は引用符を省略することもできます。
|
||||
|
||||
```js
|
||||
const anotherObject = {
|
||||
make: "Ford",
|
||||
5: "five",
|
||||
"model": "focus"
|
||||
};
|
||||
```
|
||||
|
||||
ただし、オブジェクトに文字列ではないプロパティがある場合、JavaScript では自動的に文字列に型変換されます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
犬を表現する `myDog` というオブジェクトを作成してください。このオブジェクトには `name` (文字列)、`legs`、`tails`、`friends` というプロパティを含めてください。
|
||||
|
||||
オブジェクトのプロパティには任意の値を設定できます。ただし、`name` は文字列、`legs` と `tails` は数値、`friends` は配列とします。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myDog` はプロパティ `name` を含み、それは `string` である必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (z) {
|
||||
if (
|
||||
z.hasOwnProperty('name') &&
|
||||
z.name !== undefined &&
|
||||
typeof z.name === 'string'
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
`myDog` はプロパティ `legs` を含み、それは `number` である必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (z) {
|
||||
if (
|
||||
z.hasOwnProperty('legs') &&
|
||||
z.legs !== undefined &&
|
||||
typeof z.legs === 'number'
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
`myDog` はプロパティ `tails` を含み、それは `number` である必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (z) {
|
||||
if (
|
||||
z.hasOwnProperty('tails') &&
|
||||
z.tails !== undefined &&
|
||||
typeof z.tails === 'number'
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
`myDog` はプロパティ `friends` を含み、それは `array` である必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (z) {
|
||||
if (
|
||||
z.hasOwnProperty('friends') &&
|
||||
z.friends !== undefined &&
|
||||
Array.isArray(z.friends)
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
`myDog` には指定されたすべてのプロパティのみを含めてください。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (z) {
|
||||
return Object.keys(z).length === 4;
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const myDog = {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"]
|
||||
};
|
||||
```
|
@ -0,0 +1,149 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244dc
|
||||
title: if/else ステートメントのチェーン
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/caeJgsw'
|
||||
forumTopicId: 16772
|
||||
dashedName: chaining-if-else-statements
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`if/else` ステートメントをチェーンとしてつなげて複雑なロジックを記述することができます。 次は、`if` / `else if` ステートメントを複数つなげた<dfn>擬似コード</dfn>です。
|
||||
|
||||
```js
|
||||
if (condition1) {
|
||||
statement1
|
||||
} else if (condition2) {
|
||||
statement2
|
||||
} else if (condition3) {
|
||||
statement3
|
||||
. . .
|
||||
} else {
|
||||
statementN
|
||||
}
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
次の条件を満たすように `if`/`else if` ステートメントをつなげて記述してください。
|
||||
|
||||
`num < 5` - `Tiny` を返す
|
||||
`num < 10` - `Small` を返す
|
||||
`num < 15` - `Medium` を返す
|
||||
`num < 20` - `Large` を返す
|
||||
`num >= 20` - `Huge` を返す
|
||||
|
||||
# --hints--
|
||||
|
||||
少なくとも 4 つの `else` ステートメントが必要です。
|
||||
|
||||
```js
|
||||
assert(code.match(/else/g).length > 3);
|
||||
```
|
||||
|
||||
少なくとも 4 つの `if` ステートメントが必要です。
|
||||
|
||||
```js
|
||||
assert(code.match(/if/g).length > 3);
|
||||
```
|
||||
|
||||
少なくとも 1 つの `return` ステートメントが必要です。
|
||||
|
||||
```js
|
||||
assert(code.match(/return/g).length >= 1);
|
||||
```
|
||||
|
||||
`testSize(0)` は文字列 `Tiny` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testSize(0) === 'Tiny');
|
||||
```
|
||||
|
||||
`testSize(4)` は文字列 `Tiny` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testSize(4) === 'Tiny');
|
||||
```
|
||||
|
||||
`testSize(5)` は文字列 `Small` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testSize(5) === 'Small');
|
||||
```
|
||||
|
||||
`testSize(8)` は文字列 `Small` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testSize(8) === 'Small');
|
||||
```
|
||||
|
||||
`testSize(10)` は文字列 `Medium` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testSize(10) === 'Medium');
|
||||
```
|
||||
|
||||
`testSize(14)` は文字列 `Medium` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testSize(14) === 'Medium');
|
||||
```
|
||||
|
||||
`testSize(15)` は文字列 `Large` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testSize(15) === 'Large');
|
||||
```
|
||||
|
||||
`testSize(17)` は文字列 `Large` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testSize(17) === 'Large');
|
||||
```
|
||||
|
||||
`testSize(20)` は文字列 `Huge` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testSize(20) === 'Huge');
|
||||
```
|
||||
|
||||
`testSize(25)` は文字列 `Huge` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testSize(25) === 'Huge');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testSize(num) {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
return "Change Me";
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
testSize(7);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testSize(num) {
|
||||
if (num < 5) {
|
||||
return "Tiny";
|
||||
} else if (num < 10) {
|
||||
return "Small";
|
||||
} else if (num < 15) {
|
||||
return "Medium";
|
||||
} else if (num < 20) {
|
||||
return "Large";
|
||||
} else {
|
||||
return "Huge";
|
||||
}
|
||||
}
|
||||
```
|
@ -0,0 +1,63 @@
|
||||
---
|
||||
id: bd7123c9c441eddfaeb4bdef
|
||||
title: JavaScript コードにコメントする
|
||||
challengeType: 1
|
||||
removeComments: false
|
||||
videoUrl: 'https://scrimba.com/c/c7ynnTp'
|
||||
forumTopicId: 16783
|
||||
dashedName: comment-your-javascript-code
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
コメントとは、JavaScript が意図的にコードを無視する行です。 コメントは、そのコードが何をするかを後で自分自身や他の人が把握するためにメモを残すために最適な方法です。
|
||||
|
||||
JavaScript でコメントを書く方法は 2 つあります。
|
||||
|
||||
`//` を使用すると、JavaScript は現在の行の残りのテキストを無視するようになります。 これはインラインコメントです。
|
||||
|
||||
```js
|
||||
// This is an in-line comment.
|
||||
```
|
||||
|
||||
`/*` で始まり、`*/` で終わる複数行のコメントも作成できます。 下記は複数行コメントです。
|
||||
|
||||
```js
|
||||
/* This is a
|
||||
multi-line comment */
|
||||
```
|
||||
|
||||
**注:** コードを書く際、部分的なコードが持つ機能を明確にするために、定期的にコメントを追加すると良いでしょう。 良いコメントをすることは、自分のコードの意図を伝えるのに役立ちます。他の人のため*だけでなく*未来の自分自身のためにも。
|
||||
|
||||
# --instructions--
|
||||
|
||||
それぞれのタイプのコメントを作成してみてください。
|
||||
|
||||
# --hints--
|
||||
|
||||
5 文字以上の `//` スタイルのコメントを作成する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/(\/\/)...../g));
|
||||
```
|
||||
|
||||
5 文字以上の `/* */` スタイルのコメントを作成する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/(\/\*)([^\/]{5,})(?=\*\/)/gm));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// Fake Comment
|
||||
/* Another Comment */
|
||||
```
|
@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d0
|
||||
title: 等価演算子による比較
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cKyVMAL'
|
||||
forumTopicId: 16784
|
||||
dashedName: comparison-with-the-equality-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript には多くの<dfn>比較演算子</dfn>があります。 これらの演算子はすべてブール値の `true` または `false` を返します。
|
||||
|
||||
最も基本的な演算子は等価演算子 `==` です。 等価演算子は 2 つの値を比較し、等価な場合は `true` を、そうでない場合は `false` を返します。 等価は代入 (`=`) とは異なることに注意してください。代入は演算子の右側の値を左側の変数に割り当てるものです。
|
||||
|
||||
```js
|
||||
function equalityTest(myVal) {
|
||||
if (myVal == 10) {
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
```
|
||||
|
||||
`myVal` が `10` と等しい場合には、等価演算子は `true` を返し、中括弧内のコードが実行されて、関数は `Equal` を返します。 それ以外の場合、関数は `Not Equal` を返します。 JavaScript では、比較する 2 つのデータの<dfn>データ型</dfn>が異なる場合 (たとえば `numbers` と `strings`)、必ず一方の型が他方の型に変換されます。 これを「型強制」と呼びます。 これが行われることで、次のような値の比較が可能になります。
|
||||
|
||||
```js
|
||||
1 == 1
|
||||
1 == 2
|
||||
1 == '1'
|
||||
"3" == 3
|
||||
```
|
||||
|
||||
これらの式は上から順に、`true`、`false`、`true`、`true` と評価されます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`val` が `12` と等しい場合に関数が文字列 `Equal` を返すように、指定された行に等価演算子を追加してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`testEqual(10)` は文字列 `Not Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testEqual(10) === 'Not Equal');
|
||||
```
|
||||
|
||||
`testEqual(12)` は文字列 `Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testEqual(12) === 'Equal');
|
||||
```
|
||||
|
||||
`testEqual("12")` は文字列 `Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testEqual('12') === 'Equal');
|
||||
```
|
||||
|
||||
`==` 演算子を使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/==/g) && !code.match(/===/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
|
||||
testEqual(10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testEqual(val) {
|
||||
if (val == 12) {
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
```
|
@ -0,0 +1,113 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d4
|
||||
title: 大なり演算子による比較
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cp6GbH4'
|
||||
forumTopicId: 16786
|
||||
dashedName: comparison-with-the-greater-than-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
大なり演算子 (`>`) は、2 つの数値の値を比較します。 左の数値が右の数値よりも大きい場合は、`true` を返します。 それ以外の場合は、`false` を返します。
|
||||
|
||||
等価演算子と同様に、大なり演算子でも比較時に値のデータ型が変換されます。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
5 > 3
|
||||
7 > '3'
|
||||
2 > 3
|
||||
'1' > 9
|
||||
```
|
||||
|
||||
これらの式は上から順に、`true`、`true`、`false`、`false` と評価されます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
return ステートメントの意味が正しくなるように、指定された行に大なり演算子を追加してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`testGreaterThan(0)` は文字列 `10 or Under` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(0) === '10 or Under');
|
||||
```
|
||||
|
||||
`testGreaterThan(10)` は文字列 `10 or Under` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(10) === '10 or Under');
|
||||
```
|
||||
|
||||
`testGreaterThan(11)` は文字列 `Over 10` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(11) === 'Over 10');
|
||||
```
|
||||
|
||||
`testGreaterThan(99)` は文字列 `Over 10` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(99) === 'Over 10');
|
||||
```
|
||||
|
||||
`testGreaterThan(100)` は文字列 `Over 10` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(100) === 'Over 10');
|
||||
```
|
||||
|
||||
`testGreaterThan(101)` は文字列 `Over 100` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(101) === 'Over 100');
|
||||
```
|
||||
|
||||
`testGreaterThan(150)` は文字列 `Over 100` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(150) === 'Over 100');
|
||||
```
|
||||
|
||||
`>` 演算子を 2 回以上使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/val\s*>\s*('|")*\d+('|")*/g).length > 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testGreaterThan(val) {
|
||||
if (val) { // Change this line
|
||||
return "Over 100";
|
||||
}
|
||||
|
||||
if (val) { // Change this line
|
||||
return "Over 10";
|
||||
}
|
||||
|
||||
return "10 or Under";
|
||||
}
|
||||
|
||||
testGreaterThan(10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testGreaterThan(val) {
|
||||
if (val > 100) { // Change this line
|
||||
return "Over 100";
|
||||
}
|
||||
if (val > 10) { // Change this line
|
||||
return "Over 10";
|
||||
}
|
||||
return "10 or Under";
|
||||
}
|
||||
```
|
@ -0,0 +1,115 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d5
|
||||
title: 大なりイコール演算子による比較
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c6KBqtV'
|
||||
forumTopicId: 16785
|
||||
dashedName: comparison-with-the-greater-than-or-equal-to-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
大なりイコール演算子 (`>=`) は、2 つの数値の値を比較します。 左の数値が右の数値よりも大きいか等しい場合は、`true` を返します。 それ以外の場合は、`false` を返します。
|
||||
|
||||
等価演算子と同様に、大なりイコール演算子でも比較時にデータ型が変換されます。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
6 >= 6
|
||||
7 >= '3'
|
||||
2 >= 3
|
||||
'7' >= 9
|
||||
```
|
||||
|
||||
これらの式は上から順に、`true`、`true`、`false`、`false` と評価されます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
return ステートメントの意味が正しくなるように、指定された行に大なりイコール演算子を追加してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`testGreaterOrEqual(0)` は文字列 `Less than 10` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(0) === 'Less than 10');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(9)` は文字列 `Less than 10` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(9) === 'Less than 10');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(10)` は文字列 `10 or Over` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(10) === '10 or Over');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(11)` は文字列 `10 or Over` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(11) === '10 or Over');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(19)` は文字列 `10 or Over` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(19) === '10 or Over');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(100)` は文字列 `20 or Over` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(100) === '20 or Over');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(21)` は文字列 `20 or Over` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(21) === '20 or Over');
|
||||
```
|
||||
|
||||
`>=` 演算子を 2 回以上使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/val\s*>=\s*('|")*\d+('|")*/g).length > 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testGreaterOrEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "20 or Over";
|
||||
}
|
||||
|
||||
if (val) { // Change this line
|
||||
return "10 or Over";
|
||||
}
|
||||
|
||||
return "Less than 10";
|
||||
}
|
||||
|
||||
testGreaterOrEqual(10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testGreaterOrEqual(val) {
|
||||
if (val >= 20) { // Change this line
|
||||
return "20 or Over";
|
||||
}
|
||||
|
||||
if (val >= 10) { // Change this line
|
||||
return "10 or Over";
|
||||
}
|
||||
|
||||
return "Less than 10";
|
||||
}
|
||||
```
|
@ -0,0 +1,93 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d2
|
||||
title: 不等価演算子による比較
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cdBm9Sr'
|
||||
forumTopicId: 16787
|
||||
dashedName: comparison-with-the-inequality-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
不等価演算子 (`!=`) は等価演算子の逆です。 つまり、等価演算子では等しくない場合に `false` を返していたところを、不等価演算子では `true` を返します。*逆の場合も同様です*。 等価演算子と同様に、不等価演算子でも比較時に値のデータ型が変換されます。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
1 != 2
|
||||
1 != "1"
|
||||
1 != '1'
|
||||
1 != true
|
||||
0 != false
|
||||
```
|
||||
|
||||
これらの式は上から順に、`true`、`false`、`false`、`false`、`false` と評価されます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`val` が `99` と等しくない場合に関数が文字列 `Not Equal` を返すように、`if` ステートメントに不等価演算子 `!=` を追加してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`testNotEqual(99)` は文字列 `Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testNotEqual(99) === 'Equal');
|
||||
```
|
||||
|
||||
`testNotEqual("99")` は文字列 `Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testNotEqual('99') === 'Equal');
|
||||
```
|
||||
|
||||
`testNotEqual(12)` は文字列 `Not Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testNotEqual(12) === 'Not Equal');
|
||||
```
|
||||
|
||||
`testNotEqual("12")` は文字列 `Not Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testNotEqual('12') === 'Not Equal');
|
||||
```
|
||||
|
||||
`testNotEqual("bob")` は文字列 `Not Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testNotEqual('bob') === 'Not Equal');
|
||||
```
|
||||
|
||||
`!=` 演算子を使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/(?!!==)!=/));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testNotEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Not Equal";
|
||||
}
|
||||
return "Equal";
|
||||
}
|
||||
|
||||
testNotEqual(10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testNotEqual(val) {
|
||||
if (val != 99) {
|
||||
return "Not Equal";
|
||||
}
|
||||
return "Equal";
|
||||
}
|
||||
```
|
@ -0,0 +1,108 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d6
|
||||
title: 小なり演算子による比較
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cNVRWtB'
|
||||
forumTopicId: 16789
|
||||
dashedName: comparison-with-the-less-than-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
小なり演算子 (`<`) は、2 つの数値の値を比較します。 左の数値が右の数値よりも小さい場合は、`true` を返します。 それ以外の場合は、`false` を返します。 等価演算子と同様に、小なり演算子でも比較時にデータ型が変換されます。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
2 < 5
|
||||
'3' < 7
|
||||
5 < 5
|
||||
3 < 2
|
||||
'8' < 4
|
||||
```
|
||||
|
||||
これらの式は上から順に、`true`、`true`、`false`、`false`、`false` と評価されます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
return ステートメントの意味が正しくなるように、指定された行に小なり演算子を追加してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`testLessThan(0)` は文字列 `Under 25` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessThan(0) === 'Under 25');
|
||||
```
|
||||
|
||||
`testLessThan(24)` は文字列 `Under 25` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessThan(24) === 'Under 25');
|
||||
```
|
||||
|
||||
`testLessThan(25)` は文字列 `Under 55` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessThan(25) === 'Under 55');
|
||||
```
|
||||
|
||||
`testLessThan(54)` は文字列 `Under 55` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessThan(54) === 'Under 55');
|
||||
```
|
||||
|
||||
`testLessThan(55)` は文字列 `55 or Over` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessThan(55) === '55 or Over');
|
||||
```
|
||||
|
||||
`testLessThan(99)` は文字列 `55 or Over` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessThan(99) === '55 or Over');
|
||||
```
|
||||
|
||||
`<` 演算子を 2 回以上使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/val\s*<\s*('|")*\d+('|")*/g).length > 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testLessThan(val) {
|
||||
if (val) { // Change this line
|
||||
return "Under 25";
|
||||
}
|
||||
|
||||
if (val) { // Change this line
|
||||
return "Under 55";
|
||||
}
|
||||
|
||||
return "55 or Over";
|
||||
}
|
||||
|
||||
testLessThan(10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testLessThan(val) {
|
||||
if (val < 25) { // Change this line
|
||||
return "Under 25";
|
||||
}
|
||||
|
||||
if (val < 55) { // Change this line
|
||||
return "Under 55";
|
||||
}
|
||||
|
||||
return "55 or Over";
|
||||
}
|
||||
```
|
@ -0,0 +1,114 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d7
|
||||
title: 小なりイコール演算子による比較
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cNVR7Am'
|
||||
forumTopicId: 16788
|
||||
dashedName: comparison-with-the-less-than-or-equal-to-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
小なりイコール演算子 (`<=`) は、2 つの数値の値を比較します。 左の数値が右の数値よりも小さいか等しい場合は、`true` を返します。 左の数値が右の数値よりも大きい場合は、`false` を返します。 等価演算子と同様に、小なりイコール演算子でもデータ型が変換されます。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
4 <= 5
|
||||
'7' <= 7
|
||||
5 <= 5
|
||||
3 <= 2
|
||||
'8' <= 4
|
||||
```
|
||||
|
||||
これらの式は上から順に、`true`、`true`、`true`、`false`、`false` と評価されます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
return ステートメントの意味が正しくなるように、指定された行に小なりイコール演算子を追加してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`testLessOrEqual(0)` は文字列 `Smaller Than or Equal to 12` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(0) === 'Smaller Than or Equal to 12');
|
||||
```
|
||||
|
||||
`testLessOrEqual(11)` は文字列 `Smaller Than or Equal to 12` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(11) === 'Smaller Than or Equal to 12');
|
||||
```
|
||||
|
||||
`testLessOrEqual(12)` は文字列 `Smaller Than or Equal to 12` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(12) === 'Smaller Than or Equal to 12');
|
||||
```
|
||||
|
||||
`testLessOrEqual(23)` は文字列 `Smaller Than or Equal to 24` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(23) === 'Smaller Than or Equal to 24');
|
||||
```
|
||||
|
||||
`testLessOrEqual(24)` は文字列 `Smaller Than or Equal to 24` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(24) === 'Smaller Than or Equal to 24');
|
||||
```
|
||||
|
||||
`testLessOrEqual(25)` は文字列 `More Than 24` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(25) === 'More Than 24');
|
||||
```
|
||||
|
||||
`testLessOrEqual(55)` は文字列 `More Than 24` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(55) === 'More Than 24');
|
||||
```
|
||||
|
||||
`<=` 演算子を 2 回以上使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/val\s*<=\s*('|")*\d+('|")*/g).length > 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testLessOrEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Smaller Than or Equal to 12";
|
||||
}
|
||||
|
||||
if (val) { // Change this line
|
||||
return "Smaller Than or Equal to 24";
|
||||
}
|
||||
|
||||
return "More Than 24";
|
||||
}
|
||||
|
||||
testLessOrEqual(10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testLessOrEqual(val) {
|
||||
if (val <= 12) { // Change this line
|
||||
return "Smaller Than or Equal to 12";
|
||||
}
|
||||
|
||||
if (val <= 24) { // Change this line
|
||||
return "Smaller Than or Equal to 24";
|
||||
}
|
||||
|
||||
return "More Than 24";
|
||||
}
|
||||
```
|
@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d1
|
||||
title: 厳密等価演算子による比較
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cy87atr'
|
||||
forumTopicId: 16790
|
||||
dashedName: comparison-with-the-strict-equality-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
厳密等価演算子 (`===`) は等価演算子 (`==`) の相棒です。 しかし、比較する値を共通の型に変換しようとする等価演算子とは異なり、厳密等価演算子は型変換を行いません。
|
||||
|
||||
比較する値の型が異なる場合は等しくないとみなし、厳密等価演算子は false を返します。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
3 === 3
|
||||
3 === '3'
|
||||
```
|
||||
|
||||
これらの条件ではそれぞれ、`true` と `false` を返すことになります。
|
||||
|
||||
2 番目の例では、 `3` は `Number` 型で、 `'3'` は `String` 型です。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`if` ステートメントで厳密等価演算子を使用して、`val` が `7` と厳密に等しい場合に関数が文字列 `Equal` を返すようにしてください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`testStrict(10)` は文字列 `Not Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testStrict(10) === 'Not Equal');
|
||||
```
|
||||
|
||||
`testStrict(7)` は文字列 `Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testStrict(7) === 'Equal');
|
||||
```
|
||||
|
||||
`testStrict("7")` は文字列 `Not Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testStrict('7') === 'Not Equal');
|
||||
```
|
||||
|
||||
`===` 演算子を使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/(val\s*===\s*\d+)|(\d+\s*===\s*val)/g).length > 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testStrict(val) {
|
||||
if (val) { // Change this line
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
|
||||
testStrict(10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testStrict(val) {
|
||||
if (val === 7) {
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
```
|
@ -0,0 +1,85 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d3
|
||||
title: 厳密不等価演算子による比較
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cKekkUy'
|
||||
forumTopicId: 16791
|
||||
dashedName: comparison-with-the-strict-inequality-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
厳密不等価演算子 (`!==`) は論理的な意味で厳密等価演算子の逆です。 つまり、「厳密に等しくない」 場合に厳密等価演算子が `false` を返す場面で、`true` を返します。*逆の場合も同様です*。 厳密不等価演算子はデータ型の変換を行いません。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
3 !== 3
|
||||
3 !== '3'
|
||||
4 !== 3
|
||||
```
|
||||
|
||||
これらの式は順に `false`、`true`、`true` と評価されます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`val` が `17` と厳密に等しくない場合に関数が文字列 `Not Equal` を返すように、`if` ステートメントに厳密不等価演算子を追加してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`testStrictNotEqual(17)` は文字列 `Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testStrictNotEqual(17) === 'Equal');
|
||||
```
|
||||
|
||||
`testStrictNotEqual("17")` は文字列 `Not Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testStrictNotEqual('17') === 'Not Equal');
|
||||
```
|
||||
|
||||
`testStrictNotEqual(12)` は文字列 `Not Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testStrictNotEqual(12) === 'Not Equal');
|
||||
```
|
||||
|
||||
`testStrictNotEqual("bob")` は文字列 `Not Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testStrictNotEqual('bob') === 'Not Equal');
|
||||
```
|
||||
|
||||
`!==` 演算子を使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/(val\s*!==\s*\d+)|(\d+\s*!==\s*val)/g).length > 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testStrictNotEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Not Equal";
|
||||
}
|
||||
return "Equal";
|
||||
}
|
||||
|
||||
testStrictNotEqual(10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testStrictNotEqual(val) {
|
||||
if (val !== 17) {
|
||||
return "Not Equal";
|
||||
}
|
||||
return "Equal";
|
||||
}
|
||||
```
|
@ -0,0 +1,130 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d8
|
||||
title: 論理積演算子による比較
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cvbRVtr'
|
||||
forumTopicId: 16799
|
||||
dashedName: comparisons-with-the-logical-and-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
一度に複数の条件をテストしなければならない場合があります。 <dfn>論理積</dfn>演算子 (`&&`) は、左側と右側の<dfn>オペランド</dfn>の両方が true である場合にのみ `true` を返します。
|
||||
|
||||
if ステートメントを別の if ステートメントにネストしても、同じ効果が得られます。
|
||||
|
||||
```js
|
||||
if (num > 5) {
|
||||
if (num < 10) {
|
||||
return "Yes";
|
||||
}
|
||||
}
|
||||
return "No";
|
||||
```
|
||||
|
||||
上の例は、`num` が `5` より大きく `10` より小さい場合にのみ `Yes` を返します。 同じロジックを次のように記述することができます。
|
||||
|
||||
```js
|
||||
if (num > 5 && num < 10) {
|
||||
return "Yes";
|
||||
}
|
||||
return "No";
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
`&&` 演算子を使用して、2つの if ステートメントを 1 つのステートメントにしてください。このプログラムは `val` が `25` 以上 `50` 以下の場合に文字列 `Yes` を返します。 それ以外の場合は文字列 `No` を返します。
|
||||
|
||||
# --hints--
|
||||
|
||||
`&&` 演算子を 1 回使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/&&/g).length === 1);
|
||||
```
|
||||
|
||||
`if` ステートメントを 1 つだけにする必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/if/g).length === 1);
|
||||
```
|
||||
|
||||
`testLogicalAnd(0)` は文字列 `No` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(0) === 'No');
|
||||
```
|
||||
|
||||
`testLogicalAnd(24)` は文字列 `No` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(24) === 'No');
|
||||
```
|
||||
|
||||
`testLogicalAnd(25)` は文字列 `Yes` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(25) === 'Yes');
|
||||
```
|
||||
|
||||
`testLogicalAnd(30)` は文字列 `Yes` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(30) === 'Yes');
|
||||
```
|
||||
|
||||
`testLogicalAnd(50)` は文字列 `Yes` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(50) === 'Yes');
|
||||
```
|
||||
|
||||
`testLogicalAnd(51)` は文字列 `No` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(51) === 'No');
|
||||
```
|
||||
|
||||
`testLogicalAnd(75)` は文字列 `No` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(75) === 'No');
|
||||
```
|
||||
|
||||
`testLogicalAnd(80)` は文字列 `No` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(80) === 'No');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testLogicalAnd(val) {
|
||||
// Only change code below this line
|
||||
|
||||
if (val) {
|
||||
if (val) {
|
||||
return "Yes";
|
||||
}
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
return "No";
|
||||
}
|
||||
|
||||
testLogicalAnd(10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testLogicalAnd(val) {
|
||||
if (val >= 25 && val <= 50) {
|
||||
return "Yes";
|
||||
}
|
||||
return "No";
|
||||
}
|
||||
```
|
@ -0,0 +1,135 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d9
|
||||
title: 論理和演算子による比較
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cEPrGTN'
|
||||
forumTopicId: 16800
|
||||
dashedName: comparisons-with-the-logical-or-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<dfn>論理和</dfn> 演算子 (`||`) は、<dfn>オペランド</dfn> のいずれかが `true` である場合に `true` を返します。 それ以外の場合は、`false` を返します。
|
||||
|
||||
<dfn>論理和</dfn>演算子は 2 本のパイプ記号 (`||`) で構成されます。 この記号のキーは通常、バックスペースキーと Enter キーの間近くにあります。
|
||||
|
||||
次のようなパターンは以前のチャレンジでも登場しています。
|
||||
|
||||
```js
|
||||
if (num > 10) {
|
||||
return "No";
|
||||
}
|
||||
if (num < 5) {
|
||||
return "No";
|
||||
}
|
||||
return "Yes";
|
||||
```
|
||||
|
||||
上の例は、`num` が `5` と `10` の間 (5 と 10 を含む) の場合のみ、`Yes` を返します。 同じロジックを次のように記述することができます。
|
||||
|
||||
```js
|
||||
if (num > 10 || num < 5) {
|
||||
return "No";
|
||||
}
|
||||
return "Yes";
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
2つの `if` ステートメントを 1 つのステートメントにまとめて、`val` が `10` 以上 `20` 以下でない場合は文字列 `Outside` を返すようにしてください。 それ以外の場合は、文字列 `Inside` を返してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`||`演算子を 1 回使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/\|\|/g).length === 1);
|
||||
```
|
||||
|
||||
`if` ステートメントを 1 つだけにする必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/if/g).length === 1);
|
||||
```
|
||||
|
||||
`testLogicalOr(0)` は文字列 `Outside` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(0) === 'Outside');
|
||||
```
|
||||
|
||||
`testLogicalOr(9)` は文字列 `Outside` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(9) === 'Outside');
|
||||
```
|
||||
|
||||
`testLogicalOr(10)` は文字列 `Inside` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(10) === 'Inside');
|
||||
```
|
||||
|
||||
`testLogicalOr(15)` は文字列 `Inside` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(15) === 'Inside');
|
||||
```
|
||||
|
||||
`testLogicalOr(19)` は文字列 `Inside` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(19) === 'Inside');
|
||||
```
|
||||
|
||||
`testLogicalOr(20)` は文字列 `Inside` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(20) === 'Inside');
|
||||
```
|
||||
|
||||
`testLogicalOr(21)` は文字列 `Outside` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(21) === 'Outside');
|
||||
```
|
||||
|
||||
`testLogicalOr(25)` は文字列 `Outside` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(25) === 'Outside');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testLogicalOr(val) {
|
||||
// Only change code below this line
|
||||
|
||||
if (val) {
|
||||
return "Outside";
|
||||
}
|
||||
|
||||
if (val) {
|
||||
return "Outside";
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
return "Inside";
|
||||
}
|
||||
|
||||
testLogicalOr(15);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testLogicalOr(val) {
|
||||
if (val < 10 || val > 20) {
|
||||
return "Outside";
|
||||
}
|
||||
return "Inside";
|
||||
}
|
||||
```
|
@ -0,0 +1,101 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244af
|
||||
title: 加算と代入の組み合わせ
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cDR6LCb'
|
||||
forumTopicId: 16661
|
||||
dashedName: compound-assignment-with-augmented-addition
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
プログラミングでは、代入を使用して変数の中身を変更する操作がよく行われます。 最初に等号の右辺のすべてが評価されることを忘れないようにしましょう。
|
||||
|
||||
```js
|
||||
myVar = myVar + 5;
|
||||
```
|
||||
|
||||
と記述すると、`myVar` に `5` を足すことができます。 このような操作は一般的なパターンなので、算術演算と代入の両方を一度に行う演算子があります。
|
||||
|
||||
その一つが `+=` 演算子です。
|
||||
|
||||
```js
|
||||
let myVar = 1;
|
||||
myVar += 5;
|
||||
console.log(myVar);
|
||||
```
|
||||
|
||||
上の例では `6` がコンソールに表示されます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`a`、`b`、`c` への各代入を、`+=` 演算子を使用するように変換してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`a` が `15` と等しくなるようにしてください。
|
||||
|
||||
```js
|
||||
assert(a === 15);
|
||||
```
|
||||
|
||||
`b` が `26` と等しくなるようにしてください。
|
||||
|
||||
```js
|
||||
assert(b === 26);
|
||||
```
|
||||
|
||||
`c` が `19` と等しくなるようにしてください。
|
||||
|
||||
```js
|
||||
assert(c === 19);
|
||||
```
|
||||
|
||||
各変数で `+=` 演算子を使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/\+=/g).length === 3);
|
||||
```
|
||||
|
||||
指定のコメントより上にあるコードを変更しないでください。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/let a = 3;/.test(code) &&
|
||||
/let b = 17;/.test(code) &&
|
||||
/let c = 12;/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let a = 3;
|
||||
let b = 17;
|
||||
let c = 12;
|
||||
|
||||
// Only change code below this line
|
||||
a = a + 12;
|
||||
b = 9 + b;
|
||||
c = c + 7;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let a = 3;
|
||||
let b = 17;
|
||||
let c = 12;
|
||||
|
||||
a += 12;
|
||||
b += 9;
|
||||
c += 7;
|
||||
```
|
@ -0,0 +1,95 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b2
|
||||
title: 除算と代入の組み合わせ
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c2QvKT2'
|
||||
forumTopicId: 16659
|
||||
dashedName: compound-assignment-with-augmented-division
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`/=` 演算子は変数を他の数で割ります。
|
||||
|
||||
```js
|
||||
myVar = myVar / 5;
|
||||
```
|
||||
|
||||
は `myVar` を `5` で割ります。 これは次のように書き換えることができます。
|
||||
|
||||
```js
|
||||
myVar /= 5;
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
`a`、`b`、`c` への各代入を、`/=` 演算子を使用するように変換してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`a` が `4` と等しくなるようにしてください。
|
||||
|
||||
```js
|
||||
assert(a === 4);
|
||||
```
|
||||
|
||||
`b` が `27` と等しくなるようにしてください。
|
||||
|
||||
```js
|
||||
assert(b === 27);
|
||||
```
|
||||
|
||||
`c` が `3` と等しくなるようにしてください。
|
||||
|
||||
```js
|
||||
assert(c === 3);
|
||||
```
|
||||
|
||||
各変数で `/=` 演算子を使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/\/=/g).length === 3);
|
||||
```
|
||||
|
||||
指定のコメントより上にあるコードを変更しないでください。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/let a = 48;/.test(code) &&
|
||||
/let b = 108;/.test(code) &&
|
||||
/let c = 33;/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let a = 48;
|
||||
let b = 108;
|
||||
let c = 33;
|
||||
|
||||
// Only change code below this line
|
||||
a = a / 12;
|
||||
b = b / 4;
|
||||
c = c / 11;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let a = 48;
|
||||
let b = 108;
|
||||
let c = 33;
|
||||
|
||||
a /= 12;
|
||||
b /= 4;
|
||||
c /= 11;
|
||||
```
|
@ -0,0 +1,95 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b1
|
||||
title: 乗算と代入の組み合わせ
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c83vrfa'
|
||||
forumTopicId: 16662
|
||||
dashedName: compound-assignment-with-augmented-multiplication
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`*=` 演算子は変数に数を掛けます。
|
||||
|
||||
```js
|
||||
myVar = myVar * 5;
|
||||
```
|
||||
|
||||
は `myVar` に `5` を掛けます。 これは次のように書き換えることができます。
|
||||
|
||||
```js
|
||||
myVar *= 5;
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
`a`、`b`、`c` への各代入を、`*=` 演算子を使用するように変換してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`a` が `25` と等しくなるようにしてください。
|
||||
|
||||
```js
|
||||
assert(a === 25);
|
||||
```
|
||||
|
||||
`b` が `36` と等しくなるようにしてください。
|
||||
|
||||
```js
|
||||
assert(b === 36);
|
||||
```
|
||||
|
||||
`c` が `46` と等しくなるようにしてください。
|
||||
|
||||
```js
|
||||
assert(c === 46);
|
||||
```
|
||||
|
||||
各変数で `*=` 演算子を使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/\*=/g).length === 3);
|
||||
```
|
||||
|
||||
指定のコメントより上にあるコードを変更しないでください。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/let a = 5;/.test(code) &&
|
||||
/let b = 12;/.test(code) &&
|
||||
/let c = 4\.6;/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let a = 5;
|
||||
let b = 12;
|
||||
let c = 4.6;
|
||||
|
||||
// Only change code below this line
|
||||
a = a * 5;
|
||||
b = 3 * b;
|
||||
c = c * 10;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let a = 5;
|
||||
let b = 12;
|
||||
let c = 4.6;
|
||||
|
||||
a *= 5;
|
||||
b *= 3;
|
||||
c *= 10;
|
||||
```
|
@ -0,0 +1,93 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b0
|
||||
title: 減算と代入の組み合わせ
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c2Qv7AV'
|
||||
forumTopicId: 16660
|
||||
dashedName: compound-assignment-with-augmented-subtraction
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`+=` 演算子と同様に、`-=` は変数から数を引きます。
|
||||
|
||||
```js
|
||||
myVar = myVar - 5;
|
||||
```
|
||||
|
||||
は `myVar` から `5` を引きます。 これは次のように書き換えることができます。
|
||||
|
||||
```js
|
||||
myVar -= 5;
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
`a`、`b`、`c` への各代入を、`-=` 演算子を使用するように変換してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`a` が `5` と等しくなるようにしてください。
|
||||
|
||||
```js
|
||||
assert(a === 5);
|
||||
```
|
||||
|
||||
`b` が `-6` と等しくなるようにしてください。
|
||||
|
||||
```js
|
||||
assert(b === -6);
|
||||
```
|
||||
|
||||
`c` が `2` と等しくなるようにしてください。
|
||||
|
||||
```js
|
||||
assert(c === 2);
|
||||
```
|
||||
|
||||
各変数で `-=` 演算子を使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/-=/g).length === 3);
|
||||
```
|
||||
|
||||
指定のコメントより上にあるコードを変更しないでください。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/let a = 11;/.test(code) && /let b = 9;/.test(code) && /let c = 3;/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let a = 11;
|
||||
let b = 9;
|
||||
let c = 3;
|
||||
|
||||
// Only change code below this line
|
||||
a = a - 6;
|
||||
b = b - 15;
|
||||
c = c - 1;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let a = 11;
|
||||
let b = 9;
|
||||
let c = 3;
|
||||
|
||||
a -= 6;
|
||||
b -= 15;
|
||||
c -= 1;
|
||||
```
|
@ -0,0 +1,83 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b7
|
||||
title: 文字列をプラス演算子で連結する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cNpM8AN'
|
||||
forumTopicId: 16802
|
||||
dashedName: concatenating-strings-with-plus-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript では、`+` 演算子を `String` の値に対して使用する場合、その演算子のことを<dfn>連結</dfn>演算子と呼びます。 他の文字列を一緒に<dfn>連結する</dfn>ことで新しい文字列を作ることができます。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
'My name is Alan,' + ' I concatenate.'
|
||||
```
|
||||
|
||||
**注:** 空白が必要な場合は注意してください。 連結では、文字列の間に空白が追加されないため、必要な場合は自分で付け加える必要があります。
|
||||
|
||||
例:
|
||||
|
||||
```js
|
||||
const ourStr = "I come first. " + "I come second.";
|
||||
```
|
||||
|
||||
文字列 `I come first. ` `I come second.` がコンソールに表示されます。
|
||||
# --instructions--
|
||||
|
||||
`+` 演算子を使用して、文字列 `This is the start.` と `This is the end.` から `myStr` を作成してください。 2 つの文字列の間に空白を必ず含めるようにしてください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myStr` の値が文字列 `This is the start.` `This is the end.` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(myStr === 'This is the start. This is the end.');
|
||||
```
|
||||
|
||||
`+` 演算子を使用して `myStr` を作成する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/(["']).*\1\s*\+\s*(["']).*\2/g));
|
||||
```
|
||||
|
||||
`const` キーワードを使用して `myStr` を作成する必要があります。
|
||||
|
||||
```js
|
||||
assert(/const\s+myStr/.test(code));
|
||||
```
|
||||
|
||||
結果を `myStr` 変数に代入する必要があります。
|
||||
|
||||
```js
|
||||
assert(/myStr\s*=/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){
|
||||
if(typeof myStr === 'string') {
|
||||
return 'myStr = "' + myStr + '"';
|
||||
} else {
|
||||
return 'myStr is not a string';
|
||||
}
|
||||
})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const myStr = ""; // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myStr = "This is the start. " + "This is the end.";
|
||||
```
|
@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b8
|
||||
title: 文字列を += 演算子で連結する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cbQmmC4'
|
||||
forumTopicId: 16803
|
||||
dashedName: concatenating-strings-with-the-plus-equals-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`+=` 演算子を使用して、既存の文字列変数の末尾に文字列を<dfn>連結する</dfn>こともできます。 これは長い文字列を複数行に分割する場合にとても便利です。
|
||||
|
||||
**注:** 空白が必要な場合は注意してください。 連結では、文字列の間に空白が追加されないため、必要な場合は自分で付け加える必要があります。
|
||||
|
||||
例:
|
||||
|
||||
```js
|
||||
let ourStr = "I come first. ";
|
||||
ourStr += "I come second.";
|
||||
```
|
||||
|
||||
これで `ourStr` の値は文字列 `I come first. I come second.` になります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`+=` 演算子を使用して、`This is the first sentence.` と `This is the second sentence.` の 2 つの文字列を連結し、複数行にわたる `myStr` を作成してください。 前の例のように、`+=` 演算子を使用し、必ず 2 つの文字列の間に空白を入れてください。 まず `myStr` に 1 つ目の文字列を代入し、それから 2 つ目の文字列を追加してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myStr` の値が文字列値 `This is the first sentence. This is the second sentence.` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(myStr === 'This is the first sentence. This is the second sentence.');
|
||||
```
|
||||
|
||||
`+=` 演算子を使用して `myStr` を作成する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/myStr\s*\+=\s*(["']).*\1/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){
|
||||
if(typeof myStr === 'string') {
|
||||
return 'myStr = "' + myStr + '"';
|
||||
} else {
|
||||
return 'myStr is not a string';
|
||||
}
|
||||
})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let myStr;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let myStr = "This is the first sentence. ";
|
||||
myStr += "This is the second sentence.";
|
||||
```
|
@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b9
|
||||
title: 変数を使用して文字列を作成する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cqk8rf4'
|
||||
forumTopicId: 16805
|
||||
dashedName: constructing-strings-with-variables
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
[Mad Libs](https://en.wikipedia.org/wiki/Mad_Libs) 的に文字列を作成しなければならない場合があります。 連結演算子 (`+`) を使用して、作成する文字列に 1 つまたは複数の変数を挿入することができます。
|
||||
|
||||
例:
|
||||
|
||||
```js
|
||||
const ourName = "freeCodeCamp";
|
||||
const ourStr = "Hello, our name is " + ourName + ", how are you?";
|
||||
```
|
||||
|
||||
`ourStr` の値は文字列 `Hello, our name is freeCodeCamp, how are you?` となります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`myName` にあなたの名前と同じ文字列を設定し、文字列 `My name is` と `and I am well!` の間に `myName` を挿入した `myStr` を作成してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myName` には 3 文字以上の長さの文字列を設定する必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof myName !== 'undefined' && myName.length > 2);
|
||||
```
|
||||
|
||||
2つの `+` 演算子を使用して、`myName` を挿入した `myStr` を作成する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/["']\s*\+\s*myName\s*\+\s*["']/g).length > 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){
|
||||
var output = [];
|
||||
if(typeof myName === 'string') {
|
||||
output.push('myName = "' + myName + '"');
|
||||
} else {
|
||||
output.push('myName is not a string');
|
||||
}
|
||||
if(typeof myStr === 'string') {
|
||||
output.push('myStr = "' + myStr + '"');
|
||||
} else {
|
||||
output.push('myStr is not a string');
|
||||
}
|
||||
return output.join('\n');
|
||||
})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Only change code below this line
|
||||
const myName = "";
|
||||
const myStr = "";
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myName = "Bob";
|
||||
const myStr = "My name is " + myName + " and I am well!";
|
||||
```
|
@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 56105e7b514f539506016a5e
|
||||
title: for ループでの減算
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c2R6BHa'
|
||||
forumTopicId: 16808
|
||||
dashedName: count-backwards-with-a-for-loop
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
for ループでは、適切な条件を定義することができれば、減算も可能です。
|
||||
|
||||
繰り返しごとに 2 ずつデクリメント (減算) するには、初期化式、条件式、および最終式を変更する必要があります。
|
||||
|
||||
`i = 10` から始めて、`i > 0` の間、ループ処理を行います。 `i -= 2` と記述すると、ループごとに `i` が 2 ずつデクリメントされます。
|
||||
|
||||
```js
|
||||
const ourArray = [];
|
||||
|
||||
for (let i = 10; i > 0; i -= 2) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
```
|
||||
|
||||
`ourArray` の内容は `[10, 8, 6, 4, 2]` になります。 2 ずつ減算して奇数の降順の配列を作成できるように、初期化式と最終式を変更してみましょう。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`for` ループを使用して、9 から 1 までの奇数を `myArray` に push してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
この作業では `for` ループを使用してください。
|
||||
|
||||
```js
|
||||
assert(/for\s*\([^)]+?\)/.test(code));
|
||||
```
|
||||
|
||||
配列メソッド `push` を使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/myArray.push/));
|
||||
```
|
||||
|
||||
`myArray` は `[9, 7, 5, 3, 1]` となる必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(myArray, [9, 7, 5, 3, 1]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myArray = [];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [];
|
||||
for (let i = 9; i > 0; i -= 2) {
|
||||
myArray.push(i);
|
||||
}
|
||||
```
|
@ -0,0 +1,201 @@
|
||||
---
|
||||
id: 565bbe00e9cc8ac0725390f4
|
||||
title: カードカウンティング
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c6KE7ty'
|
||||
forumTopicId: 16809
|
||||
dashedName: counting-cards
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
カジノゲームのブラックジャックでは、デッキに残っているカードのハイローの相対数を把握しておくことで、プレイヤーはディーラーに対して有利な立場を得られます。 これは[カードカウンティング](https://en.wikipedia.org/wiki/Card_counting)と呼ばれています。
|
||||
|
||||
デッキに残っているハイカードの数が多いほど、プレイヤーに有利となります。 次の表に従って各カードに値を割り当てます。 カウンティングの結果が正の場合、プレイヤーは高く賭けるべきです。 カウンティングの結果がゼロまたは負の場合、プレイヤーは少なめに賭けた方がいいでしょう。
|
||||
|
||||
<table class='table table-striped'><thead><tr><th>カウントの変更</th><th>カード</th></tr></thead><tbody><tr><td>+1</td><td>2、3、4、5、6</td></tr><tr><td>0</td><td>7、8、9</td></tr><tr><td>-1</td><td>10、'J'、'Q'、'K'、'A'</td></tr></tbody></table>
|
||||
|
||||
カードカウンティング関数を記述してください。 この関数は数値または文字列の `card` パラメーターを受け取り、カードの値に応じて (表を参照)、グローバルの `count` 変数をインクリメントまたはデクリメントします。 次に、この関数は現在のカウントを示す文字列と、文字列 `Bet` (カウントが正の場合) または文字列 `Hold` (カウントがゼロか負の場合) を返します。 現在のカウントとプレイヤーの判断 (`Bet` または `Hold`) の間にスペースを 1 つ入れてください。
|
||||
|
||||
**出力例:** `-3 Hold`、`5 Bet` など
|
||||
|
||||
**ヒント**
|
||||
値が 7、8、9 の場合に `count` を 0 にリセットしないでください。 配列を返さないでください。
|
||||
出力に引用符 (シングルクォートまたはダブルクォート) を含めないでください。
|
||||
|
||||
# --hints--
|
||||
|
||||
カードの並びが 2、3、4、5、6 の場合は文字列 `5 Bet` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(2);
|
||||
cc(3);
|
||||
cc(4);
|
||||
cc(5);
|
||||
var out = cc(6);
|
||||
if (out === '5 Bet') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
カードの並びが 7、8、9 の場合は文字列 `0 Hold` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(7);
|
||||
cc(8);
|
||||
var out = cc(9);
|
||||
if (out === '0 Hold') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
カードの並びが 10、J、Q、K、A の場合は文字列 `-5 Hold` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(10);
|
||||
cc('J');
|
||||
cc('Q');
|
||||
cc('K');
|
||||
var out = cc('A');
|
||||
if (out === '-5 Hold') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
カードの並びが 3、7、Q、8、A の場合は文字列 `-1 Hold` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(3);
|
||||
cc(7);
|
||||
cc('Q');
|
||||
cc(8);
|
||||
var out = cc('A');
|
||||
if (out === '-1 Hold') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
カードの並びが 2、J、9、2、7 の場合は文字列 `1 Bet` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(2);
|
||||
cc('J');
|
||||
cc(9);
|
||||
cc(2);
|
||||
var out = cc(7);
|
||||
if (out === '1 Bet') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
カードの並びが 2、2、10 の場合は文字列 `1 Bet` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(2);
|
||||
cc(2);
|
||||
var out = cc(10);
|
||||
if (out === '1 Bet') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
カードの並びが 3、2、A、10、K の場合は文字列 `-1 Hold` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(3);
|
||||
cc(2);
|
||||
cc('A');
|
||||
cc(10);
|
||||
var out = cc('K');
|
||||
if (out === '-1 Hold') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let count = 0;
|
||||
|
||||
function cc(card) {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
return "Change Me";
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
cc(2); cc(3); cc(7); cc('K'); cc('A');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let count = 0;
|
||||
function cc(card) {
|
||||
switch(card) {
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
count++;
|
||||
break;
|
||||
case 10:
|
||||
case 'J':
|
||||
case 'Q':
|
||||
case 'K':
|
||||
case 'A':
|
||||
count--;
|
||||
}
|
||||
if(count > 0) {
|
||||
return count + " Bet";
|
||||
} else {
|
||||
return count + " Hold";
|
||||
}
|
||||
}
|
||||
```
|
@ -0,0 +1,55 @@
|
||||
---
|
||||
id: cf1391c1c11feddfaeb4bdef
|
||||
title: JavaScript で小数を作成する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ca8GEuW'
|
||||
forumTopicId: 16826
|
||||
dashedName: create-decimal-numbers-with-javascript
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
変数に小数を格納することもできます。 小数は<dfn>浮動小数点数</dfn>または <dfn>float</dfn> と呼ばれることもあります。
|
||||
|
||||
**注:** すべての実数を正確に<dfn>浮動小数点数</dfn>で表すことができるわけではありません。 これは丸めエラーをもたらす可能性があります。 詳細は[こちら](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems)を参照してください。
|
||||
|
||||
# --instructions--
|
||||
|
||||
変数 `myDecimal` を作成して、整数部と小数部からなる小数 (たとえば `5.7`) を代入してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myDecimal` は数値である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof myDecimal === 'number');
|
||||
```
|
||||
|
||||
`myDecimal` には小数点が含まれている必要があります。
|
||||
|
||||
```js
|
||||
assert(myDecimal % 1 != 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){if(typeof myDecimal !== "undefined"){return myDecimal;}})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const ourDecimal = 5.7;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myDecimal = 9.9;
|
||||
```
|
@ -0,0 +1,88 @@
|
||||
---
|
||||
id: 587d7b87367417b2b2512b41
|
||||
title: const キーワードを使用して読み取り専用の変数を宣言する
|
||||
challengeType: 1
|
||||
forumTopicId: 301201
|
||||
dashedName: declare-a-read-only-variable-with-the-const-keyword
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
変数を宣言する新しい方法はキーワード `let` だけではありません。 ES6 では、`const` キーワードを使用して変数を宣言することもできます。
|
||||
|
||||
`const` は `let` の持つ素晴らしい機能をすべて備えていますが、それだけでなく、`const` を使用して宣言した変数は読み取り専用になります。 それらの変数は定数となり、いったん `const` で代入された変数には、再び代入することはできません。
|
||||
|
||||
```js
|
||||
const FAV_PET = "Cats";
|
||||
FAV_PET = "Dogs";
|
||||
```
|
||||
|
||||
`FAV_PET` に再び値を代入しようとしているので、コンソールにエラーが表示されます。
|
||||
|
||||
再代入を必要としない変数に名前を付けるときは、常に `const` キーワードを使用してください。 そうすれば、定数でなければならない変数に誤って再代入しようとするのを防ぐのに役立ちます。
|
||||
|
||||
一般に、定数に名前を付けるときは、すべて英大文字を使用し、単語をアンダースコアで区切ります。
|
||||
|
||||
**注:** 開発者は一般に、イミュータブル (変更不可) の値には英大文字の変数識別子を使用し、ミュータブル (変更可能) の値 (オブジェクトや配列) には英小文字またはキャメルケースを使用します。 このあとのチャレンジで、オブジェクトと配列について学び、ミュータブルの値とイミュータブルの値について詳しく説明します。 また以降のチャレンジでは、英大文字、英小文字、またはキャメルケースのさまざまな変数識別子を使用します。
|
||||
|
||||
# --instructions--
|
||||
|
||||
コードを変更して、すべての変数を `let` または `const` を使用して宣言してください。 変更を必要とする変数には `let` を使用し、定数にする必要がある変数には `const` を使用してください。 また、`const` で宣言した変数の名前について、すべて英大文字にするという一般的な慣習に従うように変更してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`var` がコード内に存在しない必要があります。
|
||||
|
||||
```js
|
||||
(getUserInput) => assert(!getUserInput('index').match(/var/g));
|
||||
```
|
||||
|
||||
`fCC` をすべて英大文字に変更する必要があります。
|
||||
|
||||
```js
|
||||
(getUserInput) => {
|
||||
assert(getUserInput('index').match(/(FCC)/));
|
||||
assert(!getUserInput('index').match(/fCC/));
|
||||
}
|
||||
```
|
||||
|
||||
`FCC` は `const` で宣言された定数変数である必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(FCC, 'freeCodeCamp');
|
||||
assert.match(code, /const\s+FCC/);
|
||||
```
|
||||
|
||||
`fact` は `let` を使用して宣言する必要があります。
|
||||
|
||||
```js
|
||||
(getUserInput) => assert(getUserInput('index').match(/(let fact)/g));
|
||||
```
|
||||
|
||||
`console.log` を、`FCC` と `fact` 変数を出力するように変更する必要があります。
|
||||
|
||||
```js
|
||||
(getUserInput) =>
|
||||
assert(getUserInput('index').match(/console\.log\(\s*FCC\s*\,\s*fact\s*\)\s*;?/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var fCC = "freeCodeCamp"; // Change this line
|
||||
var fact = "is cool!"; // Change this line
|
||||
fact = "is awesome!";
|
||||
console.log(fCC, fact); // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const FCC = "freeCodeCamp";
|
||||
let fact = "is cool!";
|
||||
|
||||
fact = "is awesome!";
|
||||
console.log(FCC, fact);
|
||||
```
|
@ -0,0 +1,61 @@
|
||||
---
|
||||
id: bd7123c9c443eddfaeb5bdef
|
||||
title: JavaScript 変数を宣言する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cNanrHq'
|
||||
forumTopicId: 17556
|
||||
dashedName: declare-javascript-variables
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
コンピューターサイエンスでは、<dfn>データ</dfn>とはコンピューターにとって意味のある情報のことです。 JavaScript では 8 つの異なる<dfn>データ型</dfn>として、`undefined`、`null`、`boolean`、`string`、`symbol`、`bigint`、`number`、`object` が提供されています。
|
||||
|
||||
たとえば、コンピューターでは `12` などの数値と、`"12"`、 `"dog"`、`"123 cats"` といった文字の集まりである `strings` (文字列) が区別されます。 コンピューターは数値に対して数学演算を実行することができますが、文字列に対してそれを実行することはできません。
|
||||
|
||||
<dfn>変数</dfn>を使用すると、コンピューターがデータを動的に格納して操作することが可能になります。 その際、データ自体が使用されるのではなく、データを指し示す「ラベル」が使用されます。 8 つのデータ型はどれも変数に格納することができます。
|
||||
|
||||
変数は、数学で使用される x や y といった変数と似ています。つまり、これらは参照したいデータを表す名前にすぎません。 コンピューターの変数は数学の変数とは異なり、さまざまな値をさまざまなタイミングで格納することができます。
|
||||
|
||||
JavaScript で変数を作成または<dfn>宣言</dfn>するには、次のように変数の前に `var` キーワードを付けます。
|
||||
|
||||
```js
|
||||
var ourName;
|
||||
```
|
||||
|
||||
これで `ourName` という変数が作成されます。 JavaScript では、ステートメントの終わりにセミコロンを付けます。 変数名には、数字、文字、`$`、または `_` を使用できます。 ただし、スペースを含めることはできず、数字で名前を始めることはできません。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`var` キーワードを使用して `myName` という変数を作成してください。
|
||||
|
||||
**ヒント**
|
||||
わからない場合は上の `ourName` の例を見てください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myName` を `var` キーワードで宣言し、末尾にセミコロンを付ける必要があります。
|
||||
|
||||
```js
|
||||
assert(/var\s+myName\s*;/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myName !== "undefined"){(function(v){return v;})(myName);}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myName;
|
||||
```
|
@ -0,0 +1,87 @@
|
||||
---
|
||||
id: bd7123c9c444eddfaeb5bdef
|
||||
title: 文字列変数を宣言する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c2QvWU6'
|
||||
forumTopicId: 17557
|
||||
dashedName: declare-string-variables
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
前回は次のコードを使用して変数を宣言しました。
|
||||
|
||||
```js
|
||||
var myName;
|
||||
```
|
||||
|
||||
しかし、次のように文字列変数を宣言することもできます。
|
||||
|
||||
```js
|
||||
var myName = "your name";
|
||||
```
|
||||
|
||||
こうした `"your name"` のような文字列のことを<dfn>文字列</dfn><dfn>リテラル</dfn>と呼びます。 文字列リテラル (単に文字列とも呼びます) は、シングルクォートまたはダブルクォートで囲まれた 0 文字以上の一連の文字です。
|
||||
|
||||
# --instructions--
|
||||
|
||||
2 つの新しい文字列変数 `myFirstName` と `myLastName` を作成し、それらにあなたの名前と苗字をそれぞれ代入してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myFirstName` は少なくとも 1 つの文字を含む文字列である必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
typeof myFirstName !== 'undefined' &&
|
||||
typeof myFirstName === 'string' &&
|
||||
myFirstName.length > 0
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
`myLastName` はは少なくとも 1 つの文字を含む文字列である必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
typeof myLastName !== 'undefined' &&
|
||||
typeof myLastName === 'string' &&
|
||||
myLastName.length > 0
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myFirstName !== "undefined" && typeof myLastName !== "undefined"){(function(){return myFirstName + ', ' + myLastName;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myFirstName = "Alan";
|
||||
var myLastName = "Turing";
|
||||
```
|
@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ad
|
||||
title: JavaScript で数値をデクリメントする
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cM2KeS2'
|
||||
forumTopicId: 17558
|
||||
dashedName: decrement-a-number-with-javascript
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`--` 演算子を使用して簡単に変数の値を 1 減らす (<dfn>デクリメント</dfn>する) ことができます。
|
||||
|
||||
```js
|
||||
i--;
|
||||
```
|
||||
|
||||
は次の式と等価です。
|
||||
|
||||
```js
|
||||
i = i - 1;
|
||||
```
|
||||
|
||||
**注:** この行全体が `i--;` となり、等号が不要になります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
コードを変更して `myVar` で `--` 演算子を使用してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myVar` は `10` に等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert(myVar === 10);
|
||||
```
|
||||
|
||||
`myVar = myVar - 1;` を書き換える必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/let\s*myVar\s*=\s*11;\s*\/*.*\s*([-]{2}\s*myVar|myVar\s*[-]{2});/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
`myVar` で `--` 演算子を使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(/[-]{2}\s*myVar|myVar\s*[-]{2}/.test(code));
|
||||
```
|
||||
|
||||
指定のコメントより上にあるコードを変更しないでください。
|
||||
|
||||
```js
|
||||
assert(/let myVar = 11;/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'myVar = ' + z;})(myVar);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let myVar = 11;
|
||||
|
||||
// Only change code below this line
|
||||
myVar = myVar - 1;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let myVar = 11;
|
||||
myVar--;
|
||||
```
|
@ -0,0 +1,96 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d3
|
||||
title: JavaScript オブジェクトからのプロパティの削除
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cDqKdTv'
|
||||
forumTopicId: 17560
|
||||
dashedName: delete-properties-from-a-javascript-object
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
次のようにオブジェクトからプロパティを削除することもできます。
|
||||
|
||||
```js
|
||||
delete ourDog.bark;
|
||||
```
|
||||
|
||||
例:
|
||||
|
||||
```js
|
||||
const ourDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"],
|
||||
"bark": "bow-wow"
|
||||
};
|
||||
|
||||
delete ourDog.bark;
|
||||
```
|
||||
|
||||
上記の最後の行の後の `ourDog` は次のようになります。
|
||||
|
||||
```js
|
||||
{
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"]
|
||||
}
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
`tails` プロパティを `myDog` から削除してください。 ドット記法またはブラケット記法のいずれも使用できます。
|
||||
|
||||
# --hints--
|
||||
|
||||
プロパティ `tails` を `myDog` から削除する必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof myDog === 'object' && myDog.tails === undefined);
|
||||
```
|
||||
|
||||
`myDog` の設定を変更しないでください。
|
||||
|
||||
```js
|
||||
assert(code.match(/"tails": 1/g).length > 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myDog = {
|
||||
"name": "Happy Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"],
|
||||
"bark": "woof"
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myDog = {
|
||||
"name": "Happy Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"],
|
||||
"bark": "woof"
|
||||
};
|
||||
delete myDog.tails;
|
||||
```
|
@ -0,0 +1,56 @@
|
||||
---
|
||||
id: bd7993c9ca9feddfaeb7bdef
|
||||
title: JavaScript で小数どうしの割り算をする
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cBZe9AW'
|
||||
forumTopicId: 18255
|
||||
dashedName: divide-one-decimal-by-another-with-javascript
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
小数どうしの割り算をしてみましょう。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`0.0` を変更して、`quotient` (割り算の商) が `2.2` と等しくなるようにしてください。
|
||||
|
||||
# --hints--
|
||||
|
||||
変数 `quotient` が `2.2` と等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert(quotient === 2.2);
|
||||
```
|
||||
|
||||
`/` 演算子を使用して 4.4 を 2.0 で割る必要があります。
|
||||
|
||||
```js
|
||||
assert(/4\.40*\s*\/\s*2\.*0*/.test(code));
|
||||
```
|
||||
|
||||
変数 quotient には一度だけ代入してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/quotient/g).length === 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(y){return 'quotient = '+y;})(quotient);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const quotient = 0.0 / 2.0; // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const quotient = 4.4 / 2.0;
|
||||
```
|
@ -0,0 +1,59 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb6bdef
|
||||
title: JavaScript で数値どうしの割り算をする
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cqkbdAr'
|
||||
forumTopicId: 17566
|
||||
dashedName: divide-one-number-by-another-with-javascript
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
数値どうしの割り算をすることもできます。
|
||||
|
||||
JavaScript では除算記号として `/` を使用します。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
const myVar = 16 / 2;
|
||||
```
|
||||
|
||||
`myVar` の値は `8` になります。
|
||||
# --instructions--
|
||||
|
||||
`0` を変更して、`quotient` (割り算の商) が `2` と等しくなるようにしてください。
|
||||
|
||||
# --hints--
|
||||
|
||||
変数 `quotient` が 2 と等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert(quotient === 2);
|
||||
```
|
||||
|
||||
`/` 演算子を使用してください。
|
||||
|
||||
```js
|
||||
assert(/\d+\s*\/\s*\d+/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'quotient = '+z;})(quotient);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const quotient = 66 / 0;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const quotient = 66 / 33;
|
||||
```
|
@ -0,0 +1,99 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b6
|
||||
title: 文字列内のエスケープシーケンス
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cvmqRh6'
|
||||
forumTopicId: 17567
|
||||
dashedName: escape-sequences-in-strings
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
文字列の中で<dfn>エスケープ</dfn>できる文字は引用符だけではありません。 エスケープ文字を使用するのには 2 つ理由があります。
|
||||
|
||||
1. キャリッジリターンのように、他に入力する方法がない文字を使用できるようになります。
|
||||
2. 文字列中で複数の引用符を表現でき、JavaScript が正しく解釈できるようになります。
|
||||
|
||||
前のチャレンジでは次のことを学習しました。
|
||||
|
||||
<table class='table table-striped'><thead><tr><th>コード</th><th>出力</th></tr></thead><tbody><tr><td><code>\'</code></td><td>シングルクォート</td></tr><tr><td><code>\"</code></td><td>ダブルクォート</td></tr><tr><td><code>\\</code></td><td>バックスラッシュ (日本語では円記号)</td></tr><tr><td><code>\n</code></td><td>改行</td></tr><tr><td><code>\r</code></td><td>キャリッジリターン</td></tr><tr><td><code>\t</code></td><td>タブ</td></tr><tr><td><code>\b</code></td><td>単語境界</td></tr><tr><td><code>\f</code></td><td>改ページ</td></tr></tbody></table>
|
||||
|
||||
*バックスラッシュ自体をバックスラッシュとして表示するためにはエスケープする必要があります。*
|
||||
|
||||
# --instructions--
|
||||
|
||||
エスケープシーケンスを使用して、単一の変数 `myStr` に次の 3 行のテキストを代入してください。
|
||||
|
||||
<blockquote>FirstLine<br> \SecondLine<br>ThirdLine</blockquote>
|
||||
|
||||
特殊文字を正しく挿入するにはエスケープシーケンスを使用する必要があります。 また、エスケープシーケンスや単語の間にスペースを入れず、上記のとおりに表示する必要があります。
|
||||
|
||||
**注:** `SecondLine` にインデントを付けるには、スペースではなくタブエスケープ文字を使用します。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myStr` にはスペースを含めない必要があります。
|
||||
|
||||
```js
|
||||
assert(!/ /.test(myStr));
|
||||
```
|
||||
|
||||
`myStr` には文字列 `FirstLine`、 `SecondLine`、および `ThirdLine` を含める必要があります (大文字小文字を正しく区別してください)。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/FirstLine/.test(myStr) && /SecondLine/.test(myStr) && /ThirdLine/.test(myStr)
|
||||
);
|
||||
```
|
||||
|
||||
`FirstLine` の直後に改行文字 `\n` を付ける必要があります。
|
||||
|
||||
```js
|
||||
assert(/FirstLine\n/.test(myStr));
|
||||
```
|
||||
|
||||
`myStr` にはタブ文字 `\t` を含め、直後に改行文字を付ける必要があります。
|
||||
|
||||
```js
|
||||
assert(/\n\t/.test(myStr));
|
||||
```
|
||||
|
||||
`SecondLine` の前にバックスラッシュ文字 `\` を付ける必要があります。
|
||||
|
||||
```js
|
||||
assert(/\\SecondLine/.test(myStr));
|
||||
```
|
||||
|
||||
`SecondLine` と `ThirdLine` の間に改行文字を入れる必要があります。
|
||||
|
||||
```js
|
||||
assert(/SecondLine\nThirdLine/.test(myStr));
|
||||
```
|
||||
|
||||
`myStr` には手順で示した文字だけを含める必要があります。
|
||||
|
||||
```js
|
||||
assert(myStr === 'FirstLine\n\t\\SecondLine\nThirdLine');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){
|
||||
if (myStr !== undefined){
|
||||
console.log('myStr:\n' + myStr);}})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const myStr = ""; // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myStr = "FirstLine\n\t\\SecondLine\nThirdLine";
|
||||
```
|
@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b5
|
||||
title: 文字列内の引用符リテラルをエスケープする
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c2QvgSr'
|
||||
forumTopicId: 17568
|
||||
dashedName: escaping-literal-quotes-in-strings
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
文字列を定義する際、文字列の先頭と末尾ではシングルクォートまたはダブルクォートを使用する必要があります。 もし文字列の中で引用符リテラル `"` または `'`、つまり引用符そのものが必要になった場合はどうすればよいでしょうか?
|
||||
|
||||
JavaScript では、引用符の直前に<dfn>バックスラッシュ (日本語では円記号)</dfn> (`\`) を入れることで、その引用符が文字列の終わりとみなされないようになります。これを「引用符を<dfn>エスケープ</dfn>する」といいます。
|
||||
|
||||
```js
|
||||
const sampleStr = "Alan said, \"Peter is learning JavaScript\".";
|
||||
```
|
||||
|
||||
エスケープによって、次に続く引用符が文字列の終わりではなく、文字列の中に出現する文字であることを JavaScript に伝えます。 この結果をコンソールに出力すると次のようになります。
|
||||
|
||||
```js
|
||||
Alan said, "Peter is learning JavaScript".
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
<dfn>バックスラッシュ</dfn>を使用して `myStr` 変数に文字列を代入し、次のようにコンソールに出力されるようにしてください。
|
||||
|
||||
```js
|
||||
I am a "double quoted" string inside "double quotes".
|
||||
```
|
||||
|
||||
# --hints--
|
||||
|
||||
2 つのダブルクォート (`"`) と 4 つのエスケープされたダブルクォート (`\"`) を使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/\\"/g).length === 4 && code.match(/[^\\]"/g).length === 2);
|
||||
```
|
||||
|
||||
変数 myStr は文字列 `I am a "double quoted" string inside "double quotes".` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(/I am a "double quoted" string inside "double quotes(\."|"\.)$/.test(myStr));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){
|
||||
if(typeof myStr === 'string') {
|
||||
console.log("myStr = \"" + myStr + "\"");
|
||||
} else {
|
||||
console.log("myStr is undefined");
|
||||
}
|
||||
})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const myStr = ""; // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myStr = "I am a \"double quoted\" string inside \"double quotes\".";
|
||||
```
|
@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 587d7b87367417b2b2512b3f
|
||||
title: var キーワードと let キーワードの違いについて
|
||||
challengeType: 1
|
||||
forumTopicId: 301202
|
||||
dashedName: explore-differences-between-the-var-and-let-keywords
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`var` キーワードによる変数宣言の最も大きな問題の一つは、簡単に変数宣言を上書きできてしまうことです。
|
||||
|
||||
```js
|
||||
var camper = "James";
|
||||
var camper = "David";
|
||||
console.log(camper);
|
||||
```
|
||||
|
||||
上記のコードで、変数 `camper` は最初は `James` として宣言されていますが、その後 `David` に上書きされています。 そのためコンソールには文字列 `David` が表示されます。
|
||||
|
||||
小規模なアプリケーションであれば、こうした問題は起きにくいかもしれまん。 しかし、コードの規模が大きくなると、変数を意図せずに誤って上書きしてしまう可能性が高くなります。 このような動作はエラーを返さないため、バグを見つけて修正することが難しくなります。
|
||||
|
||||
JavaScript の大きなアップデートとなった ES6 では、`var`キーワードに伴うこうした潜在的な問題を解決するために `let` というキーワードが導入されました。 以降のチャレンジでは ES6 の他の機能についても学習します。
|
||||
|
||||
上記のコードの `var` を `let` に置き換えた場合、結果はエラーになります。
|
||||
|
||||
```js
|
||||
let camper = "James";
|
||||
let camper = "David";
|
||||
```
|
||||
|
||||
エラーはブラウザーコンソールで確認できます。
|
||||
|
||||
`var` とは違って、`let` を使用する場合は、同じ名前を持つ変数は一度だけしか宣言できません。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`let` キーワードだけを使用するようにコードを更新してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`var` がコード内に存在しない必要があります。
|
||||
|
||||
```js
|
||||
(getUserInput) => assert(!getUserInput('index').match(/var/g));
|
||||
```
|
||||
|
||||
`catName` は文字列 `Oliver` である必要があります。
|
||||
|
||||
```js
|
||||
assert(catName === 'Oliver');
|
||||
```
|
||||
|
||||
`catSound` は文字列 `Meow!` である必要があります。
|
||||
|
||||
```js
|
||||
assert(catSound === 'Meow!');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var catName = "Oliver";
|
||||
var catSound = "Meow!";
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let catName = "Oliver";
|
||||
let catSound = "Meow!";
|
||||
```
|
@ -0,0 +1,68 @@
|
||||
---
|
||||
id: bd7123c9c448eddfaeb5bdef
|
||||
title: 文字列の長さの取得
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cvmqEAd'
|
||||
forumTopicId: 18182
|
||||
dashedName: find-the-length-of-a-string
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
文字列変数または文字列リテラルの後に `.length` と記述することで、`String` 値の長さを取得することができます。
|
||||
|
||||
```js
|
||||
console.log("Alan Peter".length);
|
||||
```
|
||||
|
||||
この例では値 `10` がコンソールに表示されます。
|
||||
|
||||
たとえば、変数 `const firstName = "Ada"` を作成した場合、`firstName.length` プロパティを使用して文字列 `Ada` の長さを取得できます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`.length` プロパティを使用して、`lastName` 変数の文字数をカウントし、それを `lastNameLength` に代入してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`// Setup` セクションの変数宣言を変更しないでください。
|
||||
|
||||
```js
|
||||
assert(
|
||||
code.match(/let lastNameLength = 0;/) &&
|
||||
code.match(/const lastName = "Lovelace";/)
|
||||
);
|
||||
```
|
||||
|
||||
`lastNameLength` は 8 になる必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof lastNameLength !== 'undefined' && lastNameLength === 8);
|
||||
```
|
||||
|
||||
`lastName.length` のように、`.length` を使用して `lastName` の長さを取得する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/=\s*lastName\.length/g) && !code.match(/lastName\s*=\s*8/));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
let lastNameLength = 0;
|
||||
const lastName = "Lovelace";
|
||||
|
||||
// Only change code below this line
|
||||
lastNameLength = lastName;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let lastNameLength = 0;
|
||||
const lastName = "Lovelace";
|
||||
lastNameLength = lastName.length;
|
||||
```
|
@ -0,0 +1,67 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ae
|
||||
title: JavaScript で割り算の余りを求める
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cWP24Ub'
|
||||
forumTopicId: 18184
|
||||
dashedName: finding-a-remainder-in-javascript
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<dfn>剰余</dfn>演算子 `%` は 2 つの数値の割り算の余りを取得します。
|
||||
|
||||
**例**
|
||||
|
||||
<blockquote>5 % 2 = 1 なぜならば<br>Math.floor(5 / 2) = 2 (割り算の商)<br>2 * 2 = 4<br>5 - 4 = 1 (割り算の余り)</blockquote>
|
||||
|
||||
**使用例**
|
||||
数学では、ある数が偶数か奇数かを求めるために、その数を `2` で割った余りを調べることができます。
|
||||
|
||||
<blockquote>17 % 2 = 1 (17 は奇数)<br>48 % 2 = 0 (48 は偶数)</blockquote>
|
||||
|
||||
**注:** <dfn>剰余</dfn>演算子はしばしばモジュロ演算子と混同されることがあります。 剰余はモジュロと非常によく似ていますが、負数では正しく機能しません。
|
||||
|
||||
# --instructions--
|
||||
|
||||
<dfn>剰余</dfn> (`%`) 演算子を使用して、`remainder` が `11` を `3` で割った余りと等しくなるように設定してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
変数 `remainder` を初期化する必要があります。
|
||||
|
||||
```js
|
||||
assert(/(const|let|var)\s+?remainder/.test(code));
|
||||
```
|
||||
|
||||
`remainder` の値は `2` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(remainder === 2);
|
||||
```
|
||||
|
||||
`%` 演算子を使用してください。
|
||||
|
||||
```js
|
||||
assert(/\s+?remainder\s*?=\s*?.*%.*;?/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(y){return 'remainder = '+y;})(remainder);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const remainder = 0;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const remainder = 11 % 3;
|
||||
```
|
@ -0,0 +1,69 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb9bdef
|
||||
title: JavaScript で小数の乱数を生成する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cyWJJs3'
|
||||
forumTopicId: 18185
|
||||
dashedName: generate-random-fractions-with-javascript
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
乱数はランダムな動作を作成するのに便利です。
|
||||
|
||||
JavaScript には、`0` (含む) から `1` (含まない) の間の小数の乱数を生成する `Math.random()` 関数があります。 つまり、`Math.random()` は `0` を返すことはありますが、`1` を返すことは決してありません。
|
||||
|
||||
**注意:** [代入演算子による値の格納](/learn/javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator)の場合と同様に、すべての関数呼び出しは `return` が実行される前に解決されるため、`Math.random()` 関数の値を `return` することができます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`0` の代わりに乱数を返すように、`randomFraction` を変更してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`randomFraction` は乱数を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof randomFraction() === 'number');
|
||||
```
|
||||
|
||||
`randomFraction` が返す数値は小数である必要があります。
|
||||
|
||||
```js
|
||||
assert((randomFraction() + '').match(/\./g));
|
||||
```
|
||||
|
||||
`Math.random` を使用して、小数の乱数を生成する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/Math\.random/g).length >= 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){return randomFraction();})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function randomFraction() {
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
return 0;
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function randomFraction() {
|
||||
return Math.random();
|
||||
}
|
||||
```
|
@ -0,0 +1,90 @@
|
||||
---
|
||||
id: cf1111c1c12feddfaeb1bdef
|
||||
title: JavaScript で整数の乱数を生成する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cRn6bfr'
|
||||
forumTopicId: 18186
|
||||
dashedName: generate-random-whole-numbers-with-javascript
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
小数の乱数を生成できるのはとても便利ですが、それを利用して整数の乱数を生成すればさらに便利です。
|
||||
|
||||
<ol><li><code>Math.random()</code> を使用して小数の乱数を生成する</li><li>その小数の乱数に <code>20</code> を掛ける</li><li>別の関数 <code>Math.floor()</code> を使用して小数点以下の端数を切り捨て、最も近い整数を得る</li></ol>
|
||||
|
||||
`Math.random()` は決して `1` を返しません。そして切り捨てを行うため、`20` を取得する可能性はありません。 この方法では `0` ~ `19` の整数が得られます。
|
||||
|
||||
すべてをまとめると次のようなコードになります。
|
||||
|
||||
```js
|
||||
Math.floor(Math.random() * 20);
|
||||
```
|
||||
|
||||
`Math.random()` を呼び出して、その結果に 20 を掛け、その値を `Math.floor()` 関数に渡して端数を切り捨て、最も近い整数を求めています。
|
||||
|
||||
# --instructions--
|
||||
|
||||
ここで紹介した方法で、`0` ~ `9` の整数の乱数を生成して返してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`randomWholeNum` の結果は整数である必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
typeof randomWholeNum() === 'number' &&
|
||||
(function () {
|
||||
var r = randomWholeNum();
|
||||
return Math.floor(r) === r;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
`Math.random` を使用して、乱数を生成する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/Math.random/g).length >= 1);
|
||||
```
|
||||
|
||||
`Math.random` の結果に 10 を掛けて、0 ~ 9 の数値を求める必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
code.match(/\s*?Math.random\s*?\(\s*?\)\s*?\*\s*?10[\D]\s*?/g) ||
|
||||
code.match(/\s*?10\s*?\*\s*?Math.random\s*?\(\s*?\)\s*?/g)
|
||||
);
|
||||
```
|
||||
|
||||
`Math.floor` を使用して、小数点以下の端数を切り捨てる必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/Math.floor/g).length >= 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){return randomWholeNum();})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function randomWholeNum() {
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
return Math.random();
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function randomWholeNum() {
|
||||
return Math.floor(Math.random() * 10);
|
||||
}
|
||||
```
|
@ -0,0 +1,102 @@
|
||||
---
|
||||
id: cf1111c1c12feddfaeb2bdef
|
||||
title: ある範囲内の整数の乱数を生成する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cm83yu6'
|
||||
forumTopicId: 18187
|
||||
dashedName: generate-random-whole-numbers-within-a-range
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
前回は、ゼロから指定した数値までの整数の乱数を生成しましたが、2 つの特定の数値の範囲内で整数の乱数を生成することができます。
|
||||
|
||||
それには最小値 `min` と最大値 `max` を定義します。
|
||||
|
||||
ここでは次のような式を使用します。 このコードの動作を理解して確かめてみてください。
|
||||
|
||||
```js
|
||||
Math.floor(Math.random() * (max - min + 1)) + min
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
`myMin` から `myMax` までを範囲とする `randomRange` という関数を作成し、`myMin` 以上 `myMax` 以下の整数の乱数を返してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`randomRange` によって生成される最小の乱数は、最小値 `myMin` と等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert(calcMin === 5);
|
||||
```
|
||||
|
||||
`randomRange` によって生成される最大の乱数は、最大値 `myMax` と等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert(calcMax === 15);
|
||||
```
|
||||
|
||||
`randomRange` によって生成される乱数は小数ではなく、整数である必要があります。
|
||||
|
||||
```js
|
||||
assert(randomRange(0, 1) % 1 === 0);
|
||||
```
|
||||
|
||||
`randomRange` では `myMax` と `myMin` の両方を使用して、範囲内の乱数を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
code.match(/myMax/g).length > 1 &&
|
||||
code.match(/myMin/g).length > 2 &&
|
||||
code.match(/Math.floor/g) &&
|
||||
code.match(/Math.random/g)
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
var calcMin = 100;
|
||||
var calcMax = -100;
|
||||
for(var i = 0; i < 100; i++) {
|
||||
var result = randomRange(5,15);
|
||||
calcMin = Math.min(calcMin, result);
|
||||
calcMax = Math.max(calcMax, result);
|
||||
}
|
||||
(function(){
|
||||
if(typeof myRandom === 'number') {
|
||||
return "myRandom = " + myRandom;
|
||||
} else {
|
||||
return "myRandom undefined";
|
||||
}
|
||||
})()
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function randomRange(myMin, myMax) {
|
||||
// Only change code below this line
|
||||
return 0;
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function randomRange(myMin, myMax) {
|
||||
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
|
||||
}
|
||||
```
|
@ -0,0 +1,128 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244be
|
||||
title: グローバルスコープと関数
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cQM7mCN'
|
||||
forumTopicId: 18193
|
||||
dashedName: global-scope-and-functions
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript では、変数の効力の及ぶ範囲のことを<dfn>スコープ</dfn>と呼びます。 関数ブロックの外側で定義された変数は、<dfn>グローバル</dfn>のスコープを持ちます。 つまり、JavaScript コードのどこからでもその変数を参照することができます。
|
||||
|
||||
`let` キーワードまたは `const` キーワードを使用せずに宣言された変数は、自動的に `global` スコープで作成されます。 これは、コード内の他の場所で、または関数を再び実行するときに、意図しない結果を引き起こす可能性があります。 変数は常に `let` または `const` で宣言するようにしてください。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`let` または `const`を使用して、関数の外部で `myGlobal` という名前のグローバル変数を宣言し、 値 `10` で初期化してください。
|
||||
|
||||
関数 `fun1` の内側で、`let` キーワードまたは `const` キーワードを***使用せずに***、`oopsGlobal` に `5` を代入してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myGlobal` を定義する必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof myGlobal != 'undefined');
|
||||
```
|
||||
|
||||
`myGlobal` の値は `10` である必要があります。
|
||||
|
||||
```js
|
||||
assert(myGlobal === 10);
|
||||
```
|
||||
|
||||
`myGlobal` は `let` キーワードまたは `const` キーワードを使用して宣言する必要があります。
|
||||
|
||||
```js
|
||||
assert(/(let|const)\s+myGlobal/.test(code));
|
||||
```
|
||||
|
||||
`oopsGlobal` はグローバル変数で、値は `5` である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof oopsGlobal != 'undefined' && oopsGlobal === 5);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
var logOutput = "";
|
||||
var originalConsole = console
|
||||
function capture() {
|
||||
var nativeLog = console.log;
|
||||
console.log = function (message) {
|
||||
logOutput = message;
|
||||
if(nativeLog.apply) {
|
||||
nativeLog.apply(originalConsole, arguments);
|
||||
} else {
|
||||
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
|
||||
nativeLog(nativeMsg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function uncapture() {
|
||||
console.log = originalConsole.log;
|
||||
}
|
||||
var oopsGlobal;
|
||||
capture();
|
||||
```
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
fun1();
|
||||
fun2();
|
||||
uncapture();
|
||||
(function() { return logOutput || "console.log never called"; })();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Declare the myGlobal variable below this line
|
||||
|
||||
|
||||
function fun1() {
|
||||
// Assign 5 to oopsGlobal Here
|
||||
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
function fun2() {
|
||||
var output = "";
|
||||
if (typeof myGlobal != "undefined") {
|
||||
output += "myGlobal: " + myGlobal;
|
||||
}
|
||||
if (typeof oopsGlobal != "undefined") {
|
||||
output += " oopsGlobal: " + oopsGlobal;
|
||||
}
|
||||
console.log(output);
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myGlobal = 10;
|
||||
|
||||
function fun1() {
|
||||
oopsGlobal = 5;
|
||||
}
|
||||
|
||||
function fun2() {
|
||||
var output = "";
|
||||
if(typeof myGlobal != "undefined") {
|
||||
output += "myGlobal: " + myGlobal;
|
||||
}
|
||||
if(typeof oopsGlobal != "undefined") {
|
||||
output += " oopsGlobal: " + oopsGlobal;
|
||||
}
|
||||
console.log(output);
|
||||
}
|
||||
```
|
@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c0
|
||||
title: 関数のグローバルスコープとローカルスコープ
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c2QwKH2'
|
||||
forumTopicId: 18194
|
||||
dashedName: global-vs--local-scope-in-functions
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<dfn>ローカル</dfn>変数と<dfn>グローバル</dfn>変数で同じ名前を使用することが可能です。 その場合は、ローカル変数がグローバル変数よりも優先されます。
|
||||
|
||||
次の例を見てみましょう。
|
||||
|
||||
```js
|
||||
const someVar = "Hat";
|
||||
|
||||
function myFun() {
|
||||
const someVar = "Head";
|
||||
return someVar;
|
||||
}
|
||||
```
|
||||
|
||||
ローカルの変数が存在するため、関数 `myFun` は文字列 `Head` を返します。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`myOutfit` 関数にローカル変数を追加し、 `outerWear` の値を文字列 `sweater`で上書きしてください。
|
||||
|
||||
# --hints--
|
||||
|
||||
グローバルの `outerWear` の値を変更してはいけません。
|
||||
|
||||
```js
|
||||
assert(outerWear === 'T-Shirt');
|
||||
```
|
||||
|
||||
`myOutfit` は文字列 `sweater` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(myOutfit() === 'sweater');
|
||||
```
|
||||
|
||||
return ステートメントを変更してはいけません。
|
||||
|
||||
```js
|
||||
assert(/return outerWear/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const outerWear = "T-Shirt";
|
||||
|
||||
function myOutfit() {
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
return outerWear;
|
||||
}
|
||||
|
||||
myOutfit();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const outerWear = "T-Shirt";
|
||||
function myOutfit() {
|
||||
const outerWear = "sweater";
|
||||
return outerWear;
|
||||
}
|
||||
```
|
@ -0,0 +1,136 @@
|
||||
---
|
||||
id: 5664820f61c48e80c9fa476c
|
||||
title: ゴルフのプログラム
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c9ykNUR'
|
||||
forumTopicId: 18195
|
||||
dashedName: golf-code
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
[ゴルフ](https://en.wikipedia.org/wiki/Golf)の試合では、各ホールに `par` (パー) が設定されています。これは、ゴルファーがボールをカップインさせてホールアウトするのに要すると考えられる平均 `strokes` (ストローク) 数です。 `strokes`数 が `par` よりどれだけ多いか少ないかによって、異なるニックネームが付いています。
|
||||
|
||||
関数は引数として `par` と `strokes` を受け取ります。 次の表に対応した正しい文字列を返してください。この表はスコアの良い順 (最高から最低の順) にストロークを並べて記載しています。
|
||||
|
||||
<table class='table table-striped'><thead><tr><th>ストローク</th><th>戻り値</th></tr></thead><tbody><tr><td>1</td><td>"Hole-in-one!"</td></tr><tr><td><= par - 2</td><td>"Eagle"</td></tr><tr><td>par - 1</td><td>"Birdie"</td></tr><tr><td>par</td><td>"Par"</td></tr><tr><td>par + 1</td><td>"Bogey"</td></tr><tr><td>par + 2</td><td>"Double Bogey"</td></tr><tr><td>>= par + 3</td><td>"Go Home!"</td></tr></tbody></table>
|
||||
|
||||
`par` と `strokes` は常に正の数値になります。 すべての名称を含む配列をすでに追加してあります。
|
||||
|
||||
# --hints--
|
||||
|
||||
`golfScore(4, 1)` は文字列 `Hole-in-one!` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 1) === 'Hole-in-one!');
|
||||
```
|
||||
|
||||
`golfScore(4, 2)` は文字列 `Eagle` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 2) === 'Eagle');
|
||||
```
|
||||
|
||||
`golfScore(5, 2)` は文字列 `Eagle` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(golfScore(5, 2) === 'Eagle');
|
||||
```
|
||||
|
||||
`golfScore(4, 3)` は文字列 `Birdie` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 3) === 'Birdie');
|
||||
```
|
||||
|
||||
`golfScore(4, 4)` は文字列 `Par` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 4) === 'Par');
|
||||
```
|
||||
|
||||
`golfScore(1, 1)` は文字列 `Hole-in-one!` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(golfScore(1, 1) === 'Hole-in-one!');
|
||||
```
|
||||
|
||||
`golfScore(5, 5)` は文字列 `Par` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(golfScore(5, 5) === 'Par');
|
||||
```
|
||||
|
||||
`golfScore(4, 5)` は文字列 `Bogey` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 5) === 'Bogey');
|
||||
```
|
||||
|
||||
`golfScore(4, 6)` は文字列 `Double Bogey` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 6) === 'Double Bogey');
|
||||
```
|
||||
|
||||
`golfScore(4, 7)` は文字列 `Go Home!` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 7) === 'Go Home!');
|
||||
```
|
||||
|
||||
`golfScore(5, 9)` は文字列 `Go Home!` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(golfScore(5, 9) === 'Go Home!');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
|
||||
|
||||
function golfScore(par, strokes) {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
return "Change Me";
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
golfScore(5, 4);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function golfScore(par, strokes) {
|
||||
if (strokes === 1) {
|
||||
return "Hole-in-one!";
|
||||
}
|
||||
|
||||
if (strokes <= par - 2) {
|
||||
return "Eagle";
|
||||
}
|
||||
|
||||
if (strokes === par - 1) {
|
||||
return "Birdie";
|
||||
}
|
||||
|
||||
if (strokes === par) {
|
||||
return "Par";
|
||||
}
|
||||
|
||||
if (strokes === par + 1) {
|
||||
return "Bogey";
|
||||
}
|
||||
|
||||
if(strokes === par + 2) {
|
||||
return "Double Bogey";
|
||||
}
|
||||
|
||||
return "Go Home!";
|
||||
}
|
||||
```
|
@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ac
|
||||
title: JavaScript で数値をインクリメントする
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ca8GLT9'
|
||||
forumTopicId: 18201
|
||||
dashedName: increment-a-number-with-javascript
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`++` 演算子を使用して簡単に変数の値を 1 増やす (<dfn>インクリメント</dfn>する) ことができます。
|
||||
|
||||
```js
|
||||
i++;
|
||||
```
|
||||
|
||||
は次の式と等価です。
|
||||
|
||||
```js
|
||||
i = i + 1;
|
||||
```
|
||||
|
||||
**注:** この行全体が `i++;` となり、等号が不要になります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
コードを変更して `myVar` で `++` 演算子を使用してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myVar` は `88` に等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert(myVar === 88);
|
||||
```
|
||||
|
||||
代入演算子は使用しないでください。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/let\s*myVar\s*=\s*87;\s*\/*.*\s*([+]{2}\s*myVar|myVar\s*[+]{2});/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
`++` 演算子を使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(/[+]{2}\s*myVar|myVar\s*[+]{2}/.test(code));
|
||||
```
|
||||
|
||||
指定のコメントより上にあるコードを変更しないでください。
|
||||
|
||||
```js
|
||||
assert(/let myVar = 87;/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'myVar = ' + z;})(myVar);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let myVar = 87;
|
||||
|
||||
// Only change code below this line
|
||||
myVar = myVar + 1;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let myVar = 87;
|
||||
myVar++;
|
||||
```
|
@ -0,0 +1,50 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244a9
|
||||
title: 代入演算子で変数を初期化する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cWJ4Bfb'
|
||||
forumTopicId: 301171
|
||||
dashedName: initializing-variables-with-the-assignment-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
変数は宣言と同じ行の中で、初期値を使用して<dfn>初期化</dfn>するのが一般的です。
|
||||
|
||||
```js
|
||||
var myVar = 0;
|
||||
```
|
||||
|
||||
上記は `myVar` という名前の新しい変数を作成し、初期値 `0` を代入します。
|
||||
|
||||
# --instructions--
|
||||
|
||||
変数 `a` を `var` で定義し、値 `9` で初期化してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`a` を値 `9` で初期化する必要があります。
|
||||
|
||||
```js
|
||||
assert(/var\s+a\s*=\s*9(\s*;?\s*)$/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof a !== 'undefined') {(function(a){return "a = " + a;})(a);} else { (function() {return 'a is undefined';})(); }
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a = 9;
|
||||
```
|
@ -0,0 +1,114 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244db
|
||||
title: else if ステートメントを導入する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/caeJ2hm'
|
||||
forumTopicId: 18206
|
||||
dashedName: introducing-else-if-statements
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
処理を必要とする条件が複数ある場合は、`if` ステートメントと `else if` ステートメントをつなぎ合わせることができます。
|
||||
|
||||
```js
|
||||
if (num > 15) {
|
||||
return "Bigger than 15";
|
||||
} else if (num < 5) {
|
||||
return "Smaller than 5";
|
||||
} else {
|
||||
return "Between 5 and 15";
|
||||
}
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
`else if` ステートメントを使用するようにロジックを変えてください。
|
||||
|
||||
# --hints--
|
||||
|
||||
2 つ以上の `else` ステートメントを記述する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/else/g).length > 1);
|
||||
```
|
||||
|
||||
2 つ以上の `if` ステートメントを記述する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/if/g).length > 1);
|
||||
```
|
||||
|
||||
`if else` コードブロックごとに開始と終了の中括弧を置く必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
code.match(
|
||||
/if\s*\((.+)\)\s*\{[\s\S]+\}\s*else\s+if\s*\((.+)\)\s*\{[\s\S]+\}\s*else\s*\{[\s\S]+\s*\}/
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`testElseIf(0)` は文字列 `Smaller than 5` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testElseIf(0) === 'Smaller than 5');
|
||||
```
|
||||
|
||||
`testElseIf(5)` は文字列 `Between 5 and 10` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testElseIf(5) === 'Between 5 and 10');
|
||||
```
|
||||
|
||||
`testElseIf(7)` は文字列 `Between 5 and 10` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testElseIf(7) === 'Between 5 and 10');
|
||||
```
|
||||
|
||||
`testElseIf(10)` は文字列 `Between 5 and 10` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testElseIf(10) === 'Between 5 and 10');
|
||||
```
|
||||
|
||||
`testElseIf(12)` は文字列 `Greater than 10` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testElseIf(12) === 'Greater than 10');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testElseIf(val) {
|
||||
if (val > 10) {
|
||||
return "Greater than 10";
|
||||
}
|
||||
|
||||
if (val < 5) {
|
||||
return "Smaller than 5";
|
||||
}
|
||||
|
||||
return "Between 5 and 10";
|
||||
}
|
||||
|
||||
testElseIf(7);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testElseIf(val) {
|
||||
if(val > 10) {
|
||||
return "Greater than 10";
|
||||
} else if(val < 5) {
|
||||
return "Smaller than 5";
|
||||
} else {
|
||||
return "Between 5 and 10";
|
||||
}
|
||||
}
|
||||
```
|
@ -0,0 +1,106 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244da
|
||||
title: else ステートメントを導入する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cek4Efq'
|
||||
forumTopicId: 18207
|
||||
dashedName: introducing-else-statements
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`if` ステートメントの条件が true の場合は、その後のコードブロックが実行されます。 条件が false の場合はどうなるでしょうか? 通常は何も起こりません。 `else` ステートメントを記述することによって、 別のコードブロックを実行できます。
|
||||
|
||||
```js
|
||||
if (num > 10) {
|
||||
return "Bigger than 10";
|
||||
} else {
|
||||
return "10 or Less";
|
||||
}
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
複数の `if` ステートメントを 1 つの `if/else` ステートメントにまとめてください。
|
||||
|
||||
# --hints--
|
||||
|
||||
エディターでは 1 つの `if` ステートメントのみを記述する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/if/g).length === 1);
|
||||
```
|
||||
|
||||
`else` ステートメントを使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(/else/g.test(code));
|
||||
```
|
||||
|
||||
`testElse(4)` は文字列 `5 or Smaller` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testElse(4) === '5 or Smaller');
|
||||
```
|
||||
|
||||
`testElse(5)` は文字列 `5 or Smaller` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testElse(5) === '5 or Smaller');
|
||||
```
|
||||
|
||||
`testElse(6)` は文字列 `Bigger than 5` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testElse(6) === 'Bigger than 5');
|
||||
```
|
||||
|
||||
`testElse(10)` は文字列 `Bigger than 5` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(testElse(10) === 'Bigger than 5');
|
||||
```
|
||||
|
||||
指定のコメントの上または下のコードを変更しないでください。
|
||||
|
||||
```js
|
||||
assert(/let result = "";/.test(code) && /return result;/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testElse(val) {
|
||||
let result = "";
|
||||
// Only change code below this line
|
||||
|
||||
if (val > 5) {
|
||||
result = "Bigger than 5";
|
||||
}
|
||||
|
||||
if (val <= 5) {
|
||||
result = "5 or Smaller";
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
return result;
|
||||
}
|
||||
|
||||
testElse(4);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testElse(val) {
|
||||
let result = "";
|
||||
if(val > 5) {
|
||||
result = "Bigger than 5";
|
||||
} else {
|
||||
result = "5 or Smaller";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
@ -0,0 +1,69 @@
|
||||
---
|
||||
id: 56104e9e514f539506016a5c
|
||||
title: for ループによる奇数の繰り返し処理
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cm8n7T9'
|
||||
forumTopicId: 18212
|
||||
dashedName: iterate-odd-numbers-with-a-for-loop
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
for ステートメントのループ処理は必ずしも 1 回に 1 つずつ繰り返す必要はありません。 `final-expression` の部分を変更して偶数ずつカウントすることができます。
|
||||
|
||||
`i = 0` から始めて、`i < 10` の間、ループ処理を行います。 `i += 2` で、`i` をループごとに 2 ずつインクリメントします。
|
||||
|
||||
```js
|
||||
const ourArray = [];
|
||||
|
||||
for (let i = 0; i < 10; i += 2) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
```
|
||||
|
||||
`ourArray` には `[0, 2, 4, 6, 8]` が格納されます。 奇数をカウントできるように `initialization` の部分を変更してみましょう。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`for` ループを使用して、1 から 9 までの奇数を `myArray` に push してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
この作業には `for` ループを使用してください。
|
||||
|
||||
```js
|
||||
assert(/for\s*\([^)]+?\)/.test(code));
|
||||
```
|
||||
|
||||
`myArray` は `[1, 3, 5, 7, 9]` になる必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(myArray, [1, 3, 5, 7, 9]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myArray = [];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [];
|
||||
for (let i = 1; i < 10; i += 2) {
|
||||
myArray.push(i);
|
||||
}
|
||||
```
|
@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 5675e877dbd60be8ad28edc6
|
||||
title: for ループによる配列の繰り返し処理
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/caeR3HB'
|
||||
forumTopicId: 18216
|
||||
dashedName: iterate-through-an-array-with-a-for-loop
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript では配列の内容を繰り返し処理する作業をよく行います。 その方法の一つとして `for` ループを使用できます。 次のコードは、配列 `arr` の各要素をコンソールに出力します。
|
||||
|
||||
```js
|
||||
const arr = [10, 9, 8, 7, 6];
|
||||
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
console.log(arr[i]);
|
||||
}
|
||||
```
|
||||
|
||||
配列は 0 から始まるインデックスを持つことに注意してください。つまり、配列の最後のインデックスは `length - 1` になります。 このループ処理の条件は `i < arr.length` となっていて、`i` が `length` に等しくなるとループ処理を終了します。 この例では、最後の繰り返しは `i === 4` です。つまり、`i` が `arr.length - 1` に等しくなり、コンソールに `6` を出力します。 その後、`i` が加算されて `5` になると、`i < arr.length` が `false` となるため、ループ処理は終了します。
|
||||
|
||||
# --instructions--
|
||||
|
||||
変数 `total` を宣言し、`0` に初期化してください。 `for` ループを使用して、`myArr` 配列の各要素の値を `total` に追加してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`total` を宣言し、0 に初期化する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/(var|let|const)\s*?total\s*=\s*0.*?;?/));
|
||||
```
|
||||
|
||||
`total` は 20 と等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert(total === 20);
|
||||
```
|
||||
|
||||
`for` ループを使用して、`myArr` の繰り返し処理を行う必要があります。
|
||||
|
||||
```js
|
||||
assert(/for\s*\(/g.test(code) && /myArr\s*\[/g.test(code));
|
||||
```
|
||||
|
||||
値 20 を直接 `total` に代入しないでください。
|
||||
|
||||
```js
|
||||
assert(!__helpers.removeWhiteSpace(code).match(/total[=+-]0*[1-9]+/gm));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){if(typeof total !== 'undefined') { return "total = " + total; } else { return "total is undefined";}})()
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myArr = [2, 3, 4, 5, 6];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArr = [2, 3, 4, 5, 6];
|
||||
let total = 0;
|
||||
|
||||
for (let i = 0; i < myArr.length; i++) {
|
||||
total += myArr[i];
|
||||
}
|
||||
```
|
@ -0,0 +1,105 @@
|
||||
---
|
||||
id: 5a2efd662fb457916e1fe604
|
||||
title: JavaScript の do...while ループによる繰り返し処理
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cDqWGcp'
|
||||
forumTopicId: 301172
|
||||
dashedName: iterate-with-javascript-do---while-loops
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
次に学ぶループ処理のタイプは `do...while` ループと呼ばれるものです。 この処理が `do...while` ループと呼ばれるのは、最初にループ内のコードが無条件に 1 回実行 (`do`) され、その後、指定された条件が `true` と評価される間ずっと (`while`)、ループ処理が実行され続けるからです。
|
||||
|
||||
```js
|
||||
const ourArray = [];
|
||||
let i = 0;
|
||||
|
||||
do {
|
||||
ourArray.push(i);
|
||||
i++;
|
||||
} while (i < 5);
|
||||
```
|
||||
|
||||
上記の例も他のタイプのループと同様の処理を行い、配列の結果は `[0, 1, 2, 3, 4]` のようになります。 しかし、`do...while` は他のループとは違って、最初の評価で条件を満たさない場合の動作が異なります。 これを実際の例で見ていきましょう。次は `i < 5` である限りループ処理を実行する通常の `while` ループです。
|
||||
|
||||
```js
|
||||
const ourArray = [];
|
||||
let i = 5;
|
||||
|
||||
while (i < 5) {
|
||||
ourArray.push(i);
|
||||
i++;
|
||||
}
|
||||
```
|
||||
|
||||
この例では、`ourArray` の値を空の配列に初期化し、`i` の値を 5 に初期化しています。 この `while` ループを実行すると、`i` が 5 未満ではないため、条件の評価が `false` となり、ループ内のコードは実行されません。 その結果、`ourArray` には何も値が追加されません。上記の例のコードの実行がすべて完了しても `[]` のままです。 次に、`do...while` ループを見てみましょう。
|
||||
|
||||
```js
|
||||
const ourArray = [];
|
||||
let i = 5;
|
||||
|
||||
do {
|
||||
ourArray.push(i);
|
||||
i++;
|
||||
} while (i < 5);
|
||||
```
|
||||
|
||||
この例でも、`while` ループの場合と同じように、`i` の値を 5 に初期化しています。 次の行に進むと、評価すべき条件がないため、中括弧内のコードに進み、そのコードを実行します。 そこでは配列に要素を 1つ追加し、`i` をインクリメントしてから、条件の評価に移ります。 最後の行で最終的に条件 `i < 5` を評価するときは、`i` は 6 になっていて、条件を満たさないため、ループを抜けて終了します。 上記の例の最後では、`ourArray` の値は `[5]` になっています。 `do...while` ループはその性質上、ループ内のコードを少なくとも 1 回は必ず実行します。 `do...while` ループを利用して配列に値を push してみましょう。
|
||||
|
||||
# --instructions--
|
||||
|
||||
コードの `while` ループを `do...while` ループに変更して、`myArray` に値 `10` だけを push し、コードの実行終了時に `i` が `11` に等しくなるようなループ処理を行ってください。
|
||||
|
||||
# --hints--
|
||||
|
||||
この課題には `do...while` ループを使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/do/g));
|
||||
```
|
||||
|
||||
`myArray` は `[10]` に等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(myArray, [10]);
|
||||
```
|
||||
|
||||
`i` は `11` に等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(i, 11);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myArray = [];
|
||||
let i = 10;
|
||||
|
||||
// Only change code below this line
|
||||
while (i < 5) {
|
||||
myArray.push(i);
|
||||
i++;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [];
|
||||
let i = 10;
|
||||
do {
|
||||
myArray.push(i);
|
||||
i++;
|
||||
} while (i < 5)
|
||||
```
|
@ -0,0 +1,81 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb5bdef
|
||||
title: JavaScript の for ループによる繰り返し処理
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c9yNVCe'
|
||||
forumTopicId: 18219
|
||||
dashedName: iterate-with-javascript-for-loops
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
ループ処理によって、同じコードを何度も実行できます。
|
||||
|
||||
JavaScript のループで最も一般的なのは、特定の回数を実行する `for` ループと呼ばれるものです。
|
||||
|
||||
for ループは、セミコロンで区切られた 3 つの任意の式を使用して
|
||||
|
||||
`for (a; b; c)` のように宣言します。ここで `a` は初期化式、`b` は条件式、`c` は最終式です。
|
||||
|
||||
初期化式は、ループが開始される前に 1 回だけ実行されます。 通常はループ変数の定義と設定に使用します。
|
||||
|
||||
条件式は各ループ処理の最初に評価され、`true` である限り繰り返されます。 繰り返し処理の開始時に条件が `false` になると、ループの実行が停止します。 したがって、条件の最初の評価が false の場合はループは決して実行されません。
|
||||
|
||||
最終式は各ループ処理の最後、次の条件評価の前に実行されます。通常はループカウンターのインクリメントまたはデクリメントに使用します。
|
||||
|
||||
次の例では、`i = 0` で初期化し、条件 `i < 5` が true である間、繰り返し処理を実行します。 最終式の `i++` により、各ループ処理で `i` を `1` ずつインクリメントします。
|
||||
|
||||
```js
|
||||
const ourArray = [];
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
```
|
||||
|
||||
`ourArray` の値は `[0, 1, 2, 3, 4]` となります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`for` ループを使用して、1 から 5 までの値を `myArray` に push してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
この課題には `for` ループを使用してください。
|
||||
|
||||
```js
|
||||
assert(/for\s*\([^)]+?\)/.test(code));
|
||||
```
|
||||
|
||||
`myArray` は `[1, 2, 3, 4, 5]` となる必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(myArray, [1, 2, 3, 4, 5]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if (typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myArray = [];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [];
|
||||
for (let i = 1; i < 6; i++) {
|
||||
myArray.push(i);
|
||||
}
|
||||
```
|
@ -0,0 +1,75 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb1bdef
|
||||
title: JavaScript の while ループによる繰り返し処理
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c8QbnCM'
|
||||
forumTopicId: 18220
|
||||
dashedName: iterate-with-javascript-while-loops
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
ループ処理によって、同じコードを何度も実行できます。
|
||||
|
||||
まず最初に `while` ループと呼ばれるループ型を学習します。こう呼ばれるのは特定の条件が true である間 (while) は実行され、条件が true でなくなると終了するためです。
|
||||
|
||||
```js
|
||||
const ourArray = [];
|
||||
let i = 0;
|
||||
|
||||
while (i < 5) {
|
||||
ourArray.push(i);
|
||||
i++;
|
||||
}
|
||||
```
|
||||
|
||||
上記のコード例では、`while` ループは 5 回実行され、0 から 4 までの数字を `ourArray` に追加します。
|
||||
|
||||
while ループを利用して配列に値を push してみましょう。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`while` ループを使用して、`myArray` に数字 5 ~ 0 を降順に追加してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
この課題には `while` ループを使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/while/g));
|
||||
```
|
||||
|
||||
`myArray` は `[5, 4, 3, 2, 1, 0]` となる必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(myArray, [5, 4, 3, 2, 1, 0]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myArray = [];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [];
|
||||
let i = 5;
|
||||
while (i >= 0) {
|
||||
myArray.push(i);
|
||||
i--;
|
||||
}
|
||||
```
|
@ -0,0 +1,86 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244bf
|
||||
title: ローカルスコープと関数
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cd62NhM'
|
||||
forumTopicId: 18227
|
||||
dashedName: local-scope-and-functions
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
関数内で宣言された変数や、関数のパラメーターは、<dfn>ローカル</dfn>のスコープを持ちます。 つまり、それらは関数内でのみ参照されます。
|
||||
|
||||
次は、`loc` というローカル変数を持つ関数 `myTest` です。
|
||||
|
||||
```js
|
||||
function myTest() {
|
||||
const loc = "foo";
|
||||
console.log(loc);
|
||||
}
|
||||
|
||||
myTest();
|
||||
console.log(loc);
|
||||
```
|
||||
|
||||
`myTest()` 関数の呼び出しでは、コンソールに文字列 `foo` が表示されます。 `console.log(loc)` の行はエラーをスローします。これは、関数の外側では `loc` が定義されていないためです。
|
||||
|
||||
# --instructions--
|
||||
|
||||
エディターにある 2 つの `console.log` は動作を確認するのに役立ちます。 コーディングを行いながらコンソールをチェックして、どのような変化が起きるかを確認してください。 `myLocalScope` 内でローカル変数 `myVar` を宣言し、テストを実行してください。
|
||||
|
||||
**注:** コンソールには `ReferenceError: myVar is not defined` と表示されますが、これが原因でテストが失敗することはありません。
|
||||
|
||||
# --hints--
|
||||
|
||||
コードにはグローバルの `myVar` 変数を含めないでください。
|
||||
|
||||
```js
|
||||
function declared() {
|
||||
myVar;
|
||||
}
|
||||
|
||||
assert.throws(declared, ReferenceError);
|
||||
```
|
||||
|
||||
ローカルの `myVar` 変数を追加する必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/functionmyLocalScope\(\)\{.*(var|let|const)myVar[\s\S]*}/.test(
|
||||
__helpers.removeWhiteSpace(code)
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function myLocalScope() {
|
||||
// Only change code below this line
|
||||
|
||||
console.log('inside myLocalScope', myVar);
|
||||
}
|
||||
myLocalScope();
|
||||
|
||||
// Run and check the console
|
||||
// myVar is not defined outside of myLocalScope
|
||||
console.log('outside myLocalScope', myVar);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function myLocalScope() {
|
||||
// Only change code below this line
|
||||
let myVar;
|
||||
console.log('inside myLocalScope', myVar);
|
||||
}
|
||||
myLocalScope();
|
||||
|
||||
// Run and check the console
|
||||
// myVar is not defined outside of myLocalScope
|
||||
console.log('outside myLocalScope', myVar);
|
||||
```
|
@ -0,0 +1,109 @@
|
||||
---
|
||||
id: 5690307fddb111c6084545d7
|
||||
title: if else ステートメントの論理的順序
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cwNvMUV'
|
||||
forumTopicId: 18228
|
||||
dashedName: logical-order-in-if-else-statements
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`if` ステートメント、`else if` ステートメントでは、順序が重要です。
|
||||
|
||||
関数は上から下へと実行されるので、どのステートメントが先に来るかに気をつけるようにしてください。
|
||||
|
||||
例として 2 つの関数を考えてみましょう。
|
||||
|
||||
まず 1 つ目です。
|
||||
|
||||
```js
|
||||
function foo(x) {
|
||||
if (x < 1) {
|
||||
return "Less than one";
|
||||
} else if (x < 2) {
|
||||
return "Less than two";
|
||||
} else {
|
||||
return "Greater than or equal to two";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2 つ目は 1 つ目のステートメントの順序を入れ替えただけのものです。
|
||||
|
||||
```js
|
||||
function bar(x) {
|
||||
if (x < 2) {
|
||||
return "Less than two";
|
||||
} else if (x < 1) {
|
||||
return "Less than one";
|
||||
} else {
|
||||
return "Greater than or equal to two";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
これら 2 つの関数は見かけはほとんど同じですが、両方に数値を渡すと異なる出力が得られます。
|
||||
|
||||
```js
|
||||
foo(0)
|
||||
bar(0)
|
||||
```
|
||||
|
||||
`foo(0)` は文字列 `Less than one` を返し、`bar(0)` は文字列 `Less than two` を返します。
|
||||
|
||||
# --instructions--
|
||||
|
||||
すべてのケースで適切なステートメントを返すように、関数内のロジックの順序を変更してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`orderMyLogic(4)` は文字列 `Less than 5` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(orderMyLogic(4) === 'Less than 5');
|
||||
```
|
||||
|
||||
`orderMyLogic(6)` は文字列 `Less than 10` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(orderMyLogic(6) === 'Less than 10');
|
||||
```
|
||||
|
||||
`orderMyLogic(11)` は文字列 `Greater than or equal to 10` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(orderMyLogic(11) === 'Greater than or equal to 10');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function orderMyLogic(val) {
|
||||
if (val < 10) {
|
||||
return "Less than 10";
|
||||
} else if (val < 5) {
|
||||
return "Less than 5";
|
||||
} else {
|
||||
return "Greater than or equal to 10";
|
||||
}
|
||||
}
|
||||
|
||||
orderMyLogic(7);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function orderMyLogic(val) {
|
||||
if(val < 5) {
|
||||
return "Less than 5";
|
||||
} else if (val < 10) {
|
||||
return "Less than 10";
|
||||
} else {
|
||||
return "Greater than or equal to 10";
|
||||
}
|
||||
}
|
||||
```
|
@ -0,0 +1,90 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392cc
|
||||
title: pop() による配列の操作
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cRbVZAB'
|
||||
forumTopicId: 18236
|
||||
dashedName: manipulate-arrays-with-pop
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
配列内のデータを変更する別の方法として、`.pop()` 関数を使用できます。
|
||||
|
||||
`.pop()` は配列の末尾の値を取り出すのに使用します。 pop で取り出した値は、変数に代入して格納できます。 `.pop()` は配列から最後の要素を削除してその要素を返す、と言うこともできます。
|
||||
|
||||
配列から任意の型の項目 (数値、文字列、さらにはネストされた配列) を取り出すことが可能です。
|
||||
|
||||
```js
|
||||
const threeArr = [1, 4, 6];
|
||||
const oneDown = threeArr.pop();
|
||||
console.log(oneDown);
|
||||
console.log(threeArr);
|
||||
```
|
||||
|
||||
最初の `console.log` は値 `6`を表示し、2 番目は値 `[1, 4]` を表示します。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`.pop()` 関数を使用して、`myArray` の最後のアイテムを削除し、取り出した値を新しい変数 `removedFromMyArray` に代入してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myArray` は `[["John", 23]]` のみを含む必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (d) {
|
||||
if (d[0][0] == 'John' && d[0][1] === 23 && d[1] == undefined) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myArray)
|
||||
);
|
||||
```
|
||||
|
||||
`myArray` で `pop()` を使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code));
|
||||
```
|
||||
|
||||
`removedFromMyArray` は `["cat", 2]` のみを含む必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (d) {
|
||||
if (d[0] == 'cat' && d[1] === 2 && d[2] == undefined) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(removedFromMyArray)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if (typeof removedFromMyArray !== 'undefined') (function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myArray = [["John", 23], ["cat", 2]];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [["John", 23], ["cat", 2]];
|
||||
const removedFromMyArray = myArray.pop();
|
||||
```
|
@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392cb
|
||||
title: push() による配列の操作
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cnqmVtJ'
|
||||
forumTopicId: 18237
|
||||
dashedName: manipulate-arrays-with-push
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`push()` 関数を使用して、配列の末尾に簡単にデータを追加することができます。
|
||||
|
||||
`.push()` は 1 つ以上の<dfn>パラメーター</dfn>を受け取り、それらを配列の末尾に「プッシュ (押し込み)」します。
|
||||
|
||||
例:
|
||||
|
||||
```js
|
||||
const arr1 = [1, 2, 3];
|
||||
arr1.push(4);
|
||||
|
||||
const arr2 = ["Stimpson", "J", "cat"];
|
||||
arr2.push(["happy", "joy"]);
|
||||
```
|
||||
|
||||
`arr1` の値は `[1, 2, 3, 4]` になり、`arr2` の値は `["Stimpson", "J", "cat", ["happy", "joy"]]` になります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`["dog", 3]` を `myArray` 変数の末尾に push してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myArray` は `[["John", 23], ["cat", 2], ["dog", 3]]` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (d) {
|
||||
if (
|
||||
d[2] != undefined &&
|
||||
d[0][0] == 'John' &&
|
||||
d[0][1] === 23 &&
|
||||
d[2][0] == 'dog' &&
|
||||
d[2][1] === 3 &&
|
||||
d[2].length == 2
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myArray)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'myArray = ' + JSON.stringify(z);})(myArray);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myArray = [["John", 23], ["cat", 2]];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [["John", 23], ["cat", 2]];
|
||||
myArray.push(["dog",3]);
|
||||
```
|
@ -0,0 +1,88 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392cd
|
||||
title: shift() による配列の操作
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cRbVETW'
|
||||
forumTopicId: 18238
|
||||
dashedName: manipulate-arrays-with-shift
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`pop()` は常に配列の最後の要素を削除します。 では、最初の要素を削除したい場合はどうすればいいでしょうか?
|
||||
|
||||
そのような場合に使用するのが `.shift()` です。 これは `.pop()` と同様の動作をしますが、最後の要素ではなく最初の要素を削除します。
|
||||
|
||||
例:
|
||||
|
||||
```js
|
||||
const ourArray = ["Stimpson", "J", ["cat"]];
|
||||
const removedFromOurArray = ourArray.shift();
|
||||
```
|
||||
|
||||
`removedFromOurArray` の値は文字列 `Stimpson` となり、`ourArray` は `["J", ["cat"]]` となります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`.shift()` 関数を使用して、`myArray` の最初のアイテムを削除し、取り出した値を新しい変数 `removedFromMyArray` に代入してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myArray` は `[["dog", 3]]` となる必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (d) {
|
||||
if (d[0][0] == 'dog' && d[0][1] === 3 && d[1] == undefined) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myArray)
|
||||
);
|
||||
```
|
||||
|
||||
`removedFromMyArray` は `["John", 23]` を含む必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (d) {
|
||||
if (
|
||||
d[0] == 'John' &&
|
||||
d[1] === 23 &&
|
||||
typeof removedFromMyArray === 'object'
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(removedFromMyArray)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if (typeof removedFromMyArray !== 'undefined') (function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myArray = [["John", 23], ["dog", 3]];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [["John", 23], ["dog", 3]];
|
||||
|
||||
// Only change code below this line
|
||||
const removedFromMyArray = myArray.shift();
|
||||
```
|
@ -0,0 +1,79 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392ce
|
||||
title: unshift() による配列の操作
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ckNDESv'
|
||||
forumTopicId: 18239
|
||||
dashedName: manipulate-arrays-with-unshift
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`shift` を使用して配列の先頭から要素を取り出すだけでなく、`unshift` を使用して配列の先頭に要素を追加することも可能です。
|
||||
|
||||
`.unshift()` は `.push()` とまったく同様の動作をします。ただし、`unshift()` は配列の末尾に要素を追加するのではなく、配列の先頭に要素を追加します。
|
||||
|
||||
例:
|
||||
|
||||
```js
|
||||
const ourArray = ["Stimpson", "J", "cat"];
|
||||
ourArray.shift();
|
||||
ourArray.unshift("Happy");
|
||||
```
|
||||
|
||||
`shift` の後、`ourArray` の値は `["J", "cat"]` となります。 `unshift` の後、`ourArray` の値は `["Happy", "J", "cat"]` となります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`unshift()` を使用して、`myArray` 変数の先頭に `["Paul", 35]` を追加してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myArray` は `[["Paul", 35], ["dog", 3]]` となる必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (d) {
|
||||
if (
|
||||
typeof d[0] === 'object' &&
|
||||
d[0][0] == 'Paul' &&
|
||||
d[0][1] === 35 &&
|
||||
d[1][0] != undefined &&
|
||||
d[1][0] == 'dog' &&
|
||||
d[1][1] != undefined &&
|
||||
d[1][1] == 3
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myArray)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(y, z){return 'myArray = ' + JSON.stringify(y);})(myArray);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myArray = [["John", 23], ["dog", 3]];
|
||||
myArray.shift();
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [["John", 23], ["dog", 3]];
|
||||
myArray.shift();
|
||||
myArray.unshift(["Paul", 35]);
|
||||
```
|
@ -0,0 +1,179 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244cb
|
||||
title: 複雑なオブジェクトの操作
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c9yNMfR'
|
||||
forumTopicId: 18208
|
||||
dashedName: manipulating-complex-objects
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
場合によっては、データを柔軟な<dfn>データ構造</dfn>に保存したいことがあります。 JavaScript オブジェクトは、柔軟性のあるデータを扱う方法の一つです。 オブジェクトでは、<dfn>文字列</dfn>、<dfn>数値</dfn>、<dfn>ブール値</dfn>、<dfn>配列</dfn>、<dfn>関数</dfn>、<dfn>オブジェクト</dfn>の任意の組み合わせを使用できます。
|
||||
|
||||
次は複雑なデータ構造の例です。
|
||||
|
||||
```js
|
||||
const ourMusic = [
|
||||
{
|
||||
"artist": "Daft Punk",
|
||||
"title": "Homework",
|
||||
"release_year": 1997,
|
||||
"formats": [
|
||||
"CD",
|
||||
"Cassette",
|
||||
"LP"
|
||||
],
|
||||
"gold": true
|
||||
}
|
||||
];
|
||||
```
|
||||
|
||||
これは内部にオブジェクトを 1 つ含む配列です。 このオブジェクトには、アルバムに関するさまざまな<dfn>メタデータ</dfn>があります。 また、ネストされた `formats` という配列もあります。 アルバムレコードを追加したい場合は、最上位の配列にレコードを追加することで実現できます。 オブジェクトではデータがプロパティに保持されます。プロパティはキーと値の組み合わせの形式を持ちます。 上記の例では、`"artist": "Daft Punk"` はプロパティで、`artist` というキーと `Daft Punk` という値を持っています。 データの格納に使用される関連のデータ交換形式として、[JavaScript Object Notation](http://www.json.org/) (`JSON`) という形式があります。
|
||||
|
||||
```json
|
||||
{
|
||||
"artist": "Daft Punk",
|
||||
"title": "Homework",
|
||||
"release_year": 1997,
|
||||
"formats": [
|
||||
"CD",
|
||||
"Cassette",
|
||||
"LP"
|
||||
],
|
||||
"gold": true
|
||||
}
|
||||
```
|
||||
|
||||
**注:** 配列内では、最後のオブジェクトを除くすべてのオブジェクトの後にコンマを置く必要があります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`myMusic` 配列に新しいアルバムを追加してください。 文字列の `artist` と `title`、数値の `release_year`、文字列配列の `formats` を追加してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myMusic` は配列である必要があります。
|
||||
|
||||
```js
|
||||
assert(Array.isArray(myMusic));
|
||||
```
|
||||
|
||||
`myMusic` には、少なくとも 2 つの要素が必要です。
|
||||
|
||||
```js
|
||||
assert(myMusic.length > 1);
|
||||
```
|
||||
|
||||
`myMusic` 配列の要素はオブジェクトである必要があります。
|
||||
|
||||
```js
|
||||
myMusic.forEach(object => {assert.typeOf(object, 'object')})
|
||||
```
|
||||
|
||||
`myMusic` 内のオブジェクトには、少なくとも 4 つのプロパティが必要です。
|
||||
|
||||
```js
|
||||
myMusic.forEach(object => {assert(Object.keys(object).length > 3); });
|
||||
```
|
||||
|
||||
`myMusic` 内のオブジェクトには、文字列であるプロパティ `artist` が含まれている必要があります。
|
||||
|
||||
```js
|
||||
myMusic.forEach(object => {
|
||||
assert.containsAllKeys(object, ['artist']);
|
||||
assert.typeOf(object.artist, 'string')
|
||||
})
|
||||
```
|
||||
|
||||
`myMusic` 内のオブジェクトには、文字列であるプロパティ `title` が含まれている必要があります。
|
||||
|
||||
```js
|
||||
myMusic.forEach(object => {
|
||||
assert.containsAllKeys(object, ['title']);
|
||||
assert.typeOf(object.title, 'string')
|
||||
})
|
||||
```
|
||||
|
||||
`myMusic` 内のオブジェクトには、数値であるプロパティ `release_year` が含まれている必要があります。
|
||||
|
||||
```js
|
||||
myMusic.forEach(object => {
|
||||
assert.containsAllKeys(object, ['release_year']);
|
||||
assert.typeOf(object.release_year, 'number')
|
||||
})
|
||||
```
|
||||
|
||||
`myMusic` 内のオブジェクトには、配列であるプロパティ `formats` が含まれている必要があります。
|
||||
|
||||
```js
|
||||
myMusic.forEach(object => {
|
||||
assert.containsAllKeys(object, ['formats']);
|
||||
assert.typeOf(object.formats, 'array')
|
||||
})
|
||||
```
|
||||
|
||||
`formats` は少なくとも 2 つの要素を持つ文字列配列である必要があります。
|
||||
|
||||
```js
|
||||
myMusic.forEach(object => {
|
||||
object.formats.forEach(format => {
|
||||
assert.typeOf(format, 'string')
|
||||
});
|
||||
assert.isAtLeast(object.formats.length, 2)
|
||||
})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(x){ if (Array.isArray(x)) { return JSON.stringify(x); } return "myMusic is not an array"})(myMusic);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const myMusic = [
|
||||
{
|
||||
"artist": "Billy Joel",
|
||||
"title": "Piano Man",
|
||||
"release_year": 1973,
|
||||
"formats": [
|
||||
"CD",
|
||||
"8T",
|
||||
"LP"
|
||||
],
|
||||
"gold": true
|
||||
}
|
||||
];
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myMusic = [
|
||||
{
|
||||
"artist": "Billy Joel",
|
||||
"title": "Piano Man",
|
||||
"release_year": 1973,
|
||||
"formats": [
|
||||
"CS",
|
||||
"8T",
|
||||
"LP" ],
|
||||
"gold": true
|
||||
},
|
||||
{
|
||||
"artist": "ABBA",
|
||||
"title": "Ring Ring",
|
||||
"release_year": 1973,
|
||||
"formats": [
|
||||
"CS",
|
||||
"8T",
|
||||
"LP",
|
||||
"CD",
|
||||
]
|
||||
}
|
||||
];
|
||||
```
|
@ -0,0 +1,87 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb8bdef
|
||||
title: インデックスによる配列データの変更
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/czQM4A8'
|
||||
forumTopicId: 18241
|
||||
dashedName: modify-array-data-with-indexes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
文字列とは異なり、配列の項目は<dfn>ミュータブル</dfn>であり、配列が `const` で宣言されている場合であっても自由に変更することができます。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
const ourArray = [50, 40, 30];
|
||||
ourArray[0] = 15;
|
||||
```
|
||||
|
||||
`ourArray` の値は `[15, 40, 30]` となります。
|
||||
|
||||
**注:** 「`array [0]`」のように、配列名と角括弧 (ブラケット) の間にスペースを入れないでください。 JavaScript はこれを正しく処理できますが、このコードを読む他のプログラマーを混乱させる恐れがあります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`myArray` のインデックス `0` に格納されているデータの値を `45` に変更してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myArray` は `[45, 64, 99]` となる必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
typeof myArray != 'undefined' &&
|
||||
myArray[0] == 45 &&
|
||||
myArray[1] == 64 &&
|
||||
myArray[2] == 99
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
正しいインデックスを使用して、`myArray` の値を変更する必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (code.match(/myArray\[0\]\s*=\s*/g)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myArray = [18, 64, 99];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [18, 64, 99];
|
||||
myArray[0] = 45;
|
||||
```
|
@ -0,0 +1,149 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244df
|
||||
title: switch ステートメントの複数の同一オプション
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cdBKWCV'
|
||||
forumTopicId: 18242
|
||||
dashedName: multiple-identical-options-in-switch-statements
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`switch` ステートメントの `case` で `break` ステートメントを省略すると、`break` が現れるまで、その後の `case` ステートメント (1 つ以上) が実行されます。 同じ出力となる複数の入力がある場合、`switch` ステートメントで次のように表現できます。
|
||||
|
||||
```js
|
||||
let result = "";
|
||||
switch(val) {
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
result = "1, 2, or 3";
|
||||
break;
|
||||
case 4:
|
||||
result = "4 alone";
|
||||
}
|
||||
```
|
||||
|
||||
1、2、3 の case はすべて同じ結果を生成します。
|
||||
|
||||
# --instructions--
|
||||
|
||||
次の範囲の `answer` を設定するように switch ステートメントを記述してください。
|
||||
`1 ~ 3` - `Low`
|
||||
`4 ~ 6` - `Mid`
|
||||
`7 ~ 9` - `High`
|
||||
|
||||
**注:** 範囲内の数値ごとに `case` ステートメントが必要です。
|
||||
|
||||
# --hints--
|
||||
|
||||
`sequentialSizes(1)` は文字列 `Low` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(1) === 'Low');
|
||||
```
|
||||
|
||||
`sequentialSizes(2)` は文字列 `Low` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(2) === 'Low');
|
||||
```
|
||||
|
||||
`sequentialSizes(3)` は文字列 `Low` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(3) === 'Low');
|
||||
```
|
||||
|
||||
`sequentialSizes(4)` は文字列 `Mid` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(4) === 'Mid');
|
||||
```
|
||||
|
||||
`sequentialSizes(5)` は文字列 `Mid` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(5) === 'Mid');
|
||||
```
|
||||
|
||||
`sequentialSizes(6)` は文字列 `Mid` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(6) === 'Mid');
|
||||
```
|
||||
|
||||
`sequentialSizes(7)` は文字列 `High` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(7) === 'High');
|
||||
```
|
||||
|
||||
`sequentialSizes(8)` は文字列 `High` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(8) === 'High');
|
||||
```
|
||||
|
||||
`sequentialSizes(9)` は文字列 `High` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(9) === 'High');
|
||||
```
|
||||
|
||||
`if` ステートメントまたは `else` ステートメントを使用しないでください。
|
||||
|
||||
```js
|
||||
assert(!/else/g.test(code) || !/if/g.test(code));
|
||||
```
|
||||
|
||||
9 つの `case` ステートメントが必要です。
|
||||
|
||||
```js
|
||||
assert(code.match(/case/g).length === 9);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sequentialSizes(val) {
|
||||
let answer = "";
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
return answer;
|
||||
}
|
||||
|
||||
sequentialSizes(1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sequentialSizes(val) {
|
||||
let answer = "";
|
||||
|
||||
switch(val) {
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
answer = "Low";
|
||||
break;
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
answer = "Mid";
|
||||
break;
|
||||
case 7:
|
||||
case 8:
|
||||
case 9:
|
||||
answer = "High";
|
||||
}
|
||||
|
||||
return answer;
|
||||
}
|
||||
```
|
@ -0,0 +1,52 @@
|
||||
---
|
||||
id: bd7993c9c69feddfaeb7bdef
|
||||
title: JavaScript で 2 つの小数を掛け合わせる
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ce2GeHq'
|
||||
forumTopicId: 301173
|
||||
dashedName: multiply-two-decimals-with-javascript
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript では小数の計算も整数と同様に行えます。
|
||||
|
||||
2 つの小数を掛け合わせて積を計算してみましょう。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`0.0` を変更して、積が `5.0` と等しくなるようにしてください。
|
||||
|
||||
# --hints--
|
||||
|
||||
変数 `product` は `5.0` に等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert(product === 5.0);
|
||||
```
|
||||
|
||||
`*` 演算子を使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(/\*/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(y){return 'product = '+y;})(product);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const product = 2.0 * 0.0;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const product = 2.0 * 2.5;
|
||||
```
|
@ -0,0 +1,60 @@
|
||||
---
|
||||
id: cf1231c1c11feddfaeb5bdef
|
||||
title: JavaScript で 2 つの数値を掛け算する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cP3y3Aq'
|
||||
forumTopicId: 18243
|
||||
dashedName: multiply-two-numbers-with-javascript
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
数値どうしの掛け算をすることもできます。
|
||||
|
||||
JavaScript では 2 つの数値の掛け算に `*` 記号を使用します。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
const myVar = 13 * 13;
|
||||
```
|
||||
|
||||
`myVar` の値は `169` になります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`0` を変更して、掛け算の積が `80` と等しくなるようにしてください。
|
||||
|
||||
# --hints--
|
||||
|
||||
変数 `product` は 80 に等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert(product === 80);
|
||||
```
|
||||
|
||||
`*` 演算子を使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(/\*/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'product = '+z;})(product);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const product = 8 * 0;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const product = 8 * 10;
|
||||
```
|
@ -0,0 +1,51 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb7bdef
|
||||
title: ある配列を別の配列内にネストする
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/crZQZf8'
|
||||
forumTopicId: 18247
|
||||
dashedName: nest-one-array-within-another-array
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
次のように、配列を他の配列内にネストすることもできます。
|
||||
|
||||
```js
|
||||
const teams = [["Bulls", 23], ["White Sox", 45]];
|
||||
```
|
||||
|
||||
これは<dfn>多次元配列</dfn>とも呼ばれます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`myArray` という名前のネストされた配列を作成してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myArray` は、別の配列内にネストされた配列を少なくとも 1 つ持つ必要があります。
|
||||
|
||||
```js
|
||||
assert(Array.isArray(myArray) && myArray.some(Array.isArray));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Only change code below this line
|
||||
const myArray = [];
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = [[1, 2, 3]];
|
||||
```
|
@ -0,0 +1,92 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244e1
|
||||
title: ネストした for ループ
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cRn6GHM'
|
||||
forumTopicId: 18248
|
||||
dashedName: nesting-for-loops
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
多次元配列を処理する場合、前に学習した同じロジックを使用して、配列とサブ配列の両方をループ処理することができます。 例:
|
||||
|
||||
```js
|
||||
const arr = [
|
||||
[1, 2], [3, 4], [5, 6]
|
||||
];
|
||||
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
for (let j = 0; j < arr[i].length; j++) {
|
||||
console.log(arr[i][j]);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
これは `arr` の各サブ要素を一度に 1 つずつ出力します。 `arr[i]` 自体が配列であるため、内側のループでは `arr[i]` の `.length` をチェックしていることに注意してください。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`arr` のサブ配列のすべての数値の積を返すように、関数 `multiplyAll` を変更してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`multiplyAll([[1], [2], [3]])` は `6` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(multiplyAll([[1], [2], [3]]) === 6);
|
||||
```
|
||||
|
||||
`multiplyAll([[1, 2], [3, 4], [5, 6, 7]])` は `5040` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
multiplyAll([
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6, 7]
|
||||
]) === 5040
|
||||
);
|
||||
```
|
||||
|
||||
`multiplyAll([[5, 1], [0.2, 4, 0.5], [3, 9]])` は `54` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
multiplyAll([
|
||||
[5, 1],
|
||||
[0.2, 4, 0.5],
|
||||
[3, 9]
|
||||
]) === 54
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function multiplyAll(arr) {
|
||||
let product = 1;
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
return product;
|
||||
}
|
||||
|
||||
multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function multiplyAll(arr) {
|
||||
let product = 1;
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
for (let j = 0; j < arr[i].length; j++) {
|
||||
product *= arr[i][j];
|
||||
}
|
||||
}
|
||||
return product;
|
||||
}
|
||||
```
|
@ -0,0 +1,120 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244bd
|
||||
title: 引数を使用して関数に値を渡す
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cy8rahW'
|
||||
forumTopicId: 18254
|
||||
dashedName: passing-values-to-functions-with-arguments
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<dfn>パラメーター</dfn>は、関数が呼び出されたときに関数に入力される値のプレイスホルダーとして機能する変数です。 通常、関数を定義するときは 1 つ以上のパラメーターを一緒に定義します。 関数が呼び出されるときに入力される (または<dfn>「渡される」</dfn>) 実際の値のことを<dfn>引数</dfn>と呼びます。
|
||||
|
||||
次の関数は `param1` と `param2` の 2 つのパラメーターを持っています。
|
||||
|
||||
```js
|
||||
function testFun(param1, param2) {
|
||||
console.log(param1, param2);
|
||||
}
|
||||
```
|
||||
|
||||
この `testFun` を `testFun("Hello", "World");` のように呼び出し、 `Hello` と `World` の 2 つの文字列を引数として渡すことができます。 関数の内部では、`param1` は文字列 `Hello` に等しくなり、`param2` は文字列 `World` に等しくなります。 別の引数を付けて再び `testFun` を呼び出すことができ、その場合、パラメーターは新しい引数の値を受け取ります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
<ol><li>2 つの引数を受け取り、その合計を開発コンソールに出力する <code>functionWithArgs</code> という関数を作成してください。</li><li>2 つの数値を引数に取る関数を呼び出してください。</li></ol>
|
||||
|
||||
# --hints--
|
||||
|
||||
`functionWithArgs` は関数である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof functionWithArgs === 'function');
|
||||
```
|
||||
|
||||
`functionWithArgs(1,2)` は `3` を出力する必要があります。
|
||||
|
||||
```js
|
||||
if (typeof functionWithArgs === 'function') {
|
||||
capture();
|
||||
functionWithArgs(1, 2);
|
||||
uncapture();
|
||||
}
|
||||
assert(logOutput == 3);
|
||||
```
|
||||
|
||||
`functionWithArgs(7,9)` は `16` を出力する必要があります。
|
||||
|
||||
```js
|
||||
if (typeof functionWithArgs === 'function') {
|
||||
capture();
|
||||
functionWithArgs(7, 9);
|
||||
uncapture();
|
||||
}
|
||||
assert(logOutput == 16);
|
||||
```
|
||||
|
||||
2 つの数値を受け取る `functionWithArgs` を定義して呼び出す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/functionWithArgs\([-+]?\d*\.?\d*,[-+]?\d*\.?\d*\)/.test(
|
||||
code.replace(/\s/g, '')
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
var logOutput = "";
|
||||
var originalConsole = console
|
||||
function capture() {
|
||||
var nativeLog = console.log;
|
||||
console.log = function (message) {
|
||||
if(message) logOutput = JSON.stringify(message).trim();
|
||||
if(nativeLog.apply) {
|
||||
nativeLog.apply(originalConsole, arguments);
|
||||
} else {
|
||||
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
|
||||
nativeLog(nativeMsg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function uncapture() {
|
||||
console.log = originalConsole.log;
|
||||
}
|
||||
|
||||
capture();
|
||||
```
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
uncapture();
|
||||
|
||||
if (typeof functionWithArgs !== "function") {
|
||||
(function() { return "functionWithArgs is not defined"; })();
|
||||
} else {
|
||||
(function() { return logOutput || "console.log never called"; })();
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function functionWithArgs(a, b) {
|
||||
console.log(a + b);
|
||||
}
|
||||
functionWithArgs(10, 5);
|
||||
```
|
@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 599a789b454f2bbd91a3ff4d
|
||||
title: 異なる値を比較する練習
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cm8PqCa'
|
||||
forumTopicId: 301174
|
||||
dashedName: practice-comparing-different-values
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
前回の 2 つのチャレンジでは等価演算子 (`==`) と厳密等価演算子 (`===`) について学習しました。 簡単なおさらいを兼ねて、これらの演算子の使用についてもう少し練習をしてみましょう。
|
||||
|
||||
比較する値が同じ型でない場合、等価演算子では型変換を行ってから、それらの値を評価します。 しかし、厳密等価演算子では型変換を行うことなく、データ型と値をそのまま比較します。
|
||||
|
||||
**例**
|
||||
|
||||
`3 == '3'` では、JavaScript によって文字列から数値への型変換が行われるため、`true` を返します。 `3 === '3'` では、型が異なり、型変換も行われないため false を返します。
|
||||
|
||||
**注:** JavaScript では次のように `typeof` 演算子を使用して、変数または値の型を判断することができます。
|
||||
|
||||
```js
|
||||
typeof 3
|
||||
typeof '3'
|
||||
```
|
||||
|
||||
`typeof 3` は文字列 `number` を返し、`typeof '3'` は文字列 `string` を返します。
|
||||
|
||||
# --instructions--
|
||||
|
||||
エディターにある `compareEquality` 関数は、等価演算子を使用して 2 つの値を比較します。 値が厳密に等しい場合にのみ、文字列 `Equal` を返すように関数を変更してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`compareEquality(10, "10")` は文字列 `Not Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(compareEquality(10, '10') === 'Not Equal');
|
||||
```
|
||||
|
||||
`compareEquality("20", 20)` は文字列 `Not Equal` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(compareEquality('20', 20) === 'Not Equal');
|
||||
```
|
||||
|
||||
`===` 演算子を使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/===/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function compareEquality(a, b) {
|
||||
if (a == b) { // Change this line
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
|
||||
compareEquality(10, "10");
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function compareEquality(a,b) {
|
||||
if (a === b) {
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
```
|
@ -0,0 +1,144 @@
|
||||
---
|
||||
id: 5688e62ea601b2482ff8422b
|
||||
title: プロファイルの検索
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cDqW2Cg'
|
||||
forumTopicId: 18259
|
||||
dashedName: profile-lookup
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
連絡先リストのさまざまな人物を表すオブジェクトの配列があります。
|
||||
|
||||
引数として `name` とプロパティ (`prop`) を受け取る `lookUpProfile` という関数があらかじめ用意されています。
|
||||
|
||||
この関数では、`name` が実際の連絡先の `firstName` と一致するかどうかを調べ、指定されたプロパティ (`prop`) がその連絡先のプロパティと一致するかどうかを調べる必要があります。
|
||||
|
||||
両方が true の場合は、そのプロパティの "value" を返してください。
|
||||
|
||||
`name` がどの連絡先とも一致しない場合には、文字列 `No such contact` を返してください。
|
||||
|
||||
`prop` が、`name` と一致する連絡先のどの有効なプロパティとも一致しない場合には、文字列 `No such property` を返してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`lookUpProfile("Kristian", "lastName")` は文字列 `Vos` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(lookUpProfile('Kristian', 'lastName') === 'Vos');
|
||||
```
|
||||
|
||||
`lookUpProfile("Sherlock", "likes")` は `["Intriguing Cases", "Violin"]` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.deepEqual(lookUpProfile('Sherlock', 'likes'), [
|
||||
'Intriguing Cases',
|
||||
'Violin'
|
||||
]);
|
||||
```
|
||||
|
||||
`lookUpProfile("Harry", "likes")` は配列を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof lookUpProfile('Harry', 'likes') === 'object');
|
||||
```
|
||||
|
||||
`lookUpProfile("Bob", "number")` は文字列 `No such contact` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(lookUpProfile('Bob', 'number') === 'No such contact');
|
||||
```
|
||||
|
||||
`lookUpProfile("Bob", "potato")` は文字列 `No such contact` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(lookUpProfile('Bob', 'potato') === 'No such contact');
|
||||
```
|
||||
|
||||
`lookUpProfile("Akira", "address")` は文字列 `No such property` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(lookUpProfile('Akira', 'address') === 'No such property');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const contacts = [
|
||||
{
|
||||
firstName: "Akira",
|
||||
lastName: "Laine",
|
||||
number: "0543236543",
|
||||
likes: ["Pizza", "Coding", "Brownie Points"],
|
||||
},
|
||||
{
|
||||
firstName: "Harry",
|
||||
lastName: "Potter",
|
||||
number: "0994372684",
|
||||
likes: ["Hogwarts", "Magic", "Hagrid"],
|
||||
},
|
||||
{
|
||||
firstName: "Sherlock",
|
||||
lastName: "Holmes",
|
||||
number: "0487345643",
|
||||
likes: ["Intriguing Cases", "Violin"],
|
||||
},
|
||||
{
|
||||
firstName: "Kristian",
|
||||
lastName: "Vos",
|
||||
number: "unknown",
|
||||
likes: ["JavaScript", "Gaming", "Foxes"],
|
||||
},
|
||||
];
|
||||
|
||||
function lookUpProfile(name, prop) {
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
lookUpProfile("Akira", "likes");
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const contacts = [
|
||||
{
|
||||
firstName: "Akira",
|
||||
lastName: "Laine",
|
||||
number: "0543236543",
|
||||
likes: ["Pizza", "Coding", "Brownie Points"],
|
||||
},
|
||||
{
|
||||
firstName: "Harry",
|
||||
lastName: "Potter",
|
||||
number: "0994372684",
|
||||
likes: ["Hogwarts", "Magic", "Hagrid"],
|
||||
},
|
||||
{
|
||||
firstName: "Sherlock",
|
||||
lastName: "Holmes",
|
||||
number: "0487345643",
|
||||
likes: ["Intriguing Cases", "Violin"],
|
||||
},
|
||||
{
|
||||
firstName: "Kristian",
|
||||
lastName: "Vos",
|
||||
number: "unknown",
|
||||
likes: ["JavaScript", "Gaming", "Foxes"],
|
||||
},
|
||||
];
|
||||
function lookUpProfile(name, prop) {
|
||||
for (let i in contacts) {
|
||||
if (contacts[i].firstName === name) {
|
||||
return contacts[i][prop] || "No such property";
|
||||
}
|
||||
}
|
||||
return "No such contact";
|
||||
}
|
||||
```
|
@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b4
|
||||
title: 文字列を 1 種類の引用符で囲む
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cbQmnhM'
|
||||
forumTopicId: 18260
|
||||
dashedName: quoting-strings-with-single-quotes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript で<dfn>文字列</dfn>の値を記述するときは、開始と終了の引用符が同じ種類であればシングルクォートとダブルクォートのどちらも使用できます。 他のいくつかのプログラミング言語とは違い、JavaScript ではシングルクォートとダブルクォートは同様に機能します。
|
||||
|
||||
```js
|
||||
const doubleQuoteStr = "This is a string";
|
||||
const singleQuoteStr = 'This is also a string';
|
||||
```
|
||||
|
||||
1 つの文字列の中で両方の種類の引用符を使用したい場合、一方の引用符を他方の引用符の中で使用する必要があります。 たとえば、引用符で囲まれた会話を文字列に保存したい場合があります。 また、引用符で囲まれたさまざまな属性を持つ `<a>` タグをまとめて文字列の中に保存したいという場合もあります。
|
||||
|
||||
```js
|
||||
const conversation = 'Finn exclaims to Jake, "Algebraic!"';
|
||||
```
|
||||
|
||||
ただし、最も外側の引用符を文字列の中で使用する必要がある場合には、問題となります。 前述のように、文字列の開始と終了には同じ種類の引用符を使用します。 しかし、その同じ引用符が中間のどこかに存在する場合は、文字列が意図したよりも早く終了してしまいエラーとなります。
|
||||
|
||||
```js
|
||||
const goodStr = 'Jake asks Finn, "Hey, let\'s go on an adventure?"';
|
||||
const badStr = 'Finn responds, "Let's go!"';
|
||||
```
|
||||
|
||||
この例の `badStr` はエラーを返します。
|
||||
|
||||
上記の <dfn>goodStr</dfn> では、バックスラッシュ (日本語では円記号) `\` をエスケープ文字として使用することで、両方の引用符を問題なく使用することができます。
|
||||
|
||||
**注:** バックスラッシュ `\` を斜線 `/` と混同しないでください。 これらの動作は同じではありません。
|
||||
|
||||
# --instructions--
|
||||
|
||||
用意された文字列を、開始と終了をシングルクォートで囲んだ、エスケープ文字を含まない文字列に変更してください。
|
||||
|
||||
現在、文字列の中の `<a>` タグでは至る所でダブルクォートが使用されています。 外側の引用符をシングルクォートに変更して、エスケープ文字を削除できるようにする必要があります。
|
||||
|
||||
# --hints--
|
||||
|
||||
すべてのバックスラッシュ (`\`) を削除する必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
!/\\/g.test(code) &&
|
||||
myStr.match(
|
||||
'\\s*<a href\\s*=\\s*"http://www.example.com"\\s*target\\s*=\\s*"_blank">\\s*Link\\s*</a>\\s*'
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
2 つのシングルクォート `'` と 4 つのダブルクォート `"` を使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/"/g).length === 4 && code.match(/'/g).length === 2);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function() { return "myStr = " + myStr; })();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const myStr = "<a href=\"http://www.example.com\" target=\"_blank\">Link</a>";
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myStr = '<a href="http://www.example.com" target="_blank">Link</a>';
|
||||
```
|
@ -0,0 +1,181 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244cf
|
||||
title: レコードコレクション
|
||||
challengeType: 1
|
||||
forumTopicId: 18261
|
||||
dashedName: record-collection
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
音楽アルバムのコレクションの一部を表すオブジェクトリテラルが用意されています。 各アルバムには、キーとして一意の ID 番号と、その他のいくつかのプロパティが含まれています。 情報が完全ではないアルバムもあります。
|
||||
|
||||
まず `updateRecords` 関数から記述を始めてください。この関数はオブジェクトリテラル `records` を受け取ります。これには、音楽アルバムのコレクション、`id`、`prop` (`artist` や `tracks` など)、および `value` が含まれています。 次に示すルールに従って関数を完成させ、関数に渡すオブジェクトを変更してください。
|
||||
|
||||
- 関数は常にレコードコレクションオブジェクト全体を返す必要があります。
|
||||
- `prop` が `tracks` ではなく、`value` が空文字列ではない場合は、アルバムの `prop` を `value` に更新または設定します。
|
||||
- `prop` が `tracks` であるものの、アルバムに `tracks` プロパティがない場合は、空の配列を作成してそこに `value` を追加します。
|
||||
- `prop` が `tracks` であり、`value` が空文字列ではない場合は、`value` をアルバムの既存の `tracks` 配列の末尾に追加します。
|
||||
- `value` が空文字列の場合は、指定された `prop` プロパティをアルバムから削除します。
|
||||
|
||||
**注:** テストには `recordCollection` オブジェクトのコピーが使用されます。
|
||||
|
||||
# --hints--
|
||||
|
||||
`updateRecords(recordCollection, 5439, "artist", "ABBA")` の実行後、`artist` は文字列 `ABBA` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
updateRecords(_recordCollection, 5439, 'artist', 'ABBA')[5439]['artist'] ===
|
||||
'ABBA'
|
||||
);
|
||||
```
|
||||
|
||||
`updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me")` の実行後、`tracks` の末尾の要素は文字列 `Take a Chance on Me` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
updateRecords(_recordCollection, 5439, 'tracks', 'Take a Chance on Me')[5439][
|
||||
'tracks'
|
||||
].pop() === 'Take a Chance on Me'
|
||||
);
|
||||
```
|
||||
|
||||
`updateRecords(recordCollection, 2548, "artist", "")` の実行後、`artist` は設定されない必要があります。
|
||||
|
||||
```js
|
||||
updateRecords(_recordCollection, 2548, 'artist', '');
|
||||
assert(!_recordCollection[2548].hasOwnProperty('artist'));
|
||||
```
|
||||
|
||||
`updateRecords(recordCollection, 1245, "tracks", "Addicted to Love")` の実行後、`tracks` の末尾の要素は文字列 `Addicted to Love` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
updateRecords(_recordCollection, 1245, 'tracks', 'Addicted to Love')[1245][
|
||||
'tracks'
|
||||
].pop() === 'Addicted to Love'
|
||||
);
|
||||
```
|
||||
|
||||
`updateRecords(recordCollection, 2468, "tracks", "Free")` の実行後、`tracks` の先頭の要素は文字列 `1999` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
updateRecords(_recordCollection, 2468, 'tracks', 'Free')[2468][
|
||||
'tracks'
|
||||
][0] === '1999'
|
||||
);
|
||||
```
|
||||
|
||||
`updateRecords(recordCollection, 2548, "tracks", "")` の実行後、`tracks` は設定されない必要があります。
|
||||
|
||||
```js
|
||||
updateRecords(_recordCollection, 2548, 'tracks', '');
|
||||
assert(!_recordCollection[2548].hasOwnProperty('tracks'));
|
||||
```
|
||||
|
||||
`updateRecords(recordCollection, 1245, "albumTitle", "Riptide")` の実行後、`albumTitle` は文字列 `Riptide` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
updateRecords(_recordCollection, 1245, 'albumTitle', 'Riptide')[1245][
|
||||
'albumTitle'
|
||||
] === 'Riptide'
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
const _recordCollection = {
|
||||
2548: {
|
||||
albumTitle: 'Slippery When Wet',
|
||||
artist: 'Bon Jovi',
|
||||
tracks: ['Let It Rock', 'You Give Love a Bad Name']
|
||||
},
|
||||
2468: {
|
||||
albumTitle: '1999',
|
||||
artist: 'Prince',
|
||||
tracks: ['1999', 'Little Red Corvette']
|
||||
},
|
||||
1245: {
|
||||
artist: 'Robert Palmer',
|
||||
tracks: []
|
||||
},
|
||||
5439: {
|
||||
albumTitle: 'ABBA Gold'
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const recordCollection = {
|
||||
2548: {
|
||||
albumTitle: 'Slippery When Wet',
|
||||
artist: 'Bon Jovi',
|
||||
tracks: ['Let It Rock', 'You Give Love a Bad Name']
|
||||
},
|
||||
2468: {
|
||||
albumTitle: '1999',
|
||||
artist: 'Prince',
|
||||
tracks: ['1999', 'Little Red Corvette']
|
||||
},
|
||||
1245: {
|
||||
artist: 'Robert Palmer',
|
||||
tracks: []
|
||||
},
|
||||
5439: {
|
||||
albumTitle: 'ABBA Gold'
|
||||
}
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
function updateRecords(records, id, prop, value) {
|
||||
return records;
|
||||
}
|
||||
|
||||
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const recordCollection = {
|
||||
2548: {
|
||||
albumTitle: 'Slippery When Wet',
|
||||
artist: 'Bon Jovi',
|
||||
tracks: ['Let It Rock', 'You Give Love a Bad Name']
|
||||
},
|
||||
2468: {
|
||||
albumTitle: '1999',
|
||||
artist: 'Prince',
|
||||
tracks: ['1999', 'Little Red Corvette']
|
||||
},
|
||||
1245: {
|
||||
artist: 'Robert Palmer',
|
||||
tracks: []
|
||||
},
|
||||
5439: {
|
||||
albumTitle: 'ABBA Gold'
|
||||
}
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
function updateRecords(records, id, prop, value) {
|
||||
if (value === '') delete records[id][prop];
|
||||
else if (prop === 'tracks') {
|
||||
records[id][prop] = records[id][prop] || [];
|
||||
records[id][prop].push(value);
|
||||
} else {
|
||||
records[id][prop] = value;
|
||||
}
|
||||
|
||||
return records;
|
||||
}
|
||||
```
|
@ -0,0 +1,105 @@
|
||||
---
|
||||
id: 5cfa3679138e7d9595b9d9d4
|
||||
title: 再帰を使用してループを置き換える
|
||||
challengeType: 1
|
||||
videoUrl: >-
|
||||
https://www.freecodecamp.org/news/how-recursion-works-explained-with-flowcharts-and-a-video-de61f40cb7f9/
|
||||
forumTopicId: 301175
|
||||
dashedName: replace-loops-using-recursion
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
関数が自分自身を呼び出せるという概念のことを再帰といいます。 再帰を理解するために、次の作業について考えてみましょう。配列の最初の `n` 個の要素を掛け合わせて、それらの要素の積を生成するとします。 これは次のように `for` ループを使用して記述できます。
|
||||
|
||||
```js
|
||||
function multiply(arr, n) {
|
||||
let product = 1;
|
||||
for (let i = 0; i < n; i++) {
|
||||
product *= arr[i];
|
||||
}
|
||||
return product;
|
||||
}
|
||||
```
|
||||
|
||||
しかし、ここで `multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]` であることに注目してください。 つまり、`multiply` を自分自身で書き換えることが可能で、ループを使用する必要はありません。
|
||||
|
||||
```js
|
||||
function multiply(arr, n) {
|
||||
if (n <= 0) {
|
||||
return 1;
|
||||
} else {
|
||||
return multiply(arr, n - 1) * arr[n - 1];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`multiply` の再帰バージョンを詳しく説明すると次のようになります。 <dfn>基準ケース</dfn>は `n <= 0` であり、1 を返します。 `n` の値がこれより大きい場合、関数は `n - 1` を引数に取って自分自身を呼び出します。 この関数呼び出しは同じ方法で評価され、`n <= 0` となるまで、`multiply` を呼び出し続けます。 n <= 0 になった時点で、すべての関数が処理を返し、最初の `multiply` が答えを返します。
|
||||
|
||||
**注:** 再帰関数では、関数の呼び出しをせずに制御を返す条件となる基準ケースを設定しておく必要があります (たとえば、この例では `n <= 0`)。これがないと再帰を無限に繰り返してしまいます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
再帰関数の `sum(arr, n)` を記述してください。この関数は配列 `arr` の最初の `n` 個の要素の合計を返します。
|
||||
|
||||
# --hints--
|
||||
|
||||
`sum([1], 0)` は 0 と等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(sum([1], 0), 0);
|
||||
```
|
||||
|
||||
`sum([2, 3, 4], 1)` は 2 と等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(sum([2, 3, 4], 1), 2);
|
||||
```
|
||||
|
||||
`sum([2, 3, 4, 5], 3)` は 9 と等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert.equal(sum([2, 3, 4, 5], 3), 9);
|
||||
```
|
||||
|
||||
このコードには、どのような種類のループ (`for`、`while`、または `forEach`、`map`、`filter`、`reduce` のような高階関数) も使用しないでください。
|
||||
|
||||
```js
|
||||
assert(
|
||||
!code.match(/for|while|forEach|map|filter|reduce/g)
|
||||
);
|
||||
```
|
||||
|
||||
この課題の解決には、再帰を使用してください。
|
||||
|
||||
```js
|
||||
assert(
|
||||
sum.toString().match(/sum\(.*\)/g).length > 1
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sum(arr, n) {
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sum(arr, n) {
|
||||
// Only change code below this line
|
||||
if(n <= 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return sum(arr, n - 1) + arr[n - 1];
|
||||
}
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
@ -0,0 +1,157 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244e0
|
||||
title: if else のチェーンを switch に置き換える
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c3JE8fy'
|
||||
forumTopicId: 18266
|
||||
dashedName: replacing-if-else-chains-with-switch
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
選択肢となるオプションが多数ある場合、`switch` ステートメントを使用すると、たくさんの `if`/`else if` ステートメントをつなげるよりも記述が簡単になります。 次の例をご覧ください。
|
||||
|
||||
```js
|
||||
if (val === 1) {
|
||||
answer = "a";
|
||||
} else if (val === 2) {
|
||||
answer = "b";
|
||||
} else {
|
||||
answer = "c";
|
||||
}
|
||||
```
|
||||
|
||||
これは次のように置き換えることができます。
|
||||
|
||||
```js
|
||||
switch(val) {
|
||||
case 1:
|
||||
answer = "a";
|
||||
break;
|
||||
case 2:
|
||||
answer = "b";
|
||||
break;
|
||||
default:
|
||||
answer = "c";
|
||||
}
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
`if`/`else if` ステートメントのチェーンを `switch` ステートメントに変更してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
プログラムにはどんな `else` ステートメントも使用しないでください。
|
||||
|
||||
```js
|
||||
assert(!/else/g.test(code));
|
||||
```
|
||||
|
||||
プログラムにはどんな `if` ステートメントも使用しないでください。
|
||||
|
||||
```js
|
||||
assert(!/if/g.test(code));
|
||||
```
|
||||
|
||||
少なくとも 4 つの `break` ステートメントを含める要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/break/g).length >= 4);
|
||||
```
|
||||
|
||||
`chainToSwitch("bob")` は文字列 `Marley` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(chainToSwitch('bob') === 'Marley');
|
||||
```
|
||||
|
||||
`chainToSwitch(42)` は文字列 `The Answer` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(chainToSwitch(42) === 'The Answer');
|
||||
```
|
||||
|
||||
`chainToSwitch(1)` は文字列 `There is no #1` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(chainToSwitch(1) === 'There is no #1');
|
||||
```
|
||||
|
||||
`chainToSwitch(99)` は文字列 `Missed me by this much!` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(chainToSwitch(99) === 'Missed me by this much!');
|
||||
```
|
||||
|
||||
`chainToSwitch(7)` は文字列 `Ate Nine` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(chainToSwitch(7) === 'Ate Nine');
|
||||
```
|
||||
|
||||
`chainToSwitch("John")` は `""` (空文字列) になる必要があります。
|
||||
|
||||
```js
|
||||
assert(chainToSwitch('John') === '');
|
||||
```
|
||||
|
||||
`chainToSwitch(156)` は `""` (空文字列) になる必要があります。
|
||||
|
||||
```js
|
||||
assert(chainToSwitch(156) === '');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function chainToSwitch(val) {
|
||||
let answer = "";
|
||||
// Only change code below this line
|
||||
|
||||
if (val === "bob") {
|
||||
answer = "Marley";
|
||||
} else if (val === 42) {
|
||||
answer = "The Answer";
|
||||
} else if (val === 1) {
|
||||
answer = "There is no #1";
|
||||
} else if (val === 99) {
|
||||
answer = "Missed me by this much!";
|
||||
} else if (val === 7) {
|
||||
answer = "Ate Nine";
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
return answer;
|
||||
}
|
||||
|
||||
chainToSwitch(7);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function chainToSwitch(val) {
|
||||
let answer = "";
|
||||
|
||||
switch(val) {
|
||||
case "bob":
|
||||
answer = "Marley";
|
||||
break;
|
||||
case 42:
|
||||
answer = "The Answer";
|
||||
break;
|
||||
case 1:
|
||||
answer = "There is no #1";
|
||||
break;
|
||||
case 99:
|
||||
answer = "Missed me by this much!";
|
||||
break;
|
||||
case 7:
|
||||
answer = "Ate Nine";
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
```
|
@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c2
|
||||
title: return で関数から値を返す
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cy87wue'
|
||||
forumTopicId: 18271
|
||||
dashedName: return-a-value-from-a-function-with-return
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
関数に値を渡すには<dfn>引数</dfn>を使用します。 関数から値を返すには `return` ステートメントを使用します。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
function plusThree(num) {
|
||||
return num + 3;
|
||||
}
|
||||
|
||||
const answer = plusThree(5);
|
||||
```
|
||||
|
||||
`answer` の値は `8` になります。
|
||||
|
||||
`plusThree` は、`num` を<dfn>引数</dfn>として受け取り、`num + 3` に等しい値を返します。
|
||||
|
||||
# --instructions--
|
||||
|
||||
引数を 1 つ受け取り、引数に `5` を掛けた新しい値を返す関数 `timesFive` を作成してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`timesFive` は関数である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof timesFive === 'function');
|
||||
```
|
||||
|
||||
`timesFive(5)` は `25` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(timesFive(5) === 25);
|
||||
```
|
||||
|
||||
`timesFive(2)` は `10` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(timesFive(2) === 10);
|
||||
```
|
||||
|
||||
`timesFive(0)` は `0` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(timesFive(0) === 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function timesFive(num) {
|
||||
return num * 5;
|
||||
}
|
||||
timesFive(10);
|
||||
```
|
@ -0,0 +1,106 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c4
|
||||
title: 関数を早めに return させる
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cQe39Sq'
|
||||
forumTopicId: 18272
|
||||
dashedName: return-early-pattern-for-functions
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`return` ステートメントに達すると、現在の関数の実行が停止し、元の呼び出し場所に制御が戻ります。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
function myFun() {
|
||||
console.log("Hello");
|
||||
return "World";
|
||||
console.log("byebye")
|
||||
}
|
||||
myFun();
|
||||
```
|
||||
|
||||
上記は文字列 `Hello` をコンソールに表示し、文字列 `World` を返します。 `return` ステートメントで関数が終了するため、文字列 `byebye` がコンソールに表示されることはありません。
|
||||
|
||||
# --instructions--
|
||||
|
||||
関数 `abTest` を変更して、`a` または `b` が `0` より小さい場合に関数が `undefined` の値を返して直ちに終了するようにしてください。
|
||||
|
||||
**ヒント**
|
||||
[`undefined` はキーワード](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/understanding-uninitialized-variables)であって、文字列ではないことに注意してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`abTest(2, 2)` は数値を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof abTest(2, 2) === 'number');
|
||||
```
|
||||
|
||||
`abTest(2, 2)` は `8` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(abTest(2, 2) === 8);
|
||||
```
|
||||
|
||||
`abTest(-2, 2)` は `undefined` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(abTest(-2, 2) === undefined);
|
||||
```
|
||||
|
||||
`abTest(2, -2)` は `undefined` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(abTest(2, -2) === undefined);
|
||||
```
|
||||
|
||||
`abTest(2, 8)` は `18` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(abTest(2, 8) === 18);
|
||||
```
|
||||
|
||||
`abTest(3, 3)` は `12` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(abTest(3, 3) === 12);
|
||||
```
|
||||
|
||||
`abTest(0, 0)` は `0` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(abTest(0, 0) === 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function abTest(a, b) {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
|
||||
}
|
||||
|
||||
abTest(2,2);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function abTest(a, b) {
|
||||
if(a < 0 || b < 0) {
|
||||
return undefined;
|
||||
}
|
||||
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
|
||||
}
|
||||
```
|
@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 5679ceb97cbaa8c51670a16b
|
||||
title: 関数からブール値を返す
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cp62qAQ'
|
||||
forumTopicId: 18273
|
||||
dashedName: returning-boolean-values-from-functions
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
[等価演算子による比較](/learn/javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-equality-operator)で説明しましたが、比較演算子はすべて、ブール値の `true` または `false` を返します。
|
||||
|
||||
次のように `if/else` ステートメントを使用して比較を行うこともできます。
|
||||
|
||||
```js
|
||||
function isEqual(a, b) {
|
||||
if (a === b) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
しかし、もっと良い方法があります。 `===` は `true` または `false` を返すので、比較の結果をそのまま返すことができます。
|
||||
|
||||
```js
|
||||
function isEqual(a, b) {
|
||||
return a === b;
|
||||
}
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
関数 `isLess` を修正して、`if/else` ステートメントを削除してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`isLess(10, 15)` は `true` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(isLess(10, 15) === true);
|
||||
```
|
||||
|
||||
`isLess(15, 10)` は `false` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(isLess(15, 10) === false);
|
||||
```
|
||||
|
||||
`if` ステートメントまたは `else` ステートメントを使用しないでください。
|
||||
|
||||
```js
|
||||
assert(!/if|else/g.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function isLess(a, b) {
|
||||
// Only change code below this line
|
||||
if (a < b) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
isLess(10, 15);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function isLess(a, b) {
|
||||
return a < b;
|
||||
}
|
||||
```
|
@ -0,0 +1,114 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244dd
|
||||
title: switch ステートメントを使用して多数の選択肢から選択する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c4mv4fm'
|
||||
forumTopicId: 18277
|
||||
dashedName: selecting-from-many-options-with-switch-statements
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
選択肢が多数ある場合は <dfn>switch</dfn> ステートメントを使用してください。 `switch` ステートメントは値を調べますが、多数の <dfn>case</dfn> ステートメントを記述してさまざまな値の候補を定義することができます。 条件が一致した最初の `case` 値から `break` に達するまで、ステートメントが実行されます。
|
||||
|
||||
次は `switch` ステートメントの例です。
|
||||
|
||||
```js
|
||||
switch(lowercaseLetter) {
|
||||
case "a":
|
||||
console.log("A");
|
||||
break;
|
||||
case "b":
|
||||
console.log("B");
|
||||
break;
|
||||
}
|
||||
```
|
||||
|
||||
`case` の値は厳密等価 (`===`) によりテストされます。 `break` は、ステートメントの実行を停止するよう JavaScript に指示します。 `break` が省略されている場合は次のステートメントが実行されます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`val` をテストし、次の条件で `answer` を設定する switch ステートメントを記述してください。
|
||||
`1` - `alpha`
|
||||
`2` - `beta`
|
||||
`3` - `gamma`
|
||||
`4` - `delta`
|
||||
|
||||
# --hints--
|
||||
|
||||
`caseInSwitch(1)` の値は文字列 `alpha` となる必要があります。
|
||||
|
||||
```js
|
||||
assert(caseInSwitch(1) === 'alpha');
|
||||
```
|
||||
|
||||
`caseInSwitch(2)` の値は文字列 `beta` となる必要があります。
|
||||
|
||||
```js
|
||||
assert(caseInSwitch(2) === 'beta');
|
||||
```
|
||||
|
||||
`caseInSwitch(3)` の値は文字列 `gamma` となる必要があります。
|
||||
|
||||
```js
|
||||
assert(caseInSwitch(3) === 'gamma');
|
||||
```
|
||||
|
||||
`caseInSwitch(4)` の値は文字列 `delta` となる必要があります。
|
||||
|
||||
```js
|
||||
assert(caseInSwitch(4) === 'delta');
|
||||
```
|
||||
|
||||
`if` ステートメントまたは `else` ステートメントを使用しないでください。
|
||||
|
||||
```js
|
||||
assert(!/else/g.test(code) || !/if/g.test(code));
|
||||
```
|
||||
|
||||
少なくとも 3 つの `break` ステートメントを記述する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/break/g).length > 2);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function caseInSwitch(val) {
|
||||
let answer = "";
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
return answer;
|
||||
}
|
||||
|
||||
caseInSwitch(1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function caseInSwitch(val) {
|
||||
let answer = "";
|
||||
|
||||
switch(val) {
|
||||
case 1:
|
||||
answer = "alpha";
|
||||
break;
|
||||
case 2:
|
||||
answer = "beta";
|
||||
break;
|
||||
case 3:
|
||||
answer = "gamma";
|
||||
break;
|
||||
case 4:
|
||||
answer = "delta";
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
```
|
@ -0,0 +1,97 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244bc
|
||||
title: ショッピングリスト
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c9MEKHZ'
|
||||
forumTopicId: 18280
|
||||
dashedName: shopping-list
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
変数 `myList` にショッピングリストを作成してください。 リストは、複数のサブ配列を含む多次元配列とします。
|
||||
|
||||
各サブ配列の最初の要素には、商品の名前を持つ文字列を含める必要があります。 2 番目の要素は、数量を表す数値としてください。
|
||||
|
||||
```js
|
||||
["Chocolate Bar", 15]
|
||||
```
|
||||
|
||||
リストには、少なくとも 5 つのサブ配列が必要です。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myList` は配列である必要があります。
|
||||
|
||||
```js
|
||||
assert(isArray);
|
||||
```
|
||||
|
||||
各サブ配列の最初の要素は、すべて文字列である必要があります。
|
||||
|
||||
```js
|
||||
assert(hasString);
|
||||
```
|
||||
|
||||
各サブ配列の 2 番目の要素は、すべて数値である必要があります。
|
||||
|
||||
```js
|
||||
assert(hasNumber);
|
||||
```
|
||||
|
||||
リストには少なくとも 5 つのアイテムが必要です。
|
||||
|
||||
```js
|
||||
assert(count > 4);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
var count = 0;
|
||||
var isArray = false;
|
||||
var hasString = false;
|
||||
var hasNumber = false;
|
||||
(function(list){
|
||||
if(Array.isArray(myList)) {
|
||||
isArray = true;
|
||||
if(myList.length > 0) {
|
||||
hasString = true;
|
||||
hasNumber = true;
|
||||
for (var elem of myList) {
|
||||
if(!elem || !elem[0] || typeof elem[0] !== 'string') {
|
||||
hasString = false;
|
||||
}
|
||||
if(!elem || typeof elem[1] !== 'number') {
|
||||
hasNumber = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
count = myList.length;
|
||||
return JSON.stringify(myList);
|
||||
} else {
|
||||
return "myList is not an array";
|
||||
}
|
||||
|
||||
})(myList);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const myList = [];
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myList = [
|
||||
["Candy", 10],
|
||||
["Potatoes", 12],
|
||||
["Eggs", 12],
|
||||
["Catfood", 1],
|
||||
["Toads", 9]
|
||||
];
|
||||
```
|
@ -0,0 +1,116 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c6
|
||||
title: 1 列に並べる
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ca8Q8tP'
|
||||
forumTopicId: 18307
|
||||
dashedName: stand-in-line
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
コンピューターサイエンスでは、アイテムを順に保存する抽象的な<dfn>データ構造</dfn>として<dfn>キュー</dfn>を使用します。 新しいアイテムはキューの末尾に追加され、古いアイテムはキューの先頭から削除されます。
|
||||
|
||||
配列 (`arr`) と数値 (`item`) を引数として取る関数 `nextInLine` を記述してください。
|
||||
|
||||
配列の末尾に数値を追加し、それから配列の先頭の要素を削除してください。
|
||||
|
||||
その後、`nextInLine` 関数は削除された要素を返す必要があります。
|
||||
|
||||
# --hints--
|
||||
|
||||
`nextInLine([], 5)` は数値を返す必要があります。
|
||||
|
||||
```js
|
||||
assert.isNumber(nextInLine([], 5));
|
||||
```
|
||||
|
||||
`nextInLine([], 1)` は `1` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(nextInLine([], 1) === 1);
|
||||
```
|
||||
|
||||
`nextInLine([2], 1)` は `2` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(nextInLine([2], 1) === 2);
|
||||
```
|
||||
|
||||
`nextInLine([5,6,7,8,9], 1)` は `5` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(nextInLine([5, 6, 7, 8, 9], 1) === 5);
|
||||
```
|
||||
|
||||
`nextInLine(testArr, 10)` の実行後、`testArr[4]` は `10` となる必要があります。
|
||||
|
||||
```js
|
||||
nextInLine(testArr, 10);
|
||||
assert(testArr[4] === 10);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
var logOutput = [];
|
||||
var originalConsole = console
|
||||
function capture() {
|
||||
var nativeLog = console.log;
|
||||
console.log = function (message) {
|
||||
logOutput.push(message);
|
||||
if(nativeLog.apply) {
|
||||
nativeLog.apply(originalConsole, arguments);
|
||||
} else {
|
||||
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
|
||||
nativeLog(nativeMsg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function uncapture() {
|
||||
console.log = originalConsole.log;
|
||||
}
|
||||
|
||||
capture();
|
||||
```
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
uncapture();
|
||||
testArr = [1,2,3,4,5];
|
||||
(function() { return logOutput.join("\n");})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function nextInLine(arr, item) {
|
||||
// Only change code below this line
|
||||
|
||||
return item;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
// Setup
|
||||
const testArr = [1, 2, 3, 4, 5];
|
||||
|
||||
// Display code
|
||||
console.log("Before: " + JSON.stringify(testArr));
|
||||
console.log(nextInLine(testArr, 6));
|
||||
console.log("After: " + JSON.stringify(testArr));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const testArr = [1, 2, 3, 4, 5];
|
||||
|
||||
function nextInLine(arr, item) {
|
||||
arr.push(item);
|
||||
return arr.shift();
|
||||
}
|
||||
```
|
@ -0,0 +1,63 @@
|
||||
---
|
||||
id: bd7993c9c69feddfaeb8bdef
|
||||
title: JavaScript の配列を使用して 1 つの変数に複数の値を格納する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/crZQWAm'
|
||||
forumTopicId: 18309
|
||||
dashedName: store-multiple-values-in-one-variable-using-javascript-arrays
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript の `array` 変数を使用すると、複数のデータを一か所に格納できます。
|
||||
|
||||
配列を宣言するには、開始の角括弧を記述して宣言を開始し、各項目の間にコンマを入れ、最後に終了の角括弧を記述します。
|
||||
|
||||
```js
|
||||
const sandwich = ["peanut butter", "jelly", "bread"];
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
文字列と数値の両方を (この順序で) 含むように、新しい配列 `myArray` を変更してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myArray` は配列である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof myArray == 'object');
|
||||
```
|
||||
|
||||
`myArray` の最初の項目は文字列である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof myArray[0] !== 'undefined' && typeof myArray[0] == 'string');
|
||||
```
|
||||
|
||||
`myArray` の 2 番目の項目は数値である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof myArray[1] !== 'undefined' && typeof myArray[1] == 'number');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myArray);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Only change code below this line
|
||||
const myArray = [];
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myArray = ["The Answer", 42];
|
||||
```
|
@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244a8
|
||||
title: 代入演算子を使用して値を格納する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cEanysE'
|
||||
forumTopicId: 18310
|
||||
dashedName: storing-values-with-the-assignment-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript では、<dfn>代入</dfn>演算子 (`=`) を使用して値を変数に格納できます。
|
||||
|
||||
```js
|
||||
myVariable = 5;
|
||||
```
|
||||
|
||||
この例は `Number` 値 `5` を `myVariable` に代入します。
|
||||
|
||||
`=` 演算子の右側に何らかの計算式がある場合は、それらの計算が実行されてから、左側の変数に値が代入されます。
|
||||
|
||||
```js
|
||||
var myVar;
|
||||
myVar = 5;
|
||||
```
|
||||
|
||||
最初に、このコードは `myVar` という名前の変数を作成します。 次に、`5` を `myVar` に代入します。 これ以降、`myVar` がコードに再び出現した場合、プログラムはそれを `5` であるかのように扱います。
|
||||
|
||||
# --instructions--
|
||||
|
||||
値 `7` を変数 `a` に代入してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
指定のコメントより上にあるコードを変更しないでください。
|
||||
|
||||
```js
|
||||
assert(/var a;/.test(code));
|
||||
```
|
||||
|
||||
`a` の値は 7 である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof a === 'number' && a === 7);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
if (typeof a != 'undefined') {
|
||||
a = undefined;
|
||||
}
|
||||
```
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a){return "a = " + a;})(a);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var a;
|
||||
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a;
|
||||
a = 7;
|
||||
```
|
@ -0,0 +1,59 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb4bdef
|
||||
title: JavaScript で、ある数値から別の数値を引き算する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cP3yQtk'
|
||||
forumTopicId: 18314
|
||||
dashedName: subtract-one-number-from-another-with-javascript
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
ある数値から別の数値を引き算することもできます。
|
||||
|
||||
JavaScript では減算記号として `-` を使用します。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
const myVar = 12 - 6;
|
||||
```
|
||||
|
||||
`myVar` の値は `6` になります。
|
||||
# --instructions--
|
||||
|
||||
差が `12` になるように `0` を変更してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
変数 `difference` は 12 に等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert(difference === 12);
|
||||
```
|
||||
|
||||
45 から 1 つの数値だけを引いてください。
|
||||
|
||||
```js
|
||||
assert(/difference=45-33;?/.test(__helpers.removeWhiteSpace(code)));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'difference = '+z;})(difference);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const difference = 45 - 0;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const difference = 45 - 33;
|
||||
```
|
@ -0,0 +1,98 @@
|
||||
---
|
||||
id: 567af2437cbaa8c51670a16c
|
||||
title: オブジェクトにプロパティがあるかどうかをテストする
|
||||
challengeType: 1
|
||||
forumTopicId: 18324
|
||||
dashedName: testing-objects-for-properties
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
指定されたオブジェクトにプロパティが存在するかどうかをチェックできると便利なことがあります。 オブジェクトの `.hasOwnProperty(propname)` メソッドを使用すると、そのオブジェクトが指定されたプロパティ名を持っているかどうかを確認することができます。 `.hasOwnProperty()` はプロパティの有無に応じて、`true` または `false` を返します。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
const myObj = {
|
||||
top: "hat",
|
||||
bottom: "pants"
|
||||
};
|
||||
|
||||
myObj.hasOwnProperty("top");
|
||||
myObj.hasOwnProperty("middle");
|
||||
```
|
||||
|
||||
最初の `hasOwnProperty` は `true` を返し、2 つ目は `false` を返します。
|
||||
|
||||
# --instructions--
|
||||
|
||||
関数に渡されたオブジェクト (`obj`) に特定のプロパティ (`checkProp`) が含まれているかどうかをテストするように、関数 `checkObj` を変更してください。 プロパティが見つかった場合は、そのプロパティの値を返してください。 見つからなかった場合は、`"Not Found"` を返してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")` は文字列 `pony` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'gift') === 'pony'
|
||||
);
|
||||
```
|
||||
|
||||
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")` は文字列 `kitten` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'pet') === 'kitten'
|
||||
);
|
||||
```
|
||||
|
||||
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")` は文字列 `Not Found` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'house') ===
|
||||
'Not Found'
|
||||
);
|
||||
```
|
||||
|
||||
`checkObj({city: "Seattle"}, "city")` は文字列 `Seattle` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(checkObj({ city: 'Seattle' }, 'city') === 'Seattle');
|
||||
```
|
||||
|
||||
`checkObj({city: "Seattle"}, "district")` は文字列 `Not Found` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(checkObj({ city: 'Seattle' }, 'district') === 'Not Found');
|
||||
```
|
||||
|
||||
`checkObj({pet: "kitten", bed: "sleigh"}, "gift")` は文字列 `Not Found` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(checkObj({ pet: 'kitten', bed: 'sleigh' }, 'gift') === 'Not Found');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function checkObj(obj, checkProp) {
|
||||
// Only change code below this line
|
||||
return "Change Me!";
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function checkObj(obj, checkProp) {
|
||||
if(obj.hasOwnProperty(checkProp)) {
|
||||
return obj[checkProp];
|
||||
} else {
|
||||
return "Not Found";
|
||||
}
|
||||
}
|
||||
```
|
@ -0,0 +1,70 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ba
|
||||
title: 文字列の不変性を理解する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cWPVaUR'
|
||||
forumTopicId: 18331
|
||||
dashedName: understand-string-immutability
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript では、`String` の値は<dfn>イミュータブル</dfn>です。つまり、作成後に変更することはできません。
|
||||
|
||||
たとえば次のコードをご覧ください。
|
||||
|
||||
```js
|
||||
let myStr = "Bob";
|
||||
myStr[0] = "J";
|
||||
```
|
||||
|
||||
このコードでは、`myStr` の値を `Job` に変えることはできません。`myStr` の内容を変更することができないからです。 これは `myStr` を変更できないという*意味ではない*ことに注意してください。<dfn>文字列リテラル</dfn>の個々の文字を変更することはできない、というだけです。 `myStr` を変更する唯一の方法は、次のように新しい文字列を代入することです。
|
||||
|
||||
```js
|
||||
let myStr = "Bob";
|
||||
myStr = "Job";
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
上の例で説明したアプローチに従って、文字列 `Hello World` を値として含むよう、`myStr` への代入を修正してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myStr` の値は文字列 `Hello World` になる必要があります。
|
||||
|
||||
```js
|
||||
assert(myStr === 'Hello World');
|
||||
```
|
||||
|
||||
指定のコメントより上にあるコードを変更しないでください。
|
||||
|
||||
```js
|
||||
assert(/myStr = "Jello World"/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(v){return "myStr = " + v;})(myStr);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
let myStr = "Jello World";
|
||||
|
||||
// Only change code below this line
|
||||
myStr[0] = "H"; // Change this line
|
||||
// Only change code above this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let myStr = "Jello World";
|
||||
myStr = "Hello World";
|
||||
```
|
@ -0,0 +1,60 @@
|
||||
---
|
||||
id: bd7123c9c441eddfaeb5bdef
|
||||
title: ブール値を理解する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c9Me8t4'
|
||||
forumTopicId: 301176
|
||||
dashedName: understanding-boolean-values
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
別のデータ型として<dfn>ブール値 (Boolean)</dfn> 型があります。 ブール値型は `true` または `false` のいずれか 1 つの値しか取りません。 基本的にはちょっとしたオン/オフスイッチとみなすことができ、`true` をオンに、`false` をオフに対応させることができます。 これら 2 つの状態は相互排他的です。
|
||||
|
||||
**注:** ブール値は引用符を付けて記述されることはありません。 文字列の `"true"` と `"false"` は ブール値ではなく、JavaScript では特別な意味を持ちません。
|
||||
|
||||
# --instructions--
|
||||
|
||||
実行ボタンがクリックされたときに `false` ではなく `true` を返すように、`welcomeToBooleans` 関数を変更してください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`welcomeToBooleans()` 関数はブール値 (`true` または `false`) を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof welcomeToBooleans() === 'boolean');
|
||||
```
|
||||
|
||||
`welcomeToBooleans()` は `true` を返す必要があります。
|
||||
|
||||
```js
|
||||
assert(welcomeToBooleans() === true);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
welcomeToBooleans();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function welcomeToBooleans() {
|
||||
// Only change code below this line
|
||||
|
||||
return false; // Change this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function welcomeToBooleans() {
|
||||
return true; // Change this line
|
||||
}
|
||||
```
|
@ -0,0 +1,100 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ab
|
||||
title: 変数の大文字と小文字の区別を理解する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cd6GDcD'
|
||||
forumTopicId: 18334
|
||||
dashedName: understanding-case-sensitivity-in-variables
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript では、すべての変数名および関数名は大文字と小文字を区別します。 これは大文字の使い方が重要であることを意味します。
|
||||
|
||||
`MYVAR` は `MyVar` や `myvar` と同じではありません。 同じ名前で大文字・小文字が異なる、複数の別々の変数を持つことができます。 明確さを維持するためには、この言語機能を*使用しない*ことを強く推奨します。
|
||||
|
||||
**ベストプラクティス**
|
||||
|
||||
JavaScript では <dfn>camelCase (キャメルケース)</dfn> で変数名を書きましょう。 <dfn>キャメルケース</dfn>は、複数の単語からなる変数名であり、最初の単語の最初の文字は小文字で、後続の単語の最初の文字は大文字にします。
|
||||
|
||||
**例:**
|
||||
|
||||
```js
|
||||
var someVariable;
|
||||
var anotherVariableName;
|
||||
var thisVariableNameIsSoLong;
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
変数名として<dfn>キャメルケース</dfn>を使用するよう、既存の宣言と割り当てを変更してください。
|
||||
|
||||
新しい変数を作成しないでください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`studlyCapVar` が定義され、値 `10` を持つ必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof studlyCapVar !== 'undefined' && studlyCapVar === 10);
|
||||
```
|
||||
|
||||
`properCamelCase` が定義され、文字列 `A String` を持つ必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
typeof properCamelCase !== 'undefined' && properCamelCase === 'A String'
|
||||
);
|
||||
```
|
||||
|
||||
`titleCaseOver` が定義され、値 `9000` を持つ必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof titleCaseOver !== 'undefined' && titleCaseOver === 9000);
|
||||
```
|
||||
|
||||
`studlyCapVar` は宣言部分と割り当て部分の両方でキャメルケースを使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/studlyCapVar/g).length === 2);
|
||||
```
|
||||
|
||||
`properCamelCase` は宣言部分と割り当て部分の両方でキャメルケースを使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/properCamelCase/g).length === 2);
|
||||
```
|
||||
|
||||
`titleCaseOver` は宣言部分と割り当て部分の両方でキャメルケースを使用する必要があります。
|
||||
|
||||
```js
|
||||
assert(code.match(/titleCaseOver/g).length === 2);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Variable declarations
|
||||
var StUdLyCapVaR;
|
||||
var properCamelCase;
|
||||
var TitleCaseOver;
|
||||
|
||||
// Variable assignments
|
||||
STUDLYCAPVAR = 10;
|
||||
PRoperCAmelCAse = "A String";
|
||||
tITLEcASEoVER = 9000;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var studlyCapVar;
|
||||
var properCamelCase;
|
||||
var titleCaseOver;
|
||||
|
||||
studlyCapVar = 10;
|
||||
properCamelCase = "A String";
|
||||
titleCaseOver = 9000;
|
||||
```
|
@ -0,0 +1,96 @@
|
||||
---
|
||||
id: 598e8944f009e646fc236146
|
||||
title: 関数から返される undefined 値を理解する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ce2p7cL'
|
||||
forumTopicId: 301177
|
||||
dashedName: understanding-undefined-value-returned-from-a-function
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
関数には `return` ステートメントを含めることができますが、必須ではありません。 関数に `return` ステートメントがない場合、関数は呼び出されたときに内部のコードを処理しますが、戻り値は `undefined` になります。
|
||||
|
||||
**例**
|
||||
|
||||
```js
|
||||
let sum = 0;
|
||||
|
||||
function addSum(num) {
|
||||
sum = sum + num;
|
||||
}
|
||||
|
||||
addSum(3);
|
||||
```
|
||||
|
||||
`addSum` は、`return` ステートメントを持たない関数です。 関数はグローバル変数の `sum` を変更しますが、関数の戻り値は `undefined` です。
|
||||
|
||||
# --instructions--
|
||||
|
||||
引数を持たない関数 `addFive` を作成してください。 この関数は `sum` 変数に 5 を足しますが、戻り値は `undefined` です。
|
||||
|
||||
# --hints--
|
||||
|
||||
`addFive` は関数である必要があります。
|
||||
|
||||
```js
|
||||
assert(typeof addFive === 'function');
|
||||
```
|
||||
|
||||
両方の関数の実行後に、`sum` は `8` と等しくなる必要があります。
|
||||
|
||||
```js
|
||||
assert(sum === 8);
|
||||
```
|
||||
|
||||
`addFive` の戻り値は `undefined` となる必要があります。
|
||||
|
||||
```js
|
||||
assert(addFive() === undefined);
|
||||
```
|
||||
|
||||
`addFive` 関数の内部で、`sum` 変数に `5` を足す必要があります。
|
||||
|
||||
```js
|
||||
assert(
|
||||
__helpers.removeWhiteSpace(addFive.toString()).match(/sum=sum\+5|sum\+=5/)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
let sum = 0;
|
||||
|
||||
function addThree() {
|
||||
sum = sum + 3;
|
||||
}
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
addThree();
|
||||
addFive();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let sum = 0;
|
||||
|
||||
function addThree() {
|
||||
sum = sum + 3;
|
||||
}
|
||||
|
||||
function addFive() {
|
||||
sum = sum + 5;
|
||||
}
|
||||
|
||||
addThree();
|
||||
addFive();
|
||||
```
|
@ -0,0 +1,79 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244aa
|
||||
title: 初期化されていない変数について理解する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cBa2JAL'
|
||||
forumTopicId: 18335
|
||||
dashedName: understanding-uninitialized-variables
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript で変数が宣言されるとき、その初期値は `undefined` です。 `undefined` の変数に対して数値演算を行うと、結果は `NaN` になります。これは、<dfn>"Not a Number" (数字ではない)</dfn> という意味です。 `undefined` の変数と文字列を連結すると、文字通り `undefined` という<dfn>文字列</dfn>が得られます。
|
||||
|
||||
# --instructions--
|
||||
|
||||
3 つの変数 `a`、`b`、`c` をそれぞれ `5`、`10`、`"I am a"` で初期化し、これらが `undefined` にならないようにしてください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`a` を定義し、計算の結果 `6` の値を持つようにしてください。
|
||||
|
||||
```js
|
||||
assert(typeof a === 'number' && a === 6);
|
||||
```
|
||||
|
||||
`b` を定義し、計算の結果 `15` の値を持つようにしてください。
|
||||
|
||||
```js
|
||||
assert(typeof b === 'number' && b === 15);
|
||||
```
|
||||
|
||||
`c` は `undefined` を含まず、文字列の値 `I am a String!` を持つようにしてください。
|
||||
|
||||
```js
|
||||
assert(!/undefined/.test(c) && c === 'I am a String!');
|
||||
```
|
||||
|
||||
指定のコメントより下にあるコードを変更しないでください。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/a = a \+ 1;/.test(code) &&
|
||||
/b = b \+ 5;/.test(code) &&
|
||||
/c = c \+ " String!";/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = '" + c + "'"; })(a,b,c);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Only change code below this line
|
||||
var a;
|
||||
var b;
|
||||
var c;
|
||||
// Only change code above this line
|
||||
|
||||
a = a + 1;
|
||||
b = b + 5;
|
||||
c = c + " String!";
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a = 5;
|
||||
var b = 10;
|
||||
var c = "I am a";
|
||||
a = a + 1;
|
||||
b = b + 5;
|
||||
c = c + " String!";
|
||||
```
|
@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d1
|
||||
title: オブジェクトのプロパティを更新する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c9yEJT4'
|
||||
forumTopicId: 18336
|
||||
dashedName: updating-object-properties
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript オブジェクトを作成した後、他の変数と同じようにいつでもそのプロパティを更新することができます。 更新には、ドット記法またはブラケット記法のいずれも使用できます。
|
||||
|
||||
例として、`ourDog` を見てみましょう。
|
||||
|
||||
```js
|
||||
const ourDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"]
|
||||
};
|
||||
```
|
||||
|
||||
この犬はとても楽しい犬なので、名前を `Happy Camper` に変更してみましょう。 オブジェクトの name プロパティを更新するには、`ourDog.name = "Happy Camper";` と記述するか、`ourDog["name"] = "Happy Camper";` と記述します。これで `ourDog.name` の値を確認すると、`Camper` ではなく新しい名前の `Happy Camper` になっています。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`myDog` オブジェクトの name プロパティを更新してください。 名前を `Coder` から `Happy Coder` に変更しましょう。 ドット記法またはブラケット記法のいずれも使用できます。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myDog` の `name` プロパティが文字列 `Happy Coder` と等しくなるようにします。
|
||||
|
||||
```js
|
||||
assert(/happy coder/gi.test(myDog.name));
|
||||
```
|
||||
|
||||
`myDog` の定義を編集しないでください。
|
||||
|
||||
```js
|
||||
assert(/"name": "Coder"/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
const myDog = {
|
||||
"name": "Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"]
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const myDog = {
|
||||
"name": "Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"]
|
||||
};
|
||||
myDog.name = "Happy Coder";
|
||||
```
|
@ -0,0 +1,74 @@
|
||||
---
|
||||
id: bd7123c9c549eddfaeb5bdef
|
||||
title: ブラケット記法を使用して文字列の最初の文字を取得する
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ca8JwhW'
|
||||
forumTopicId: 18341
|
||||
dashedName: use-bracket-notation-to-find-the-first-character-in-a-string
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
<dfn>ブラケット記法</dfn>を使用すると、文字列内の特定のインデックスにある文字を取得できます。
|
||||
|
||||
JavaScript をはじめとする現代のほとんどのプログラミング言語では、人間のように 1 から数え始めることをせず、 0 から数え始めます。 これを<dfn>ゼロベースの</dfn>インデックスといいます。
|
||||
|
||||
たとえば、`Charles` という単語のインデックス 0 の文字は `C` です。 したがって、`const firstName = "Charles"` とした場合は、`firstName[0]` とすることでこの文字列の 1 文字目の値を取得できます。
|
||||
|
||||
例:
|
||||
|
||||
```js
|
||||
const firstName = "Charles";
|
||||
const firstLetter = firstName[0];
|
||||
```
|
||||
|
||||
`firstLetter` の値は文字列 `C` となります。
|
||||
|
||||
# --instructions--
|
||||
|
||||
ブラケット記法を使用して、`lastName` 変数の 1 文字目を取得し、それを `firstLetterOfLastName` に代入してください。
|
||||
|
||||
**ヒント:** 解答できない場合は上記の例を参考にしてください。
|
||||
|
||||
# --hints--
|
||||
|
||||
`firstLetterOfLastName` 変数の値は `L` となる必要があります。
|
||||
|
||||
```js
|
||||
assert(firstLetterOfLastName === 'L');
|
||||
```
|
||||
|
||||
ブラケット記法を使用してください。
|
||||
|
||||
```js
|
||||
assert(code.match(/firstLetterOfLastName\s*?=\s*?lastName\[.*?\]/));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(v){return v;})(firstLetterOfLastName);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
let firstLetterOfLastName = "";
|
||||
const lastName = "Lovelace";
|
||||
|
||||
// Only change code below this line
|
||||
firstLetterOfLastName = lastName; // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let firstLetterOfLastName = "";
|
||||
const lastName = "Lovelace";
|
||||
|
||||
// Only change code below this line
|
||||
firstLetterOfLastName = lastName[0];
|
||||
```
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user