chore(i18n,curriculum): update translations

This commit is contained in:
camperbot
2021-10-27 15:10:57 +00:00
committed by Mrugesh Mohapatra
parent e139fbcf13
commit c1fb339bbc
539 changed files with 3319 additions and 3352 deletions

View File

@ -18,9 +18,9 @@ dashedName: access-array-data-with-indexes
**示例** **示例**
```js ```js
var array = [50,60,70]; const array = [50, 60, 70];
array[0]; array[0];
var data = array[1]; const data = array[1];
``` ```
現在 `array[0]` 的值是 `50` `data` 的值爲 `60`. 現在 `array[0]` 的值是 `50` `data` 的值爲 `60`.
@ -76,7 +76,7 @@ if(typeof myArray !== "undefined" && typeof myData !== "undefined"){(function(y,
## --seed-contents-- ## --seed-contents--
```js ```js
var myArray = [50,60,70]; const myArray = [50, 60, 70];
``` ```
@ -84,6 +84,6 @@ var myArray = [50,60,70];
# --solutions-- # --solutions--
```js ```js
var myArray = [50,60,70]; const myArray = [50, 60, 70];
var myData = myArray[0]; const myData = myArray[0];
``` ```

View File

@ -14,12 +14,13 @@ dashedName: access-multi-dimensional-arrays-with-indexes
**例如:** **例如:**
```js ```js
var arr = [ const arr = [
[1,2,3], [1, 2, 3],
[4,5,6], [4, 5, 6],
[7,8,9], [7, 8, 9],
[[10,11,12], 13, 14] [[10, 11, 12], 13, 14]
]; ];
arr[3]; arr[3];
arr[3][0]; arr[3][0];
arr[3][0][1]; arr[3][0][1];
@ -58,14 +59,19 @@ if(typeof myArray !== "undefined"){(function(){return "myData: " + myData + " my
## --seed-contents-- ## --seed-contents--
```js ```js
var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]]; const myArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[[10, 11, 12], 13, 14],
];
var myData = myArray[0][0]; const myData = myArray[0][0];
``` ```
# --solutions-- # --solutions--
```js ```js
var myArray = [[1,2,3],[4,5,6], [7,8,9], [[10,11,12], 13, 14]]; const myArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [[10, 11, 12], 13, 14]];
var myData = myArray[2][1]; const myData = myArray[2][1];
``` ```

View File

@ -14,7 +14,7 @@ dashedName: accessing-nested-arrays
下面是訪問嵌套數組的例子: 下面是訪問嵌套數組的例子:
```js ```js
var ourPets = [ const ourPets = [
{ {
animalType: "cat", animalType: "cat",
names: [ names: [
@ -32,6 +32,7 @@ var ourPets = [
] ]
} }
]; ];
ourPets[0].names[1]; ourPets[0].names[1];
ourPets[1].names[0]; ourPets[1].names[0];
``` ```
@ -72,7 +73,7 @@ assert(/=\s*myPlants\[1\].list\[1\]/.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var myPlants = [ const myPlants = [
{ {
type: "flowers", type: "flowers",
list: [ list: [
@ -91,13 +92,13 @@ var myPlants = [
} }
]; ];
var secondTree = ""; const secondTree = "";
``` ```
# --solutions-- # --solutions--
```js ```js
var myPlants = [ const myPlants = [
{ {
type: "flowers", type: "flowers",
list: [ list: [
@ -116,5 +117,5 @@ var myPlants = [
} }
]; ];
var secondTree = myPlants[1].list[1]; const secondTree = myPlants[1].list[1];
``` ```

View File

@ -14,7 +14,7 @@ dashedName: accessing-nested-objects
這是一個嵌套對象: 這是一個嵌套對象:
```js ```js
var ourStorage = { const ourStorage = {
"desk": { "desk": {
"drawer": "stapler" "drawer": "stapler"
}, },
@ -26,6 +26,7 @@ var ourStorage = {
"bottom drawer": "soda" "bottom drawer": "soda"
} }
}; };
ourStorage.cabinet["top drawer"].folder2; ourStorage.cabinet["top drawer"].folder2;
ourStorage.desk.drawer; ourStorage.desk.drawer;
``` ```
@ -66,7 +67,7 @@ assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var myStorage = { const myStorage = {
"car": { "car": {
"inside": { "inside": {
"glove box": "maps", "glove box": "maps",
@ -78,13 +79,13 @@ var myStorage = {
} }
}; };
var gloveBoxContents = undefined; const gloveBoxContents = undefined;
``` ```
# --solutions-- # --solutions--
```js ```js
var myStorage = { const myStorage = {
"car":{ "car":{
"inside":{ "inside":{
"glove box":"maps", "glove box":"maps",
@ -95,5 +96,5 @@ var myStorage = {
} }
} }
}; };
var gloveBoxContents = myStorage.car.inside["glove box"]; const gloveBoxContents = myStorage.car.inside["glove box"];
``` ```

View File

@ -16,11 +16,12 @@ dashedName: accessing-object-properties-with-bracket-notation
這是一個使用方括號表示法讀取對象屬性的例子: 這是一個使用方括號表示法讀取對象屬性的例子:
```js ```js
var myObj = { const myObj = {
"Space Name": "Kirk", "Space Name": "Kirk",
"More Space": "Spock", "More Space": "Spock",
"NoSpace": "USS Enterprise" "NoSpace": "USS Enterprise"
}; };
myObj["Space Name"]; myObj["Space Name"];
myObj['More Space']; myObj['More Space'];
myObj["NoSpace"]; myObj["NoSpace"];
@ -78,26 +79,25 @@ assert(code.match(/testObj\s*?\[('|")[^'"]+\1\]/g).length > 1);
```js ```js
// Setup // Setup
var testObj = { const testObj = {
"an entree": "hamburger", "an entree": "hamburger",
"my side": "veggies", "my side": "veggies",
"the drink": "water" "the drink": "water"
}; };
// Only change code below this line // Only change code below this line
const entreeValue = testObj; // Change this line
var entreeValue = testObj; // Change this line const drinkValue = testObj; // Change this line
var drinkValue = testObj; // Change this line
``` ```
# --solutions-- # --solutions--
```js ```js
var testObj = { const testObj = {
"an entree": "hamburger", "an entree": "hamburger",
"my side": "veggies", "my side": "veggies",
"the drink": "water" "the drink": "water"
}; };
var entreeValue = testObj["an entree"]; const entreeValue = testObj["an entree"];
var drinkValue = testObj['the drink']; const drinkValue = testObj['the drink'];
``` ```

View File

@ -16,15 +16,17 @@ dashedName: accessing-object-properties-with-dot-notation
這裏是一個用點符號(`.`)讀取對象屬性的示例: 這裏是一個用點符號(`.`)讀取對象屬性的示例:
```js ```js
var myObj = { const myObj = {
prop1: "val1", prop1: "val1",
prop2: "val2" prop2: "val2"
}; };
var prop1val = myObj.prop1;
var prop2val = myObj.prop2; const prop1val = myObj.prop1;
const prop2val = myObj.prop2;
``` ```
`prop1val` 的值將爲字符串 `val1`,並且`prop2val` 的值將爲字符串 `val2` `prop1val` 的值將爲字符串 `val1`,並且`prop2val` 的值將爲字符串 `val2`
# --instructions-- # --instructions--
使用點號讀取 `testObj` 的屬性值。 將變量 `hatValue` 的值設置爲該對象的 `hat` 屬性的值,並將變量 `shirtValue` 的值設置爲該對象的 `shirt` 屬性的值。 使用點號讀取 `testObj` 的屬性值。 將變量 `hatValue` 的值設置爲該對象的 `hat` 屬性的值,並將變量 `shirtValue` 的值設置爲該對象的 `shirt` 屬性的值。
@ -73,27 +75,26 @@ assert(code.match(/testObj\.\w+/g).length > 1);
```js ```js
// Setup // Setup
var testObj = { const testObj = {
"hat": "ballcap", "hat": "ballcap",
"shirt": "jersey", "shirt": "jersey",
"shoes": "cleats" "shoes": "cleats"
}; };
// Only change code below this line // Only change code below this line
const hatValue = testObj; // Change this line
var hatValue = testObj; // Change this line const shirtValue = testObj; // Change this line
var shirtValue = testObj; // Change this line
``` ```
# --solutions-- # --solutions--
```js ```js
var testObj = { const testObj = {
"hat": "ballcap", "hat": "ballcap",
"shirt": "jersey", "shirt": "jersey",
"shoes": "cleats" "shoes": "cleats"
}; };
var hatValue = testObj.hat; const hatValue = testObj.hat;
var shirtValue = testObj.shirt; const shirtValue = testObj.shirt;
``` ```

View File

@ -14,11 +14,14 @@ dashedName: accessing-object-properties-with-variables
以下是一個使用變量來訪問屬性的例子: 以下是一個使用變量來訪問屬性的例子:
```js ```js
var dogs = { const dogs = {
Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle" Fido: "Mutt",
Hunter: "Doberman",
Snoopie: "Beagle"
}; };
var myDog = "Hunter";
var myBreed = dogs[myDog]; const myDog = "Hunter";
const myBreed = dogs[myDog];
console.log(myBreed); console.log(myBreed);
``` ```
@ -27,14 +30,16 @@ console.log(myBreed);
使用這一概念的另一種情況是:屬性的名字是在程序運行期間動態收集得到的。如下所示: 使用這一概念的另一種情況是:屬性的名字是在程序運行期間動態收集得到的。如下所示:
```js ```js
var someObj = { const someObj = {
propName: "John" propName: "John"
}; };
function propPrefix(str) { function propPrefix(str) {
var s = "prop"; const s = "prop";
return s + str; return s + str;
} }
var someProp = propPrefix("Name");
const someProp = propPrefix("Name");
console.log(someObj[someProp]); console.log(someObj[someProp]);
``` ```
@ -96,26 +101,25 @@ if(typeof player !== "undefined"){(function(v){return v;})(player);}
```js ```js
// Setup // Setup
var testObj = { const testObj = {
12: "Namath", 12: "Namath",
16: "Montana", 16: "Montana",
19: "Unitas" 19: "Unitas"
}; };
// Only change code below this line // Only change code below this line
const playerNumber = 42; // Change this line
var playerNumber; // Change this line const player = testObj; // Change this line
var player = testObj; // Change this line
``` ```
# --solutions-- # --solutions--
```js ```js
var testObj = { const testObj = {
12: "Namath", 12: "Namath",
16: "Montana", 16: "Montana",
19: "Unitas" 19: "Unitas"
}; };
var playerNumber = 16; const playerNumber = 16;
var player = testObj[playerNumber]; const player = testObj[playerNumber];
``` ```

View File

@ -28,7 +28,7 @@ ourDog["bark"] = "bow-wow";
例如: 例如:
```js ```js
var ourDog = { const ourDog = {
"name": "Camper", "name": "Camper",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,
@ -67,7 +67,7 @@ assert(!/bark[^\n]:/.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var myDog = { const myDog = {
"name": "Happy Coder", "name": "Happy Coder",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,
@ -80,7 +80,7 @@ var myDog = {
# --solutions-- # --solutions--
```js ```js
var myDog = { const myDog = {
"name": "Happy Coder", "name": "Happy Coder",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,

View File

@ -18,7 +18,7 @@ JavaScript 中,我們通過符號 `+` 來進行加法運算。
**代碼示例:** **代碼示例:**
```js ```js
myVar = 5 + 10; const myVar = 5 + 10;
``` ```
現在,變量 `myVar` 的值爲 `15` 現在,變量 `myVar` 的值爲 `15`
@ -52,11 +52,11 @@ assert(/\+/.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var sum = 10 + 0; const sum = 10 + 0;
``` ```
# --solutions-- # --solutions--
```js ```js
var sum = 10 + 10; const sum = 10 + 10;
``` ```

View File

@ -92,7 +92,7 @@ assert(code.match(/break/g).length > 2);
```js ```js
function switchOfStuff(val) { function switchOfStuff(val) {
var answer = ""; let answer = "";
// Only change code below this line // Only change code below this line
@ -108,7 +108,7 @@ switchOfStuff(1);
```js ```js
function switchOfStuff(val) { function switchOfStuff(val) {
var answer = ""; let answer = "";
switch(val) { switch(val) {
case "a": case "a":

View File

@ -14,8 +14,8 @@ dashedName: appending-variables-to-strings
示例: 示例:
```js ```js
var anAdjective = "awesome!"; const anAdjective = "awesome!";
var ourStr = "freeCodeCamp is "; let ourStr = "freeCodeCamp is ";
ourStr += anAdjective; ourStr += anAdjective;
``` ```
@ -64,15 +64,14 @@ assert(code.match(/myStr\s*\+=\s*someAdjective\s*/).length > 0);
```js ```js
// Change code below this line // Change code below this line
const someAdjective = "";
var someAdjective; let myStr = "Learning to code is ";
var myStr = "Learning to code is ";
``` ```
# --solutions-- # --solutions--
```js ```js
var someAdjective = "neat"; const someAdjective = "neat";
var myStr = "Learning to code is "; let myStr = "Learning to code is ";
myStr += someAdjective; myStr += someAdjective;
``` ```

View File

@ -18,7 +18,7 @@ dashedName: build-javascript-objects
這裏是一個貓對象的樣本: 這裏是一個貓對象的樣本:
```js ```js
var cat = { const cat = {
"name": "Whiskers", "name": "Whiskers",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,
@ -29,7 +29,7 @@ var cat = {
在此示例中,所有屬性都存儲爲字符串,例如 `name``legs``tails`。 然而,你也可以使用數字作爲屬性。 你甚至可以省略單字字符串屬性中的引號,如下所示: 在此示例中,所有屬性都存儲爲字符串,例如 `name``legs``tails`。 然而,你也可以使用數字作爲屬性。 你甚至可以省略單字字符串屬性中的引號,如下所示:
```js ```js
var anotherObject = { const anotherObject = {
make: "Ford", make: "Ford",
5: "five", 5: "five",
"model": "focus" "model": "focus"
@ -139,18 +139,18 @@ assert(
## --seed-contents-- ## --seed-contents--
```js ```js
var myDog = { const myDog = {
// Only change code below this line // Only change code below this line
// Only change code above this line // Only change code above this line
}; };
``` ```
# --solutions-- # --solutions--
```js ```js
var myDog = { const myDog = {
"name": "Camper", "name": "Camper",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,

View File

@ -16,7 +16,7 @@ dashedName: comparison-with-the-equality-operator
```js ```js
function equalityTest(myVal) { function equalityTest(myVal) {
if (myVal == 10) { if (myVal == 10) {
return "Equal"; return "Equal";
} }
return "Not Equal"; return "Not Equal";
} }

View File

@ -20,7 +20,7 @@ myVar = myVar + 5;
其中一種就是 `+=` 運算符。 其中一種就是 `+=` 運算符。
```js ```js
var myVar = 1; let myVar = 1;
myVar += 5; myVar += 5;
console.log(myVar); console.log(myVar);
``` ```
@ -61,9 +61,9 @@ assert(code.match(/\+=/g).length === 3);
```js ```js
assert( assert(
/var a = 3;/.test(code) && /let a = 3;/.test(code) &&
/var b = 17;/.test(code) && /let b = 17;/.test(code) &&
/var c = 12;/.test(code) /let c = 12;/.test(code)
); );
``` ```
@ -78,9 +78,9 @@ assert(
## --seed-contents-- ## --seed-contents--
```js ```js
var a = 3; let a = 3;
var b = 17; let b = 17;
var c = 12; let c = 12;
// Only change code below this line // Only change code below this line
a = a + 12; a = a + 12;
@ -91,9 +91,9 @@ c = c + 7;
# --solutions-- # --solutions--
```js ```js
var a = 3; let a = 3;
var b = 17; let b = 17;
var c = 12; let c = 12;
a += 12; a += 12;
b += 9; b += 9;

View File

@ -55,9 +55,9 @@ assert(code.match(/\/=/g).length === 3);
```js ```js
assert( assert(
/var a = 48;/.test(code) && /let a = 48;/.test(code) &&
/var b = 108;/.test(code) && /let b = 108;/.test(code) &&
/var c = 33;/.test(code) /let c = 33;/.test(code)
); );
``` ```
@ -72,9 +72,9 @@ assert(
## --seed-contents-- ## --seed-contents--
```js ```js
var a = 48; let a = 48;
var b = 108; let b = 108;
var c = 33; let c = 33;
// Only change code below this line // Only change code below this line
a = a / 12; a = a / 12;
@ -85,9 +85,9 @@ c = c / 11;
# --solutions-- # --solutions--
```js ```js
var a = 48; let a = 48;
var b = 108; let b = 108;
var c = 33; let c = 33;
a /= 12; a /= 12;
b /= 4; b /= 4;

View File

@ -55,9 +55,9 @@ assert(code.match(/\*=/g).length === 3);
```js ```js
assert( assert(
/var a = 5;/.test(code) && /let a = 5;/.test(code) &&
/var b = 12;/.test(code) && /let b = 12;/.test(code) &&
/var c = 4\.6;/.test(code) /let c = 4\.6;/.test(code)
); );
``` ```
@ -72,9 +72,9 @@ assert(
## --seed-contents-- ## --seed-contents--
```js ```js
var a = 5; let a = 5;
var b = 12; let b = 12;
var c = 4.6; let c = 4.6;
// Only change code below this line // Only change code below this line
a = a * 5; a = a * 5;
@ -85,9 +85,9 @@ c = c * 10;
# --solutions-- # --solutions--
```js ```js
var a = 5; let a = 5;
var b = 12; let b = 12;
var c = 4.6; let c = 4.6;
a *= 5; a *= 5;
b *= 3; b *= 3;

View File

@ -55,7 +55,7 @@ assert(code.match(/-=/g).length === 3);
```js ```js
assert( assert(
/var a = 11;/.test(code) && /var b = 9;/.test(code) && /var c = 3;/.test(code) /let a = 11;/.test(code) && /let b = 9;/.test(code) && /let c = 3;/.test(code)
); );
``` ```
@ -70,9 +70,9 @@ assert(
## --seed-contents-- ## --seed-contents--
```js ```js
var a = 11; let a = 11;
var b = 9; let b = 9;
var c = 3; let c = 3;
// Only change code below this line // Only change code below this line
a = a - 6; a = a - 6;
@ -83,9 +83,9 @@ c = c - 1;
# --solutions-- # --solutions--
```js ```js
var a = 11; let a = 11;
var b = 9; let b = 9;
var c = 3; let c = 3;
a -= 6; a -= 6;
b -= 15; b -= 15;

View File

@ -16,7 +16,7 @@ dashedName: concatenating-strings-with-the-plus-equals-operator
例如: 例如:
```js ```js
var ourStr = "I come first. "; let ourStr = "I come first. ";
ourStr += "I come second."; ourStr += "I come second.";
``` ```
@ -57,14 +57,12 @@ assert(code.match(/myStr\s*\+=\s*(["']).*\1/g));
## --seed-contents-- ## --seed-contents--
```js ```js
// Only change code below this line let myStr;
var myStr;
``` ```
# --solutions-- # --solutions--
```js ```js
var myStr = "This is the first sentence. "; let myStr = "This is the first sentence. ";
myStr += "This is the second sentence."; myStr += "This is the second sentence.";
``` ```

View File

@ -14,8 +14,8 @@ dashedName: constructing-strings-with-variables
例如: 例如:
```js ```js
var ourName = "freeCodeCamp"; const ourName = "freeCodeCamp";
var ourStr = "Hello, our name is " + ourName + ", how are you?"; const ourStr = "Hello, our name is " + ourName + ", how are you?";
``` ```
`ourStr` 值爲 `Hello, our name is freeCodeCamp, how are you?` `ourStr` 值爲 `Hello, our name is freeCodeCamp, how are you?`
@ -63,13 +63,13 @@ assert(code.match(/["']\s*\+\s*myName\s*\+\s*["']/g).length > 0);
```js ```js
// Only change code below this line // Only change code below this line
var myName; const myName = "";
var myStr; const myStr = "";
``` ```
# --solutions-- # --solutions--
```js ```js
var myName = "Bob"; const myName = "Bob";
var myStr = "My name is " + myName + " and I am well!"; const myStr = "My name is " + myName + " and I am well!";
``` ```

View File

@ -159,7 +159,7 @@ assert(
## --seed-contents-- ## --seed-contents--
```js ```js
var count = 0; let count = 0;
function cc(card) { function cc(card) {
// Only change code below this line // Only change code below this line
@ -175,7 +175,7 @@ cc(2); cc(3); cc(7); cc('K'); cc('A');
# --solutions-- # --solutions--
```js ```js
var count = 0; let count = 0;
function cc(card) { function cc(card) {
switch(card) { switch(card) {
case 2: case 2:

View File

@ -42,13 +42,14 @@ assert(myDecimal % 1 != 0);
## --seed-contents-- ## --seed-contents--
```js ```js
var ourDecimal = 5.7; const ourDecimal = 5.7;
// Only change code below this line // Only change code below this line
``` ```
# --solutions-- # --solutions--
```js ```js
var myDecimal = 9.9; const myDecimal = 9.9;
``` ```

View File

@ -39,7 +39,7 @@ assert(myVar === 10);
```js ```js
assert( assert(
/var\s*myVar\s*=\s*11;\s*\/*.*\s*([-]{2}\s*myVar|myVar\s*[-]{2});/.test(code) /let\s*myVar\s*=\s*11;\s*\/*.*\s*([-]{2}\s*myVar|myVar\s*[-]{2});/.test(code)
); );
``` ```
@ -52,7 +52,7 @@ assert(/[-]{2}\s*myVar|myVar\s*[-]{2}/.test(code));
不應修改註釋上方的代碼。 不應修改註釋上方的代碼。
```js ```js
assert(/var myVar = 11;/.test(code)); assert(/let myVar = 11;/.test(code));
``` ```
# --seed-- # --seed--
@ -66,7 +66,7 @@ assert(/var myVar = 11;/.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var myVar = 11; let myVar = 11;
// Only change code below this line // Only change code below this line
myVar = myVar - 1; myVar = myVar - 1;
@ -75,6 +75,6 @@ myVar = myVar - 1;
# --solutions-- # --solutions--
```js ```js
var myVar = 11; let myVar = 11;
myVar--; myVar--;
``` ```

View File

@ -18,7 +18,7 @@ delete ourDog.bark;
例如: 例如:
```js ```js
var ourDog = { const ourDog = {
"name": "Camper", "name": "Camper",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,
@ -70,7 +70,7 @@ assert(code.match(/"tails": 1/g).length > 0);
```js ```js
// Setup // Setup
var myDog = { const myDog = {
"name": "Happy Coder", "name": "Happy Coder",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,
@ -79,12 +79,13 @@ var myDog = {
}; };
// Only change code below this line // Only change code below this line
``` ```
# --solutions-- # --solutions--
```js ```js
var myDog = { const myDog = {
"name": "Happy Coder", "name": "Happy Coder",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,

View File

@ -46,11 +46,11 @@ assert(code.match(/quotient/g).length === 1);
## --seed-contents-- ## --seed-contents--
```js ```js
var quotient = 0.0 / 2.0; // Change this line const quotient = 0.0 / 2.0; // Change this line
``` ```
# --solutions-- # --solutions--
```js ```js
var quotient = 4.4 / 2.0; const quotient = 4.4 / 2.0;
``` ```

View File

@ -16,7 +16,7 @@ JavaScript 中使用 `/` 符號做除法運算。
**示例** **示例**
```js ```js
myVar = 16 / 2; const myVar = 16 / 2;
``` ```
現在,變量 `myVar` 的值爲 `8` 現在,變量 `myVar` 的值爲 `8`
@ -49,11 +49,11 @@ assert(/\d+\s*\/\s*\d+/.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var quotient = 66 / 0; const quotient = 66 / 0;
``` ```
# --solutions-- # --solutions--
```js ```js
var quotient = 66 / 33; const quotient = 66 / 33;
``` ```

View File

@ -89,11 +89,11 @@ console.log('myStr:\n' + myStr);}})();
## --seed-contents-- ## --seed-contents--
```js ```js
var myStr; // Change this line const myStr = ""; // Change this line
``` ```
# --solutions-- # --solutions--
```js ```js
var myStr = "FirstLine\n\t\\SecondLine\nThirdLine"; const myStr = "FirstLine\n\t\\SecondLine\nThirdLine";
``` ```

View File

@ -14,7 +14,7 @@ dashedName: escaping-literal-quotes-in-strings
在 JavaScript 中,可以通過在引號前面使用<dfn>反斜槓</dfn>`\`)來<dfn>轉義</dfn>引號。 在 JavaScript 中,可以通過在引號前面使用<dfn>反斜槓</dfn>`\`)來<dfn>轉義</dfn>引號。
```js ```js
var sampleStr = "Alan said, \"Peter is learning JavaScript\"."; const sampleStr = "Alan said, \"Peter is learning JavaScript\".";
``` ```
有了轉義符號JavaScript 就知道這個單引號或雙引號並不是字符串的結尾,而是字符串內的字符。 所以,上面的字符串打印到控制檯的結果爲: 有了轉義符號JavaScript 就知道這個單引號或雙引號並不是字符串的結尾,而是字符串內的字符。 所以,上面的字符串打印到控制檯的結果爲:
@ -62,11 +62,11 @@ assert(/I am a "double quoted" string inside "double quotes(\."|"\.)$/.test(mySt
## --seed-contents-- ## --seed-contents--
```js ```js
var myStr = ""; // Change this line const myStr = ""; // Change this line
``` ```
# --solutions-- # --solutions--
```js ```js
var myStr = "I am a \"double quoted\" string inside \"double quotes\"."; const myStr = "I am a \"double quoted\" string inside \"double quotes\".";
``` ```

View File

@ -31,7 +31,7 @@ dashedName: finding-a-remainder-in-javascript
變量 `remainder` 應該被初始化。 變量 `remainder` 應該被初始化。
```js ```js
assert(/var\s+?remainder/.test(code)); assert(/(const|let|var)\s+?remainder/.test(code));
``` ```
`remainder` 的值應該等於 `2` `remainder` 的值應該等於 `2`
@ -57,13 +57,11 @@ assert(/\s+?remainder\s*?=\s*?.*%.*;?/.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
// Only change code below this line const remainder = 0;
var remainder;
``` ```
# --solutions-- # --solutions--
```js ```js
var remainder = 11 % 3; const remainder = 11 % 3;
``` ```

View File

@ -14,9 +14,10 @@ dashedName: global-vs--local-scope-in-functions
下面爲例: 下面爲例:
```js ```js
var someVar = "Hat"; const someVar = "Hat";
function myFun() { function myFun() {
var someVar = "Head"; const someVar = "Head";
return someVar; return someVar;
} }
``` ```
@ -53,13 +54,11 @@ assert(/return outerWear/.test(code));
```js ```js
// Setup // Setup
var outerWear = "T-Shirt"; const outerWear = "T-Shirt";
function myOutfit() { function myOutfit() {
// Only change code below this line // Only change code below this line
// Only change code above this line // Only change code above this line
return outerWear; return outerWear;
} }
@ -70,9 +69,9 @@ myOutfit();
# --solutions-- # --solutions--
```js ```js
var outerWear = "T-Shirt"; const outerWear = "T-Shirt";
function myOutfit() { function myOutfit() {
var outerWear = "sweater"; const outerWear = "sweater";
return outerWear; return outerWear;
} }
``` ```

View File

@ -90,7 +90,8 @@ assert(golfScore(5, 9) === 'Go Home!');
## --seed-contents-- ## --seed-contents--
```js ```js
var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"]; const names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) { function golfScore(par, strokes) {
// Only change code below this line // Only change code below this line

View File

@ -39,7 +39,7 @@ assert(myVar === 88);
```js ```js
assert( assert(
/var\s*myVar\s*=\s*87;\s*\/*.*\s*([+]{2}\s*myVar|myVar\s*[+]{2});/.test(code) /let\s*myVar\s*=\s*87;\s*\/*.*\s*([+]{2}\s*myVar|myVar\s*[+]{2});/.test(code)
); );
``` ```
@ -52,7 +52,7 @@ assert(/[+]{2}\s*myVar|myVar\s*[+]{2}/.test(code));
不應該修改註釋上面的代碼。 不應該修改註釋上面的代碼。
```js ```js
assert(/var myVar = 87;/.test(code)); assert(/let myVar = 87;/.test(code));
``` ```
# --seed-- # --seed--
@ -66,7 +66,7 @@ assert(/var myVar = 87;/.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var myVar = 87; let myVar = 87;
// Only change code below this line // Only change code below this line
myVar = myVar + 1; myVar = myVar + 1;
@ -75,6 +75,6 @@ myVar = myVar + 1;
# --solutions-- # --solutions--
```js ```js
var myVar = 87; let myVar = 87;
myVar++; myVar++;
``` ```

View File

@ -64,7 +64,7 @@ assert(testElse(10) === 'Bigger than 5');
不要修改相應註釋的上面或下面的代碼。 不要修改相應註釋的上面或下面的代碼。
```js ```js
assert(/var result = "";/.test(code) && /return result;/.test(code)); assert(/let result = "";/.test(code) && /return result;/.test(code));
``` ```
# --seed-- # --seed--
@ -73,7 +73,7 @@ assert(/var result = "";/.test(code) && /return result;/.test(code));
```js ```js
function testElse(val) { function testElse(val) {
var result = ""; let result = "";
// Only change code below this line // Only change code below this line
if (val > 5) { if (val > 5) {
@ -95,7 +95,7 @@ testElse(4);
```js ```js
function testElse(val) { function testElse(val) {
var result = ""; let result = "";
if(val > 5) { if(val > 5) {
result = "Bigger than 5"; result = "Bigger than 5";
} else { } else {

View File

@ -12,8 +12,9 @@ dashedName: iterate-through-an-array-with-a-for-loop
JavaScript 中的一個常見任務是遍歷數組的內容。 一種方法是使用 `for` 循環。 下面的代碼將輸出數組 `arr` 的每個元素到控制檯: JavaScript 中的一個常見任務是遍歷數組的內容。 一種方法是使用 `for` 循環。 下面的代碼將輸出數組 `arr` 的每個元素到控制檯:
```js ```js
var arr = [10, 9, 8, 7, 6]; const arr = [10, 9, 8, 7, 6];
for (var i = 0; i < arr.length; i++) {
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]); console.log(arr[i]);
} }
``` ```
@ -62,18 +63,19 @@ assert(!__helpers.removeWhiteSpace(code).match(/total[=+-]0*[1-9]+/gm));
```js ```js
// Setup // Setup
var myArr = [ 2, 3, 4, 5, 6]; const myArr = [2, 3, 4, 5, 6];
// Only change code below this line // Only change code below this line
``` ```
# --solutions-- # --solutions--
```js ```js
var myArr = [ 2, 3, 4, 5, 6]; const myArr = [2, 3, 4, 5, 6];
var total = 0; let total = 0;
for (var i = 0; i < myArr.length; i++) { for (let i = 0; i < myArr.length; i++) {
total += myArr[i]; total += myArr[i];
} }
``` ```

View File

@ -12,8 +12,9 @@ dashedName: iterate-with-javascript-do---while-loops
下一種循環叫作 `do...while` 循環。 它被稱爲 `do...while` 循環,是因爲不論什麼情況,它都會首先 `do`(運行)循環裏的第一部分代碼,然後 `while`(當)規定的條件被評估爲 `true`(真)的時候,它會繼續運行循環。 下一種循環叫作 `do...while` 循環。 它被稱爲 `do...while` 循環,是因爲不論什麼情況,它都會首先 `do`(運行)循環裏的第一部分代碼,然後 `while`(當)規定的條件被評估爲 `true`(真)的時候,它會繼續運行循環。
```js ```js
var ourArray = []; const ourArray = [];
var i = 0; let i = 0;
do { do {
ourArray.push(i); ourArray.push(i);
i++; i++;
@ -23,8 +24,9 @@ do {
上面的示例行爲類似於其他類型的循環,由此產生的數組將看起來像 `[0, 1, 2, 3, 4]`。 然而,`do...while` 不同於其他循環的地方,是第一次循環檢查失敗時的行爲。 讓我們看看代碼中的區別:這裏是一個常規的 `while` 循環,只要 `i < 5`,就會在循環中運行代碼: 上面的示例行爲類似於其他類型的循環,由此產生的數組將看起來像 `[0, 1, 2, 3, 4]`。 然而,`do...while` 不同於其他循環的地方,是第一次循環檢查失敗時的行爲。 讓我們看看代碼中的區別:這裏是一個常規的 `while` 循環,只要 `i < 5`,就會在循環中運行代碼:
```js ```js
var ourArray = []; const ourArray = [];
var i = 5; let i = 5;
while (i < 5) { while (i < 5) {
ourArray.push(i); ourArray.push(i);
i++; i++;
@ -34,8 +36,9 @@ while (i < 5) {
這個例子中,定義了一個空數組 `ourArray` 以及一個值爲 5 的 `i` 。 當執行 `while` 循環時,因爲 `i` 不小於 5所以循環條件爲 `false`,循環內的代碼將不會執行。 `ourArray` 最終沒有添加任何內容因此示例中的所有代碼執行完時ourArray 仍然是`[]`。 現在,看一下 `do...while` 循環。 這個例子中,定義了一個空數組 `ourArray` 以及一個值爲 5 的 `i` 。 當執行 `while` 循環時,因爲 `i` 不小於 5所以循環條件爲 `false`,循環內的代碼將不會執行。 `ourArray` 最終沒有添加任何內容因此示例中的所有代碼執行完時ourArray 仍然是`[]`。 現在,看一下 `do...while` 循環。
```js ```js
var ourArray = []; const ourArray = [];
var i = 5; let i = 5;
do { do {
ourArray.push(i); ourArray.push(i);
i++; i++;
@ -80,8 +83,8 @@ if(typeof myArray !== "undefined"){(function(){return myArray;})();}
```js ```js
// Setup // Setup
var myArray = []; const myArray = [];
var i = 10; let i = 10;
// Only change code below this line // Only change code below this line
while (i < 5) { while (i < 5) {
@ -93,8 +96,8 @@ while (i < 5) {
# --solutions-- # --solutions--
```js ```js
var myArray = []; const myArray = [];
var i = 10; let i = 10;
do { do {
myArray.push(i); myArray.push(i);
i++; i++;

View File

@ -15,9 +15,10 @@ dashedName: local-scope-and-functions
```js ```js
function myTest() { function myTest() {
var loc = "foo"; const loc = "foo";
console.log(loc); console.log(loc);
} }
myTest(); myTest();
console.log(loc); console.log(loc);
``` ```
@ -38,6 +39,7 @@ console.log(loc);
function declared() { function declared() {
myVar; myVar;
} }
assert.throws(declared, ReferenceError); assert.throws(declared, ReferenceError);
``` ```
@ -57,7 +59,6 @@ assert(
```js ```js
function myLocalScope() { function myLocalScope() {
// Only change code below this line // Only change code below this line
console.log('inside myLocalScope', myVar); console.log('inside myLocalScope', myVar);
@ -73,9 +74,8 @@ console.log('outside myLocalScope', myVar);
```js ```js
function myLocalScope() { function myLocalScope() {
// Only change code below this line // Only change code below this line
var myVar; let myVar;
console.log('inside myLocalScope', myVar); console.log('inside myLocalScope', myVar);
} }
myLocalScope(); myLocalScope();

View File

@ -16,10 +16,10 @@ dashedName: manipulate-arrays-with-push
示例: 示例:
```js ```js
var arr1 = [1,2,3]; const arr1 = [1, 2, 3];
arr1.push(4); arr1.push(4);
var arr2 = ["Stimpson", "J", "cat"]; const arr2 = ["Stimpson", "J", "cat"];
arr2.push(["happy", "joy"]); arr2.push(["happy", "joy"]);
``` ```
@ -64,14 +64,15 @@ assert(
```js ```js
// Setup // Setup
var myArray = [["John", 23], ["cat", 2]]; const myArray = [["John", 23], ["cat", 2]];
// Only change code below this line // Only change code below this line
``` ```
# --solutions-- # --solutions--
```js ```js
var myArray = [["John", 23], ["cat", 2]]; const myArray = [["John", 23], ["cat", 2]];
myArray.push(["dog",3]); myArray.push(["dog",3]);
``` ```

View File

@ -14,7 +14,7 @@ dashedName: manipulating-complex-objects
這是一個複雜數據結構的示例: 這是一個複雜數據結構的示例:
```js ```js
var ourMusic = [ const ourMusic = [
{ {
"artist": "Daft Punk", "artist": "Daft Punk",
"title": "Homework", "title": "Homework",
@ -135,7 +135,7 @@ myMusic.forEach(object => {
## --seed-contents-- ## --seed-contents--
```js ```js
var myMusic = [ const myMusic = [
{ {
"artist": "Billy Joel", "artist": "Billy Joel",
"title": "Piano Man", "title": "Piano Man",
@ -153,7 +153,7 @@ var myMusic = [
# --solutions-- # --solutions--
```js ```js
var myMusic = [ const myMusic = [
{ {
"artist": "Billy Joel", "artist": "Billy Joel",
"title": "Piano Man", "title": "Piano Man",

View File

@ -12,7 +12,7 @@ dashedName: multiple-identical-options-in-switch-statements
如果你忘了給 `switch` 的每一條 `case` 添加 `break`,那麼後續的 `case` 會一直執行,直到遇見 `break` 爲止。 如果你想爲 `switch` 中的多個不同的輸入設置相同的結果,可以這樣寫: 如果你忘了給 `switch` 的每一條 `case` 添加 `break`,那麼後續的 `case` 會一直執行,直到遇見 `break` 爲止。 如果你想爲 `switch` 中的多個不同的輸入設置相同的結果,可以這樣寫:
```js ```js
var result = ""; let result = "";
switch(val) { switch(val) {
case 1: case 1:
case 2: case 2:
@ -109,7 +109,7 @@ assert(code.match(/case/g).length === 9);
```js ```js
function sequentialSizes(val) { function sequentialSizes(val) {
var answer = ""; let answer = "";
// Only change code below this line // Only change code below this line
@ -125,7 +125,7 @@ sequentialSizes(1);
```js ```js
function sequentialSizes(val) { function sequentialSizes(val) {
var answer = ""; let answer = "";
switch(val) { switch(val) {
case 1: case 1:

View File

@ -42,11 +42,11 @@ assert(/\*/.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var product = 2.0 * 0.0; const product = 2.0 * 0.0;
``` ```
# --solutions-- # --solutions--
```js ```js
var product = 2.0 * 2.5; const product = 2.0 * 2.5;
``` ```

View File

@ -16,7 +16,7 @@ JavaScript 使用 `*` 符號表示兩數相乘。
**示例** **示例**
```js ```js
myVar = 13 * 13; const myVar = 13 * 13;
``` ```
現在,變量 `myVar` 的值爲 `169` 現在,變量 `myVar` 的值爲 `169`
@ -50,11 +50,11 @@ assert(/\*/.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var product = 8 * 0; const product = 8 * 0;
``` ```
# --solutions-- # --solutions--
```js ```js
var product = 8 * 10; const product = 8 * 10;
``` ```

View File

@ -12,7 +12,7 @@ dashedName: nest-one-array-within-another-array
您也可以在其他數組中嵌套數組,如: 您也可以在其他數組中嵌套數組,如:
```js ```js
[["Bulls", 23], ["White Sox", 45]] const teams = [["Bulls", 23], ["White Sox", 45]];
``` ```
這也叫做多維數組(<dfn>multi-dimensional array</dfn>)。 這也叫做多維數組(<dfn>multi-dimensional array</dfn>)。
@ -41,11 +41,11 @@ if(typeof myArray !== "undefined"){(function(){return myArray;})();}
```js ```js
// Only change code below this line // Only change code below this line
var myArray = []; const myArray = [];
``` ```
# --solutions-- # --solutions--
```js ```js
var myArray = [[1,2,3]]; const myArray = [[1, 2, 3]];
``` ```

View File

@ -68,34 +68,33 @@ assert(lookUpProfile('Akira', 'address') === 'No such property');
```js ```js
// Setup // Setup
var contacts = [ const contacts = [
{ {
"firstName": "Akira", firstName: "Akira",
"lastName": "Laine", lastName: "Laine",
"number": "0543236543", number: "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"] likes: ["Pizza", "Coding", "Brownie Points"],
}, },
{ {
"firstName": "Harry", firstName: "Harry",
"lastName": "Potter", lastName: "Potter",
"number": "0994372684", number: "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"] likes: ["Hogwarts", "Magic", "Hagrid"],
}, },
{ {
"firstName": "Sherlock", firstName: "Sherlock",
"lastName": "Holmes", lastName: "Holmes",
"number": "0487345643", number: "0487345643",
"likes": ["Intriguing Cases", "Violin"] likes: ["Intriguing Cases", "Violin"],
}, },
{ {
"firstName": "Kristian", firstName: "Kristian",
"lastName": "Vos", lastName: "Vos",
"number": "unknown", number: "unknown",
"likes": ["JavaScript", "Gaming", "Foxes"] likes: ["JavaScript", "Gaming", "Foxes"],
} },
]; ];
function lookUpProfile(name, prop) { function lookUpProfile(name, prop) {
// Only change code below this line // Only change code below this line
@ -108,44 +107,38 @@ lookUpProfile("Akira", "likes");
# --solutions-- # --solutions--
```js ```js
var contacts = [ const contacts = [
{ {
"firstName": "Akira", firstName: "Akira",
"lastName": "Laine", lastName: "Laine",
"number": "0543236543", number: "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"] likes: ["Pizza", "Coding", "Brownie Points"],
}, },
{ {
"firstName": "Harry", firstName: "Harry",
"lastName": "Potter", lastName: "Potter",
"number": "0994372684", number: "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"] likes: ["Hogwarts", "Magic", "Hagrid"],
}, },
{ {
"firstName": "Sherlock", firstName: "Sherlock",
"lastName": "Holmes", lastName: "Holmes",
"number": "0487345643", number: "0487345643",
"likes": ["Intriguing Cases", "Violin"] likes: ["Intriguing Cases", "Violin"],
}, },
{ {
"firstName": "Kristian", firstName: "Kristian",
"lastName": "Vos", lastName: "Vos",
"number": "unknown", number: "unknown",
"likes": ["JavaScript", "Gaming", "Foxes"] likes: ["JavaScript", "Gaming", "Foxes"],
}, },
]; ];
function lookUpProfile(name, prop) {
for (let i in contacts) {
//Write your function in between these comments if (contacts[i].firstName === name) {
function lookUpProfile(name, prop){ return contacts[i][prop] || "No such property";
for(var i in contacts){
if(contacts[i].firstName === name) {
return contacts[i][prop] || "No such property";
}
} }
return "No such contact"; }
return "No such contact";
} }
//Write your function in between these comments
lookUpProfile("Akira", "likes");
``` ```

View File

@ -12,21 +12,21 @@ dashedName: quoting-strings-with-single-quotes
JavaScript 中的<dfn>字符串</dfn>可以使用開始和結束都是同類型的單引號或雙引號表示。 與其他一些編程語言不同的是,單引號和雙引號的功能在 JavaScript 中是相同的。 JavaScript 中的<dfn>字符串</dfn>可以使用開始和結束都是同類型的單引號或雙引號表示。 與其他一些編程語言不同的是,單引號和雙引號的功能在 JavaScript 中是相同的。
```js ```js
doubleQuoteStr = "This is a string"; const doubleQuoteStr = "This is a string";
singleQuoteStr = 'This is also a string'; const singleQuoteStr = 'This is also a string';
``` ```
當你需要在一個字符串中使用多個引號的時候,你可以使用單引號包裹雙引號或者相反。 常見的場景比如在字符串中包含對話的句子需要用引號包裹。 另外比如在一個包含有 `<a>` 標籤的字符串中,標籤的屬性值需要用引號包裹。 當你需要在一個字符串中使用多個引號的時候,你可以使用單引號包裹雙引號或者相反。 常見的場景比如在字符串中包含對話的句子需要用引號包裹。 另外比如在一個包含有 `<a>` 標籤的字符串中,標籤的屬性值需要用引號包裹。
```js ```js
conversation = 'Finn exclaims to Jake, "Algebraic!"'; const conversation = 'Finn exclaims to Jake, "Algebraic!"';
``` ```
然而,如果你需要在其中使用外面的引號,這就成爲一個問題。 記住,一個字符串在開頭和結尾處有相同的引號。 要知道,字符串在開頭和結尾都有相同的引號,如果在中間使用了相同的引號,字符串會提前中止並拋出錯誤。 然而,如果你需要在其中使用外面的引號,這就成爲一個問題。 記住,一個字符串在開頭和結尾處有相同的引號。 要知道,字符串在開頭和結尾都有相同的引號,如果在中間使用了相同的引號,字符串會提前中止並拋出錯誤。
```js ```js
goodStr = 'Jake asks Finn, "Hey, let\'s go on an adventure?"'; const goodStr = 'Jake asks Finn, "Hey, let\'s go on an adventure?"';
badStr = 'Finn responds, "Let's go!"'; const badStr = 'Finn responds, "Let's go!"';
``` ```
在這裏 `badStr` 會產生一個錯誤。 在這裏 `badStr` 會產生一個錯誤。
@ -71,11 +71,11 @@ assert(code.match(/"/g).length === 4 && code.match(/'/g).length === 2);
## --seed-contents-- ## --seed-contents--
```js ```js
var myStr = "<a href=\"http://www.example.com\" target=\"_blank\">Link</a>"; const myStr = "<a href=\"http://www.example.com\" target=\"_blank\">Link</a>";
``` ```
# --solutions-- # --solutions--
```js ```js
var myStr = '<a href="http://www.example.com" target="_blank">Link</a>'; const myStr = '<a href="http://www.example.com" target="_blank">Link</a>';
``` ```

View File

@ -115,7 +115,7 @@ const _recordCollection = {
```js ```js
// Setup // Setup
var recordCollection = { const recordCollection = {
2548: { 2548: {
albumTitle: 'Slippery When Wet', albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi', artist: 'Bon Jovi',
@ -146,7 +146,7 @@ updateRecords(recordCollection, 5439, 'artist', 'ABBA');
# --solutions-- # --solutions--
```js ```js
var recordCollection = { const recordCollection = {
2548: { 2548: {
albumTitle: 'Slippery When Wet', albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi', artist: 'Bon Jovi',

View File

@ -14,9 +14,9 @@ dashedName: replace-loops-using-recursion
```js ```js
function multiply(arr, n) { function multiply(arr, n) {
var product = 1; let product = 1;
for (var i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
product *= arr[i]; product *= arr[i];
} }
return product; return product;
} }

View File

@ -108,7 +108,7 @@ assert(chainToSwitch(156) === '');
```js ```js
function chainToSwitch(val) { function chainToSwitch(val) {
var answer = ""; let answer = "";
// Only change code below this line // Only change code below this line
if (val === "bob") { if (val === "bob") {
@ -134,7 +134,7 @@ chainToSwitch(7);
```js ```js
function chainToSwitch(val) { function chainToSwitch(val) {
var answer = ""; let answer = "";
switch(val) { switch(val) {
case "bob": case "bob":

View File

@ -17,7 +17,8 @@ dashedName: return-a-value-from-a-function-with-return
function plusThree(num) { function plusThree(num) {
return num + 3; return num + 3;
} }
var answer = plusThree(5);
const answer = plusThree(5);
``` ```
`answer` 的值爲 `8` `answer` 的值爲 `8`

View File

@ -78,7 +78,7 @@ assert(code.match(/break/g).length > 2);
```js ```js
function caseInSwitch(val) { function caseInSwitch(val) {
var answer = ""; let answer = "";
// Only change code below this line // Only change code below this line
@ -94,7 +94,7 @@ caseInSwitch(1);
```js ```js
function caseInSwitch(val) { function caseInSwitch(val) {
var answer = ""; let answer = "";
switch(val) { switch(val) {
case 1: case 1:

View File

@ -81,13 +81,13 @@ var hasNumber = false;
## --seed-contents-- ## --seed-contents--
```js ```js
var myList = []; const myList = [];
``` ```
# --solutions-- # --solutions--
```js ```js
var myList = [ const myList = [
["Candy", 10], ["Candy", 10],
["Potatoes", 12], ["Potatoes", 12],
["Eggs", 12], ["Eggs", 12],

View File

@ -93,12 +93,10 @@ function nextInLine(arr, item) {
return item; return item;
// Only change code above this line // Only change code above this line
} }
// Setup // Setup
var testArr = [1,2,3,4,5]; const testArr = [1, 2, 3, 4, 5];
// Display code // Display code
console.log("Before: " + JSON.stringify(testArr)); console.log("Before: " + JSON.stringify(testArr));
@ -109,7 +107,7 @@ console.log("After: " + JSON.stringify(testArr));
# --solutions-- # --solutions--
```js ```js
var testArr = [ 1,2,3,4,5]; const testArr = [1, 2, 3, 4, 5];
function nextInLine(arr, item) { function nextInLine(arr, item) {
arr.push(item); arr.push(item);

View File

@ -14,7 +14,7 @@ dashedName: store-multiple-values-in-one-variable-using-javascript-arrays
以左方括號開始定義一個數組,以右方括號結束,裏面每個元素之間用逗號隔開,例如: 以左方括號開始定義一個數組,以右方括號結束,裏面每個元素之間用逗號隔開,例如:
```js ```js
var sandwich = ["peanut butter", "jelly", "bread"] const sandwich = ["peanut butter", "jelly", "bread"];
``` ```
# --instructions-- # --instructions--
@ -53,11 +53,11 @@ assert(typeof myArray[1] !== 'undefined' && typeof myArray[1] == 'number');
```js ```js
// Only change code below this line // Only change code below this line
var myArray = []; const myArray = [];
``` ```
# --solutions-- # --solutions--
```js ```js
var myArray = ["The Answer", 42]; const myArray = ["The Answer", 42];
``` ```

View File

@ -16,7 +16,7 @@ JavaScript 中使用 `-` 來做減法運算。
**示例** **示例**
```js ```js
myVar = 12 - 6; const myVar = 12 - 6;
``` ```
現在,變量 `myVar` 的值爲 `6` 現在,變量 `myVar` 的值爲 `6`
@ -49,11 +49,11 @@ assert(/difference=45-33;?/.test(__helpers.removeWhiteSpace(code)));
## --seed-contents-- ## --seed-contents--
```js ```js
var difference = 45 - 0; const difference = 45 - 0;
``` ```
# --solutions-- # --solutions--
```js ```js
var difference = 45 - 33; const difference = 45 - 33;
``` ```

View File

@ -13,10 +13,11 @@ dashedName: testing-objects-for-properties
**示例** **示例**
```js ```js
var myObj = { const myObj = {
top: "hat", top: "hat",
bottom: "pants" bottom: "pants"
}; };
myObj.hasOwnProperty("top"); myObj.hasOwnProperty("top");
myObj.hasOwnProperty("middle"); myObj.hasOwnProperty("middle");
``` ```

View File

@ -14,14 +14,14 @@ dashedName: understand-string-immutability
例如,下面的代碼: 例如,下面的代碼:
```js ```js
var myStr = "Bob"; let myStr = "Bob";
myStr[0] = "J"; myStr[0] = "J";
``` ```
是不會把變量 `myStr` 的值改變成 `Job` 的,因爲變量 `myStr` 是不可變的。 注意,這*並不*意味着 `myStr` 永遠不能被改變,只是字符串字面量 <dfn>string literal</dfn> 的各個字符不能被改變。 改變 `myStr` 的唯一方法是重新給它賦一個值,例如: 是不會把變量 `myStr` 的值改變成 `Job` 的,因爲變量 `myStr` 是不可變的。 注意,這*並不*意味着 `myStr` 永遠不能被改變,只是字符串字面量 <dfn>string literal</dfn> 的各個字符不能被改變。 改變 `myStr` 的唯一方法是重新給它賦一個值,例如:
```js ```js
var myStr = "Bob"; let myStr = "Bob";
myStr = "Job"; myStr = "Job";
``` ```
@ -55,7 +55,7 @@ assert(/myStr = "Jello World"/.test(code));
```js ```js
// Setup // Setup
var myStr = "Jello World"; let myStr = "Jello World";
// Only change code below this line // Only change code below this line
myStr[0] = "H"; // Change this line myStr[0] = "H"; // Change this line
@ -65,6 +65,6 @@ myStr[0] = "H"; // Change this line
# --solutions-- # --solutions--
```js ```js
var myStr = "Jello World"; let myStr = "Jello World";
myStr = "Hello World"; myStr = "Hello World";
``` ```

View File

@ -43,7 +43,6 @@ welcomeToBooleans();
```js ```js
function welcomeToBooleans() { function welcomeToBooleans() {
// Only change code below this line // Only change code below this line
return false; // Change this line return false; // Change this line

View File

@ -14,10 +14,12 @@ dashedName: understanding-undefined-value-returned-from-a-function
**示例** **示例**
```js ```js
var sum = 0; let sum = 0;
function addSum(num) { function addSum(num) {
sum = sum + num; sum = sum + num;
} }
addSum(3); addSum(3);
``` ```
@ -61,7 +63,7 @@ assert(
```js ```js
// Setup // Setup
var sum = 0; let sum = 0;
function addThree() { function addThree() {
sum = sum + 3; sum = sum + 3;
@ -79,7 +81,7 @@ addFive();
# --solutions-- # --solutions--
```js ```js
var sum = 0; let sum = 0;
function addThree() { function addThree() {
sum = sum + 3; sum = sum + 3;

View File

@ -14,7 +14,7 @@ dashedName: updating-object-properties
舉個例子,讓我們看看 `ourDog` 舉個例子,讓我們看看 `ourDog`
```js ```js
var ourDog = { const ourDog = {
"name": "Camper", "name": "Camper",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,
@ -54,7 +54,7 @@ assert(/"name": "Coder"/.test(code));
```js ```js
// Setup // Setup
var myDog = { const myDog = {
"name": "Coder", "name": "Coder",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,
@ -62,12 +62,13 @@ var myDog = {
}; };
// Only change code below this line // Only change code below this line
``` ```
# --solutions-- # --solutions--
```js ```js
var myDog = { const myDog = {
"name": "Coder", "name": "Coder",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,

View File

@ -16,8 +16,8 @@ dashedName: use-bracket-notation-to-find-the-nth-character-in-a-string
例如: 例如:
```js ```js
var firstName = "Ada"; const firstName = "Ada";
var secondLetterOfFirstName = firstName[1]; const secondLetterOfFirstName = firstName[1];
``` ```
`secondLetterOfFirstName` 值應該爲字符串 `d` `secondLetterOfFirstName` 值應該爲字符串 `d`
@ -54,15 +54,15 @@ assert(code.match(/thirdLetterOfLastName\s*?=\s*?lastName\[.*?\]/));
```js ```js
// Setup // Setup
var lastName = "Lovelace"; const lastName = "Lovelace";
// Only change code below this line // Only change code below this line
var thirdLetterOfLastName = lastName; // Change this line const thirdLetterOfLastName = lastName; // Change this line
``` ```
# --solutions-- # --solutions--
```js ```js
var lastName = "Lovelace"; const lastName = "Lovelace";
var thirdLetterOfLastName = lastName[2]; const thirdLetterOfLastName = lastName[2];
``` ```

View File

@ -73,7 +73,7 @@ function rangeOfNumbers(startNum, endNum) {
if (endNum - startNum === 0) { if (endNum - startNum === 0) {
return [startNum]; return [startNum];
} else { } else {
var numbers = rangeOfNumbers(startNum, endNum - 1); const numbers = rangeOfNumbers(startNum, endNum - 1);
numbers.push(endNum); numbers.push(endNum);
return numbers; return numbers;
} }

View File

@ -20,7 +20,7 @@ parseInt(string, radix);
這是一個示例: 這是一個示例:
```js ```js
var a = parseInt("11", 2); const a = parseInt("11", 2);
``` ```
變量 radix 表示 `11` 是在二進制系統中。 這個示例將字符串 `11` 轉換爲整數 `3` 變量 radix 表示 `11` 是在二進制系統中。 這個示例將字符串 `11` 轉換爲整數 `3`

View File

@ -12,7 +12,7 @@ dashedName: use-the-parseint-function
`parseInt()` 函數解析一個字符串返回一個整數。 下面是一個示例: `parseInt()` 函數解析一個字符串返回一個整數。 下面是一個示例:
```js ```js
var a = parseInt("007"); const a = parseInt("007");
``` ```
上述函數將字符串 `007` 轉換爲整數 `7`。 如果字符串中的第一個字符不能轉換爲數字,則返回 `NaN` 上述函數將字符串 `007` 轉換爲整數 `7`。 如果字符串中的第一個字符不能轉換爲數字,則返回 `NaN`

View File

@ -14,7 +14,7 @@ dashedName: using-objects-for-lookups
這是簡單的反向字母表: 這是簡單的反向字母表:
```js ```js
var alpha = { const alpha = {
1:"Z", 1:"Z",
2:"Y", 2:"Y",
3:"X", 3:"X",
@ -24,10 +24,11 @@ var alpha = {
25:"B", 25:"B",
26:"A" 26:"A"
}; };
alpha[2]; alpha[2];
alpha[24]; alpha[24];
var value = 2; const value = 2;
alpha[value]; alpha[value];
``` ```
@ -102,7 +103,7 @@ assert(
```js ```js
// Setup // Setup
function phoneticLookup(val) { function phoneticLookup(val) {
var result = ""; let result = "";
// Only change code below this line // Only change code below this line
switch(val) { switch(val) {
@ -136,9 +137,9 @@ phoneticLookup("charlie");
```js ```js
function phoneticLookup(val) { function phoneticLookup(val) {
var result = ""; let result = "";
var lookup = { const lookup = {
alpha: "Adams", alpha: "Adams",
bravo: "Boston", bravo: "Boston",
charlie: "Chicago", charlie: "Chicago",

View File

@ -16,7 +16,7 @@ dashedName: word-blanks
思考一下這句話 - It was really **\_\_\_\_**, and we **\_\_\_\_** ourselves **\_\_\_\_**。 這句話有三個缺失的部分 - 形容詞、動詞和副詞,選擇合適單詞填入完成它。 然後將完成的句子賦值給變量,如下所示: 思考一下這句話 - It was really **\_\_\_\_**, and we **\_\_\_\_** ourselves **\_\_\_\_**。 這句話有三個缺失的部分 - 形容詞、動詞和副詞,選擇合適單詞填入完成它。 然後將完成的句子賦值給變量,如下所示:
```js ```js
var sentence = "It was really " + "hot" + ", and we " + "laughed" + " ourselves " + "silly" + "."; const sentence = "It was really " + "hot" + ", and we " + "laughed" + " ourselves " + "silly" + ".";
``` ```
# --instructions-- # --instructions--
@ -84,24 +84,24 @@ const removeAssignments = str => str
## --seed-contents-- ## --seed-contents--
```js ```js
var myNoun = "dog"; const myNoun = "dog";
var myAdjective = "big"; const myAdjective = "big";
var myVerb = "ran"; const myVerb = "ran";
var myAdverb = "quickly"; const myAdverb = "quickly";
// Only change code below this line // Only change code below this line
var wordBlanks = ""; // Change this line const wordBlanks = ""; // Change this line
// Only change code above this line // Only change code above this line
``` ```
# --solutions-- # --solutions--
```js ```js
var myNoun = "dog"; const myNoun = "dog";
var myAdjective = "big"; const myAdjective = "big";
var myVerb = "ran"; const myVerb = "ran";
var myAdverb = "quickly"; const myAdverb = "quickly";
var wordBlanks = "Once there was a " + myNoun + " which was very " + myAdjective + ". "; let wordBlanks = "Once there was a " + myNoun + " which was very " + myAdjective + ". ";
wordBlanks += "It " + myVerb + " " + myAdverb + " around the yard."; wordBlanks += "It " + myVerb + " " + myAdverb + " around the yard.";
``` ```

View File

@ -13,7 +13,7 @@ dashedName: add-elements-to-the-end-of-an-array-using-concat-instead-of-push
上一個挑戰介紹了 `concat` 方法,這是一種在不改變原始數組的前提下,將數組組合成新數組的方法。 將 `concat` 方法與 `push` 方法做比較。 `push` 將元素添加到調用它的數組的末尾,這樣會改變該數組。 舉個例子: 上一個挑戰介紹了 `concat` 方法,這是一種在不改變原始數組的前提下,將數組組合成新數組的方法。 將 `concat` 方法與 `push` 方法做比較。 `push` 將元素添加到調用它的數組的末尾,這樣會改變該數組。 舉個例子:
```js ```js
var arr = [1, 2, 3]; const arr = [1, 2, 3];
arr.push([4, 5, 6]); arr.push([4, 5, 6]);
``` ```
@ -71,8 +71,9 @@ function nonMutatingPush(original, newItem) {
// Only change code above this line // Only change code above this line
} }
var first = [1, 2, 3];
var second = [4, 5]; const first = [1, 2, 3];
const second = [4, 5];
nonMutatingPush(first, second); nonMutatingPush(first, second);
``` ```
@ -82,7 +83,6 @@ nonMutatingPush(first, second);
function nonMutatingPush(original, newItem) { function nonMutatingPush(original, newItem) {
return original.concat(newItem); return original.concat(newItem);
} }
var first = [1, 2, 3]; const first = [1, 2, 3];
var second = [4, 5]; const second = [4, 5];
nonMutatingPush(first, second);
``` ```

View File

@ -77,7 +77,6 @@ function urlSlug(title) {
# --solutions-- # --solutions--
```js ```js
// Only change code below this line
function urlSlug(title) { function urlSlug(title) {
return title.trim().split(/\s+/).join("-").toLowerCase(); return title.trim().split(/\s+/).join("-").toLowerCase();
} }

View File

@ -57,9 +57,9 @@ assert(__newValue === 5);
```js ```js
// The global variable // The global variable
var fixedValue = 4; let fixedValue = 4;
function incrementer () { function incrementer() {
// Only change code below this line // Only change code below this line
@ -70,7 +70,7 @@ function incrementer () {
# --solutions-- # --solutions--
```js ```js
var fixedValue = 4 let fixedValue = 4
function incrementer() { function incrementer() {
return fixedValue + 1 return fixedValue + 1

View File

@ -13,8 +13,8 @@ dashedName: combine-an-array-into-a-string-using-the-join-method
舉個例子: 舉個例子:
```js ```js
var arr = ["Hello", "World"]; const arr = ["Hello", "World"];
var str = arr.join(" "); const str = arr.join(" ");
``` ```
`str` 的值應該是字符串 `Hello World` `str` 的值應該是字符串 `Hello World`
@ -76,6 +76,7 @@ function sentensify(str) {
// Only change code above this line // Only change code above this line
} }
sentensify("May-the-force-be-with-you"); sentensify("May-the-force-be-with-you");
``` ```
@ -83,8 +84,6 @@ sentensify("May-the-force-be-with-you");
```js ```js
function sentensify(str) { function sentensify(str) {
// Only change code below this line
return str.split(/\W/).join(' '); return str.split(/\W/).join(' ');
// Only change code above this line
} }
``` ```

View File

@ -60,8 +60,9 @@ function nonMutatingConcat(original, attach) {
// Only change code above this line // Only change code above this line
} }
var first = [1, 2, 3];
var second = [4, 5]; const first = [1, 2, 3];
const second = [4, 5];
nonMutatingConcat(first, second); nonMutatingConcat(first, second);
``` ```
@ -69,11 +70,8 @@ nonMutatingConcat(first, second);
```js ```js
function nonMutatingConcat(original, attach) { function nonMutatingConcat(original, attach) {
// Only change code below this line
return original.concat(attach); return original.concat(attach);
// Only change code above this line
} }
var first = [1, 2, 3]; const first = [1, 2, 3];
var second = [4, 5]; const second = [4, 5];
nonMutatingConcat(first, second);
``` ```

View File

@ -38,17 +38,17 @@ assert(!code.match(/\.?[\s\S]*?map/g));
```js ```js
// The global variable // The global variable
var s = [23, 65, 98, 5]; const s = [23, 65, 98, 5];
Array.prototype.myMap = function(callback) { Array.prototype.myMap = function(callback) {
var newArray = []; const newArray = [];
// Only change code below this line // Only change code below this line
// Only change code above this line // Only change code above this line
return newArray; return newArray;
}; };
var new_s = s.myMap(function(item) { const new_s = s.myMap(function(item) {
return item * 2; return item * 2;
}); });
``` ```
@ -56,20 +56,17 @@ var new_s = s.myMap(function(item) {
# --solutions-- # --solutions--
```js ```js
// the global Array const s = [23, 65, 98, 5];
var s = [23, 65, 98, 5];
Array.prototype.myMap = function(callback) { Array.prototype.myMap = function(callback) {
var newArray = []; const newArray = [];
// Only change code below this line for (const elem of this) {
for (var elem of this) {
newArray.push(callback(elem)); newArray.push(callback(elem));
} }
// Only change code above this line
return newArray; return newArray;
}; };
var new_s = s.myMap(function(item) { const new_s = s.myMap(function(item) {
return item * 2; return item * 2;
}); });
``` ```

View File

@ -34,16 +34,16 @@ assert(!code.match(/\.?[\s\S]*?filter/g));
```js ```js
// The global variable // The global variable
var s = [23, 65, 98, 5]; const s = [23, 65, 98, 5];
Array.prototype.myFilter = function(callback) { Array.prototype.myFilter = function(callback) {
// Only change code below this line // Only change code below this line
var newArray = []; const newArray = [];
// Only change code above this line // Only change code above this line
return newArray; return newArray;
}; };
var new_s = s.myFilter(function(item) { const new_s = s.myFilter(function(item) {
return item % 2 === 1; return item % 2 === 1;
}); });
``` ```
@ -51,20 +51,17 @@ var new_s = s.myFilter(function(item) {
# --solutions-- # --solutions--
```js ```js
// the global Array const s = [23, 65, 98, 5];
var s = [23, 65, 98, 5];
Array.prototype.myFilter = function(callback) { Array.prototype.myFilter = function(callback) {
var newArray = []; const newArray = [];
// Only change code below this line
for (let i = 0; i < this.length; i++) { for (let i = 0; i < this.length; i++) {
if (callback(this[i])) newArray.push(this[i]); if (callback(this[i])) newArray.push(this[i]);
} }
// Only change code above this line
return newArray; return newArray;
}; };
var new_s = s.myFilter(function(item) { const new_s = s.myFilter(function(item) {
return item % 2 === 1; return item % 2 === 1;
}); });
``` ```

View File

@ -35,7 +35,7 @@ curried(1)(2)
柯里化在不能一次爲函數提供所有參數情況下很有用。 因爲它可以將每個函數的調用保存到一個變量中,該變量將保存返回的函數引用,該引用在下一個參數可用時接受該參數。 下面是使用柯里化函數的例子: 柯里化在不能一次爲函數提供所有參數情況下很有用。 因爲它可以將每個函數的調用保存到一個變量中,該變量將保存返回的函數引用,該引用在下一個參數可用時接受該參數。 下面是使用柯里化函數的例子:
```js ```js
var funcForY = curried(1); const funcForY = curried(1);
console.log(funcForY(2)); // 3 console.log(funcForY(2)); // 3
``` ```
@ -45,7 +45,8 @@ console.log(funcForY(2)); // 3
function impartial(x, y, z) { function impartial(x, y, z) {
return x + y + z; return x + y + z;
} }
var partialFn = impartial.bind(this, 1, 2);
const partialFn = impartial.bind(this, 1, 2);
partialFn(10); // 13 partialFn(10); // 13
``` ```
@ -90,6 +91,7 @@ function add(x) {
// Only change code above this line // Only change code above this line
} }
add(10)(20)(30); add(10)(20)(30);
``` ```

View File

@ -53,10 +53,10 @@ assert(__newValue === 5);
```js ```js
// The global variable // The global variable
var fixedValue = 4; let fixedValue = 4;
// Only change code below this line // Only change code below this line
function incrementer () { function incrementer() {
// Only change code above this line // Only change code above this line
@ -66,15 +66,9 @@ function incrementer () {
# --solutions-- # --solutions--
```js ```js
// The global variable let fixedValue = 4;
var fixedValue = 4;
// Only change code below this line function incrementer(fixedValue) {
function incrementer (fixedValue) {
return fixedValue + 1; return fixedValue + 1;
// Only change code above this line
} }
``` ```

View File

@ -11,7 +11,7 @@ dashedName: remove-elements-from-an-array-using-slice-instead-of-splice
使用數組時經常遇到要刪除一些元素並保留數組剩餘部分的情況。 爲此JavaScript 提供了 `splice` 方法,它接收兩個參數:從哪裏開始刪除項目的索引,和要刪除的項目數。 如果沒有提供第二個參數,默認情況下是移除一直到結尾的所有元素。 但 `splice` 方法會改變調用它的原始數組。 舉個例子: 使用數組時經常遇到要刪除一些元素並保留數組剩餘部分的情況。 爲此JavaScript 提供了 `splice` 方法,它接收兩個參數:從哪裏開始刪除項目的索引,和要刪除的項目數。 如果沒有提供第二個參數,默認情況下是移除一直到結尾的所有元素。 但 `splice` 方法會改變調用它的原始數組。 舉個例子:
```js ```js
var cities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"]; const cities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
cities.splice(3, 1); cities.splice(3, 1);
``` ```
@ -69,7 +69,8 @@ function nonMutatingSplice(cities) {
// Only change code above this line // Only change code above this line
} }
var inputCities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
const inputCities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
nonMutatingSplice(inputCities); nonMutatingSplice(inputCities);
``` ```
@ -77,10 +78,7 @@ nonMutatingSplice(inputCities);
```js ```js
function nonMutatingSplice(cities) { function nonMutatingSplice(cities) {
// Only change code below this line
return cities.slice(0,3); return cities.slice(0,3);
// Only change code above this line
} }
var inputCities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"]; const inputCities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
nonMutatingSplice(inputCities);
``` ```

View File

@ -68,24 +68,23 @@ assert(JSON.stringify(nonMutatingSort([140000, 104, 99])) ===
## --seed-contents-- ## --seed-contents--
```js ```js
var globalArray = [5, 6, 3, 2, 9]; const globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) { function nonMutatingSort(arr) {
// Only change code below this line // Only change code below this line
// Only change code above this line // Only change code above this line
} }
nonMutatingSort(globalArray); nonMutatingSort(globalArray);
``` ```
# --solutions-- # --solutions--
```js ```js
var globalArray = [5, 6, 3, 2, 9]; const globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) { function nonMutatingSort(arr) {
// Only change code below this line
return [].concat(arr).sort((a,b) => a-b); return [].concat(arr).sort((a,b) => a-b);
// Only change code above this line
} }
nonMutatingSort(globalArray);
``` ```

View File

@ -13,8 +13,8 @@ dashedName: return-part-of-an-array-using-the-slice-method
舉個例子: 舉個例子:
```js ```js
var arr = ["Cat", "Dog", "Tiger", "Zebra"]; const arr = ["Cat", "Dog", "Tiger", "Zebra"];
var newArray = arr.slice(1, 3); const newArray = arr.slice(1, 3);
``` ```
`newArray` 值爲 `["Dog", "Tiger"]` `newArray` 值爲 `["Dog", "Tiger"]`
@ -78,7 +78,8 @@ function sliceArray(anim, beginSlice, endSlice) {
// Only change code above this line // Only change code above this line
} }
var inputAnim = ["Cat", "Dog", "Tiger", "Zebra", "Ant"];
const inputAnim = ["Cat", "Dog", "Tiger", "Zebra", "Ant"];
sliceArray(inputAnim, 1, 3); sliceArray(inputAnim, 1, 3);
``` ```
@ -86,10 +87,7 @@ sliceArray(inputAnim, 1, 3);
```js ```js
function sliceArray(anim, beginSlice, endSlice) { function sliceArray(anim, beginSlice, endSlice) {
// Only change code below this line return anim.slice(beginSlice, endSlice);
return anim.slice(beginSlice, endSlice)
// Only change code above this line
} }
var inputAnim = ["Cat", "Dog", "Tiger", "Zebra", "Ant"]; const inputAnim = ["Cat", "Dog", "Tiger", "Zebra", "Ant"];
sliceArray(inputAnim, 1, 3);
``` ```

View File

@ -18,6 +18,7 @@ function ascendingOrder(arr) {
return a - b; return a - b;
}); });
} }
ascendingOrder([1, 5, 2, 3, 4]); ascendingOrder([1, 5, 2, 3, 4]);
``` ```
@ -29,6 +30,7 @@ function reverseAlpha(arr) {
return a === b ? 0 : a < b ? 1 : -1; return a === b ? 0 : a < b ? 1 : -1;
}); });
} }
reverseAlpha(['l', 'h', 'z', 'b', 's']); reverseAlpha(['l', 'h', 'z', 'b', 's']);
``` ```
@ -86,6 +88,7 @@ function alphabeticalOrder(arr) {
return arr return arr
// Only change code above this line // Only change code above this line
} }
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]); alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
``` ```
@ -93,9 +96,6 @@ alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
```js ```js
function alphabeticalOrder(arr) { function alphabeticalOrder(arr) {
// Only change code below this line
return arr.sort(); return arr.sort();
// Only change code above this line
} }
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
``` ```

View File

@ -13,11 +13,11 @@ dashedName: split-a-string-into-an-array-using-the-split-method
下面是兩個用空格分隔一個字符串的例子,另一個是用數字的正則表達式分隔: 下面是兩個用空格分隔一個字符串的例子,另一個是用數字的正則表達式分隔:
```js ```js
var str = "Hello World"; const str = "Hello World";
var bySpace = str.split(" "); const bySpace = str.split(" ");
var otherString = "How9are7you2today"; const otherString = "How9are7you2today";
var byDigits = otherString.split(/\d/); const byDigits = otherString.split(/\d/);
``` ```
`bySpace` 將有值 `["Hello", "World"]``byDigits` 將有值 `["How", "are", "you", "today"]` `bySpace` 將有值 `["Hello", "World"]``byDigits` 將有值 `["How", "are", "you", "today"]`
@ -74,6 +74,7 @@ function splitify(str) {
// Only change code above this line // Only change code above this line
} }
splitify("Hello World,I-am code"); splitify("Hello World,I-am code");
``` ```
@ -81,8 +82,6 @@ splitify("Hello World,I-am code");
```js ```js
function splitify(str) { function splitify(str) {
// Only change code below this line
return str.split(/\W/); return str.split(/\W/);
// Only change code above this line
} }
``` ```

View File

@ -59,29 +59,29 @@ assert.deepEqual(finalTabs.tabs, [
```js ```js
// tabs is an array of titles of each site open within the window // tabs is an array of titles of each site open within the window
var Window = function(tabs) { const Window = function(tabs) {
this.tabs = tabs; // We keep a record of the array inside the object this.tabs = tabs; // We keep a record of the array inside the object
}; };
// When you join two windows into one window // When you join two windows into one window
Window.prototype.join = function (otherWindow) { Window.prototype.join = function(otherWindow) {
this.tabs = this.tabs.concat(otherWindow.tabs); this.tabs = this.tabs.concat(otherWindow.tabs);
return this; return this;
}; };
// When you open a new tab at the end // When you open a new tab at the end
Window.prototype.tabOpen = function (tab) { Window.prototype.tabOpen = function(tab) {
this.tabs.push('new tab'); // Let's open a new tab for now this.tabs.push('new tab'); // Let's open a new tab for now
return this; return this;
}; };
// When you close a tab // When you close a tab
Window.prototype.tabClose = function (index) { Window.prototype.tabClose = function(index) {
// Only change code below this line // Only change code below this line
var tabsBeforeIndex = this.tabs.splice(0, index); // Get the tabs before the tab const tabsBeforeIndex = this.tabs.splice(0, index); // Get the tabs before the tab
var tabsAfterIndex = this.tabs.splice(index + 1); // Get the tabs after the tab const tabsAfterIndex = this.tabs.splice(index + 1); // Get the tabs after the tab
this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // Join them together this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // Join them together
@ -91,12 +91,12 @@ Window.prototype.tabClose = function (index) {
}; };
// Let's create three browser windows // Let's create three browser windows
var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // Your mailbox, drive, and other work sites const workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // Your mailbox, drive, and other work sites
var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); // Social sites const socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); // Social sites
var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); // Entertainment sites const videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); // Entertainment sites
// Now perform the tab opening, closing, and other operations // Now perform the tab opening, closing, and other operations
var finalTabs = socialWindow const finalTabs = socialWindow
.tabOpen() // Open a new tab for cat memes .tabOpen() // Open a new tab for cat memes
.join(videoWindow.tabClose(2)) // Close third tab in video window, and join .join(videoWindow.tabClose(2)) // Close third tab in video window, and join
.join(workWindow.tabClose(1).tabOpen()); .join(workWindow.tabClose(1).tabOpen());
@ -106,40 +106,34 @@ console.log(finalTabs.tabs);
# --solutions-- # --solutions--
```js ```js
// tabs is an array of titles of each site open within the window const Window = function(tabs) {
var Window = function(tabs) { this.tabs = tabs;
this.tabs = tabs; // We keep a record of the array inside the object
}; };
// When you join two windows into one window Window.prototype.join = function(otherWindow) {
Window.prototype.join = function (otherWindow) {
this.tabs = this.tabs.concat(otherWindow.tabs); this.tabs = this.tabs.concat(otherWindow.tabs);
return this; return this;
}; };
// When you open a new tab at the end Window.prototype.tabOpen = function(tab) {
Window.prototype.tabOpen = function (tab) { this.tabs.push('new tab');
this.tabs.push('new tab'); // Let's open a new tab for now
return this; return this;
}; };
// When you close a tab Window.prototype.tabClose = function(index) {
Window.prototype.tabClose = function (index) { const tabsBeforeIndex = this.tabs.slice(0, index);
var tabsBeforeIndex = this.tabs.slice(0, index); // Get the tabs before the tab const tabsAfterIndex = this.tabs.slice(index + 1);
var tabsAfterIndex = this.tabs.slice(index + 1); // Get the tabs after the tab
this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // Join them together this.tabs = tabsBeforeIndex.concat(tabsAfterIndex);
return this; return this;
}; };
// Let's create three browser windows const workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']);
var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // Your mailbox, drive, and other work sites const socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']);
var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); // Social sites const videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']);
var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); // Entertainment sites
// Now perform the tab opening, closing, and other operations const finalTabs = socialWindow
var finalTabs = socialWindow .tabOpen()
.tabOpen() // Open a new tab for cat memes .join(videoWindow.tabClose(2))
.join(videoWindow.tabClose(2)) // Close third tab in video window, and join
.join(workWindow.tabClose(1).tabOpen()); .join(workWindow.tabClose(1).tabOpen());
``` ```

View File

@ -13,7 +13,8 @@ dashedName: use-the-every-method-to-check-that-every-element-in-an-array-meets-a
舉個例子,下面的代碼檢測數組 `numbers` 的所有元素是否都小於 10 舉個例子,下面的代碼檢測數組 `numbers` 的所有元素是否都小於 10
```js ```js
var numbers = [1, 5, 8, 0, 10, 11]; const numbers = [1, 5, 8, 0, 10, 11];
numbers.every(function(currentValue) { numbers.every(function(currentValue) {
return currentValue < 10; return currentValue < 10;
}); });
@ -62,6 +63,7 @@ function checkPositive(arr) {
// Only change code above this line // Only change code above this line
} }
checkPositive([1, 2, 3, -4, 5]); checkPositive([1, 2, 3, -4, 5]);
``` ```
@ -69,9 +71,6 @@ checkPositive([1, 2, 3, -4, 5]);
```js ```js
function checkPositive(arr) { function checkPositive(arr) {
// Only change code below this line
return arr.every(num => num > 0); return arr.every(num => num > 0);
// Only change code above this line
} }
checkPositive([1, 2, 3, -4, 5]);
``` ```

View File

@ -93,7 +93,7 @@ assert(getRating(watchList.filter((_, i) => i < 1 || i > 2)) === 8.55);
```js ```js
// The global variable // The global variable
var watchList = [ const watchList = [
{ {
"Title": "Inception", "Title": "Inception",
"Year": "2010", "Year": "2010",
@ -206,22 +206,22 @@ var watchList = [
} }
]; ];
function getRating(watchList){ function getRating(watchList) {
// Only change code below this line // Only change code below this line
var averageRating; let averageRating;
// Only change code above this line // Only change code above this line
return averageRating; return averageRating;
} }
console.log(getRating(watchList)); console.log(getRating(watchList));
``` ```
# --solutions-- # --solutions--
```js ```js
// The global variable const watchList = [
var watchList = [
{ {
"Title": "Inception", "Title": "Inception",
"Year": "2010", "Year": "2010",
@ -334,8 +334,8 @@ var watchList = [
} }
]; ];
function getRating(watchList){ function getRating(watchList) {
var averageRating; let averageRating;
const rating = watchList const rating = watchList
.filter(obj => obj.Director === "Christopher Nolan") .filter(obj => obj.Director === "Christopher Nolan")
.map(obj => Number(obj.imdbRating)); .map(obj => Number(obj.imdbRating));

View File

@ -13,7 +13,8 @@ dashedName: use-the-some-method-to-check-that-any-elements-in-an-array-meet-a-cr
舉個例子,下面的代碼檢測數組`numbers`中是否有元素小於 10 舉個例子,下面的代碼檢測數組`numbers`中是否有元素小於 10
```js ```js
var numbers = [10, 50, 8, 220, 110, 11]; const numbers = [10, 50, 8, 220, 110, 11];
numbers.some(function(currentValue) { numbers.some(function(currentValue) {
return currentValue < 10; return currentValue < 10;
}); });
@ -62,6 +63,7 @@ function checkPositive(arr) {
// Only change code above this line // Only change code above this line
} }
checkPositive([1, 2, 3, -4, 5]); checkPositive([1, 2, 3, -4, 5]);
``` ```
@ -69,9 +71,6 @@ checkPositive([1, 2, 3, -4, 5]);
```js ```js
function checkPositive(arr) { function checkPositive(arr) {
// Only change code below this line
return arr.some(elem => elem > 0); return arr.some(elem => elem > 0);
// Only change code above this line
} }
checkPositive([1, 2, 3, -4, 5]);
``` ```

View File

@ -157,7 +157,7 @@ assert(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']).length === 6);
```js ```js
function diffArray(arr1, arr2) { function diffArray(arr1, arr2) {
var newArr = []; const newArr = [];
return newArr; return newArr;
} }
@ -168,13 +168,12 @@ diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
```js ```js
function diffArray(arr1, arr2) { function diffArray(arr1, arr2) {
var newArr = []; const newArr = [];
var h1 = Object.create(null); const h1 = Object.create(null);
arr1.forEach(function(e) { arr1.forEach(function(e) {
h1[e] = e; h1[e] = e;
}); });
const h2 = Object.create(null);
var h2 = Object.create(null);
arr2.forEach(function(e) { arr2.forEach(function(e) {
h2[e] = e; h2[e] = e;
}); });

View File

@ -148,7 +148,7 @@ if(bob){
## --seed-contents-- ## --seed-contents--
```js ```js
var Person = function(firstAndLast) { const Person = function(firstAndLast) {
// Only change code below this line // Only change code below this line
// Complete the method below and implement the others similarly // Complete the method below and implement the others similarly
this.getFullName = function() { this.getFullName = function() {
@ -157,16 +157,16 @@ var Person = function(firstAndLast) {
return firstAndLast; return firstAndLast;
}; };
var bob = new Person('Bob Ross'); const bob = new Person('Bob Ross');
bob.getFullName(); bob.getFullName();
``` ```
# --solutions-- # --solutions--
```js ```js
var Person = function(firstAndLast) { const Person = function(firstAndLast) {
var firstName, lastName; let firstName, lastName;
function updateName(str) { function updateName(str) {
firstName = str.split(" ")[0]; firstName = str.split(" ")[0];
@ -201,6 +201,6 @@ var Person = function(firstAndLast) {
}; };
}; };
var bob = new Person('Bob Ross'); const bob = new Person('Bob Ross');
bob.getFullName(); bob.getFullName();
``` ```

View File

@ -51,8 +51,8 @@ assert.deepEqual(
```js ```js
function orbitalPeriod(arr) { function orbitalPeriod(arr) {
var GM = 398600.4418; const GM = 398600.4418;
var earthRadius = 6367.4447; const earthRadius = 6367.4447;
return arr; return arr;
} }
@ -63,9 +63,9 @@ orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
```js ```js
function orbitalPeriod(arr) { function orbitalPeriod(arr) {
var GM = 398600.4418; const GM = 398600.4418;
var earthRadius = 6367.4447; const earthRadius = 6367.4447;
var TAU = 2 * Math.PI; const TAU = 2 * Math.PI;
return arr.map(function(obj) { return arr.map(function(obj) {
return { return {
name: obj.name, name: obj.name,
@ -73,6 +73,4 @@ function orbitalPeriod(arr) {
}; };
}); });
} }
orbitalPeriod([{name : "sputkin", avgAlt : 35873.5553}]);
``` ```

View File

@ -61,7 +61,6 @@ function smallestCommons(arr) {
return arr; return arr;
} }
smallestCommons([1,5]); smallestCommons([1,5]);
``` ```

View File

@ -103,7 +103,7 @@ assert.deepEqual(
```js ```js
function whatIsInAName(collection, source) { function whatIsInAName(collection, source) {
var arr = []; const arr = [];
// Only change code below this line // Only change code below this line
@ -118,8 +118,8 @@ whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last:
```js ```js
function whatIsInAName(collection, source) { function whatIsInAName(collection, source) {
var arr = []; const arr = [];
var keys = Object.keys(source); const keys = Object.keys(source);
collection.forEach(function(e) { collection.forEach(function(e) {
if(keys.every(function(key) {return e[key] === source[key];})) { if(keys.every(function(key) {return e[key] === source[key];})) {
arr.push(e); arr.push(e);

View File

@ -51,7 +51,6 @@ assert(
```js ```js
function rot13(str) { function rot13(str) {
return str; return str;
} }

View File

@ -186,7 +186,7 @@ assert.deepEqual(
```js ```js
function checkCashRegister(price, cash, cid) { function checkCashRegister(price, cash, cid) {
var change; let change;
return change; return change;
} }
@ -196,54 +196,57 @@ checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], [
# --solutions-- # --solutions--
```js ```js
var denom = [ const denom = [
{ name: 'ONE HUNDRED', val: 100}, { name: "ONE HUNDRED", val: 100 },
{ name: 'TWENTY', val: 20}, { name: "TWENTY", val: 20 },
{ name: 'TEN', val: 10}, { name: "TEN", val: 10 },
{ name: 'FIVE', val: 5}, { name: "FIVE", val: 5 },
{ name: 'ONE', val: 1}, { name: "ONE", val: 1 },
{ name: 'QUARTER', val: 0.25}, { name: "QUARTER", val: 0.25 },
{ name: 'DIME', val: 0.1}, { name: "DIME", val: 0.1 },
{ name: 'NICKEL', val: 0.05}, { name: "NICKEL", val: 0.05 },
{ name: 'PENNY', val: 0.01} { name: "PENNY", val: 0.01 },
]; ];
function checkCashRegister(price, cash, cid) { function checkCashRegister(price, cash, cid) {
var output = {status: null, change: []}; const output = { status: null, change: [] };
var change = cash - price; let change = cash - price;
var register = cid.reduce(function(acc, curr) { const register = cid.reduce(
acc.total += curr[1]; function (acc, curr) {
acc[curr[0]] = curr[1]; acc.total += curr[1];
return acc; acc[curr[0]] = curr[1];
}, {total: 0}); return acc;
if(register.total === change) { },
output.status = 'CLOSED'; { total: 0 }
output.change = cid; );
return output; if (register.total === change) {
} output.status = "CLOSED";
if(register.total < change) { output.change = cid;
output.status = 'INSUFFICIENT_FUNDS'; return output;
return output; }
} if (register.total < change) {
var change_arr = denom.reduce(function(acc, curr) { output.status = "INSUFFICIENT_FUNDS";
var value = 0; return output;
while(register[curr.name] > 0 && change >= curr.val) { }
change -= curr.val; const change_arr = denom.reduce(function (acc, curr) {
register[curr.name] -= curr.val; let value = 0;
value += curr.val; while (register[curr.name] > 0 && change >= curr.val) {
change = Math.round(change * 100) / 100; change -= curr.val;
} register[curr.name] -= curr.val;
if(value > 0) { value += curr.val;
acc.push([ curr.name, value ]); change = Math.round(change * 100) / 100;
} }
return acc; if (value > 0) {
}, []); acc.push([curr.name, value]);
if(change_arr.length < 1 || change > 0) { }
output.status = 'INSUFFICIENT_FUNDS'; return acc;
return output; }, []);
} if (change_arr.length < 1 || change > 0) {
output.status = 'OPEN'; output.status = "INSUFFICIENT_FUNDS";
output.change = change_arr; return output;
return output; }
output.status = "OPEN";
output.change = change_arr;
return output;
} }
``` ```

View File

@ -107,8 +107,6 @@ function palindrome(str) {
return true; return true;
} }
palindrome("eye"); palindrome("eye");
``` ```

View File

@ -18,9 +18,9 @@ dashedName: access-array-data-with-indexes
**示例** **示例**
```js ```js
var array = [50,60,70]; const array = [50, 60, 70];
array[0]; array[0];
var data = array[1]; const data = array[1];
``` ```
现在 `array[0]` 的值是 `50` `data` 的值为 `60`. 现在 `array[0]` 的值是 `50` `data` 的值为 `60`.
@ -76,7 +76,7 @@ if(typeof myArray !== "undefined" && typeof myData !== "undefined"){(function(y,
## --seed-contents-- ## --seed-contents--
```js ```js
var myArray = [50,60,70]; const myArray = [50, 60, 70];
``` ```
@ -84,6 +84,6 @@ var myArray = [50,60,70];
# --solutions-- # --solutions--
```js ```js
var myArray = [50,60,70]; const myArray = [50, 60, 70];
var myData = myArray[0]; const myData = myArray[0];
``` ```

View File

@ -14,12 +14,13 @@ dashedName: access-multi-dimensional-arrays-with-indexes
**例如:** **例如:**
```js ```js
var arr = [ const arr = [
[1,2,3], [1, 2, 3],
[4,5,6], [4, 5, 6],
[7,8,9], [7, 8, 9],
[[10,11,12], 13, 14] [[10, 11, 12], 13, 14]
]; ];
arr[3]; arr[3];
arr[3][0]; arr[3][0];
arr[3][0][1]; arr[3][0][1];
@ -58,14 +59,19 @@ if(typeof myArray !== "undefined"){(function(){return "myData: " + myData + " my
## --seed-contents-- ## --seed-contents--
```js ```js
var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]]; const myArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[[10, 11, 12], 13, 14],
];
var myData = myArray[0][0]; const myData = myArray[0][0];
``` ```
# --solutions-- # --solutions--
```js ```js
var myArray = [[1,2,3],[4,5,6], [7,8,9], [[10,11,12], 13, 14]]; const myArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [[10, 11, 12], 13, 14]];
var myData = myArray[2][1]; const myData = myArray[2][1];
``` ```

View File

@ -14,7 +14,7 @@ dashedName: accessing-nested-arrays
下面是访问嵌套数组的例子: 下面是访问嵌套数组的例子:
```js ```js
var ourPets = [ const ourPets = [
{ {
animalType: "cat", animalType: "cat",
names: [ names: [
@ -32,6 +32,7 @@ var ourPets = [
] ]
} }
]; ];
ourPets[0].names[1]; ourPets[0].names[1];
ourPets[1].names[0]; ourPets[1].names[0];
``` ```
@ -72,7 +73,7 @@ assert(/=\s*myPlants\[1\].list\[1\]/.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var myPlants = [ const myPlants = [
{ {
type: "flowers", type: "flowers",
list: [ list: [
@ -91,13 +92,13 @@ var myPlants = [
} }
]; ];
var secondTree = ""; const secondTree = "";
``` ```
# --solutions-- # --solutions--
```js ```js
var myPlants = [ const myPlants = [
{ {
type: "flowers", type: "flowers",
list: [ list: [
@ -116,5 +117,5 @@ var myPlants = [
} }
]; ];
var secondTree = myPlants[1].list[1]; const secondTree = myPlants[1].list[1];
``` ```

View File

@ -14,7 +14,7 @@ dashedName: accessing-nested-objects
这是一个嵌套对象: 这是一个嵌套对象:
```js ```js
var ourStorage = { const ourStorage = {
"desk": { "desk": {
"drawer": "stapler" "drawer": "stapler"
}, },
@ -26,6 +26,7 @@ var ourStorage = {
"bottom drawer": "soda" "bottom drawer": "soda"
} }
}; };
ourStorage.cabinet["top drawer"].folder2; ourStorage.cabinet["top drawer"].folder2;
ourStorage.desk.drawer; ourStorage.desk.drawer;
``` ```
@ -66,7 +67,7 @@ assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var myStorage = { const myStorage = {
"car": { "car": {
"inside": { "inside": {
"glove box": "maps", "glove box": "maps",
@ -78,13 +79,13 @@ var myStorage = {
} }
}; };
var gloveBoxContents = undefined; const gloveBoxContents = undefined;
``` ```
# --solutions-- # --solutions--
```js ```js
var myStorage = { const myStorage = {
"car":{ "car":{
"inside":{ "inside":{
"glove box":"maps", "glove box":"maps",
@ -95,5 +96,5 @@ var myStorage = {
} }
} }
}; };
var gloveBoxContents = myStorage.car.inside["glove box"]; const gloveBoxContents = myStorage.car.inside["glove box"];
``` ```

View File

@ -16,11 +16,12 @@ dashedName: accessing-object-properties-with-bracket-notation
这是一个使用方括号表示法读取对象属性的例子: 这是一个使用方括号表示法读取对象属性的例子:
```js ```js
var myObj = { const myObj = {
"Space Name": "Kirk", "Space Name": "Kirk",
"More Space": "Spock", "More Space": "Spock",
"NoSpace": "USS Enterprise" "NoSpace": "USS Enterprise"
}; };
myObj["Space Name"]; myObj["Space Name"];
myObj['More Space']; myObj['More Space'];
myObj["NoSpace"]; myObj["NoSpace"];
@ -78,26 +79,25 @@ assert(code.match(/testObj\s*?\[('|")[^'"]+\1\]/g).length > 1);
```js ```js
// Setup // Setup
var testObj = { const testObj = {
"an entree": "hamburger", "an entree": "hamburger",
"my side": "veggies", "my side": "veggies",
"the drink": "water" "the drink": "water"
}; };
// Only change code below this line // Only change code below this line
const entreeValue = testObj; // Change this line
var entreeValue = testObj; // Change this line const drinkValue = testObj; // Change this line
var drinkValue = testObj; // Change this line
``` ```
# --solutions-- # --solutions--
```js ```js
var testObj = { const testObj = {
"an entree": "hamburger", "an entree": "hamburger",
"my side": "veggies", "my side": "veggies",
"the drink": "water" "the drink": "water"
}; };
var entreeValue = testObj["an entree"]; const entreeValue = testObj["an entree"];
var drinkValue = testObj['the drink']; const drinkValue = testObj['the drink'];
``` ```

View File

@ -16,15 +16,17 @@ dashedName: accessing-object-properties-with-dot-notation
这里是一个用点符号(`.`)读取对象属性的示例: 这里是一个用点符号(`.`)读取对象属性的示例:
```js ```js
var myObj = { const myObj = {
prop1: "val1", prop1: "val1",
prop2: "val2" prop2: "val2"
}; };
var prop1val = myObj.prop1;
var prop2val = myObj.prop2; const prop1val = myObj.prop1;
const prop2val = myObj.prop2;
``` ```
`prop1val` 的值将为字符串 `val1`,并且`prop2val` 的值将为字符串 `val2` `prop1val` 的值将为字符串 `val1`,并且`prop2val` 的值将为字符串 `val2`
# --instructions-- # --instructions--
使用点号读取 `testObj` 的属性值。 将变量 `hatValue` 的值设置为该对象的 `hat` 属性的值,并将变量 `shirtValue` 的值设置为该对象的 `shirt` 属性的值。 使用点号读取 `testObj` 的属性值。 将变量 `hatValue` 的值设置为该对象的 `hat` 属性的值,并将变量 `shirtValue` 的值设置为该对象的 `shirt` 属性的值。
@ -73,27 +75,26 @@ assert(code.match(/testObj\.\w+/g).length > 1);
```js ```js
// Setup // Setup
var testObj = { const testObj = {
"hat": "ballcap", "hat": "ballcap",
"shirt": "jersey", "shirt": "jersey",
"shoes": "cleats" "shoes": "cleats"
}; };
// Only change code below this line // Only change code below this line
const hatValue = testObj; // Change this line
var hatValue = testObj; // Change this line const shirtValue = testObj; // Change this line
var shirtValue = testObj; // Change this line
``` ```
# --solutions-- # --solutions--
```js ```js
var testObj = { const testObj = {
"hat": "ballcap", "hat": "ballcap",
"shirt": "jersey", "shirt": "jersey",
"shoes": "cleats" "shoes": "cleats"
}; };
var hatValue = testObj.hat; const hatValue = testObj.hat;
var shirtValue = testObj.shirt; const shirtValue = testObj.shirt;
``` ```

View File

@ -14,11 +14,14 @@ dashedName: accessing-object-properties-with-variables
以下是一个使用变量来访问属性的例子: 以下是一个使用变量来访问属性的例子:
```js ```js
var dogs = { const dogs = {
Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle" Fido: "Mutt",
Hunter: "Doberman",
Snoopie: "Beagle"
}; };
var myDog = "Hunter";
var myBreed = dogs[myDog]; const myDog = "Hunter";
const myBreed = dogs[myDog];
console.log(myBreed); console.log(myBreed);
``` ```
@ -27,14 +30,16 @@ console.log(myBreed);
使用这一概念的另一种情况是:属性的名字是在程序运行期间动态收集得到的。如下所示: 使用这一概念的另一种情况是:属性的名字是在程序运行期间动态收集得到的。如下所示:
```js ```js
var someObj = { const someObj = {
propName: "John" propName: "John"
}; };
function propPrefix(str) { function propPrefix(str) {
var s = "prop"; const s = "prop";
return s + str; return s + str;
} }
var someProp = propPrefix("Name");
const someProp = propPrefix("Name");
console.log(someObj[someProp]); console.log(someObj[someProp]);
``` ```
@ -96,26 +101,25 @@ if(typeof player !== "undefined"){(function(v){return v;})(player);}
```js ```js
// Setup // Setup
var testObj = { const testObj = {
12: "Namath", 12: "Namath",
16: "Montana", 16: "Montana",
19: "Unitas" 19: "Unitas"
}; };
// Only change code below this line // Only change code below this line
const playerNumber = 42; // Change this line
var playerNumber; // Change this line const player = testObj; // Change this line
var player = testObj; // Change this line
``` ```
# --solutions-- # --solutions--
```js ```js
var testObj = { const testObj = {
12: "Namath", 12: "Namath",
16: "Montana", 16: "Montana",
19: "Unitas" 19: "Unitas"
}; };
var playerNumber = 16; const playerNumber = 16;
var player = testObj[playerNumber]; const player = testObj[playerNumber];
``` ```

View File

@ -28,7 +28,7 @@ ourDog["bark"] = "bow-wow";
例如: 例如:
```js ```js
var ourDog = { const ourDog = {
"name": "Camper", "name": "Camper",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,
@ -67,7 +67,7 @@ assert(!/bark[^\n]:/.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var myDog = { const myDog = {
"name": "Happy Coder", "name": "Happy Coder",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,
@ -80,7 +80,7 @@ var myDog = {
# --solutions-- # --solutions--
```js ```js
var myDog = { const myDog = {
"name": "Happy Coder", "name": "Happy Coder",
"legs": 4, "legs": 4,
"tails": 1, "tails": 1,

View File

@ -18,7 +18,7 @@ JavaScript 中,我们通过符号 `+` 来进行加法运算。
**代码示例:** **代码示例:**
```js ```js
myVar = 5 + 10; const myVar = 5 + 10;
``` ```
现在,变量 `myVar` 的值为 `15` 现在,变量 `myVar` 的值为 `15`
@ -52,11 +52,11 @@ assert(/\+/.test(code));
## --seed-contents-- ## --seed-contents--
```js ```js
var sum = 10 + 0; const sum = 10 + 0;
``` ```
# --solutions-- # --solutions--
```js ```js
var sum = 10 + 10; const sum = 10 + 10;
``` ```

View File

@ -92,7 +92,7 @@ assert(code.match(/break/g).length > 2);
```js ```js
function switchOfStuff(val) { function switchOfStuff(val) {
var answer = ""; let answer = "";
// Only change code below this line // Only change code below this line
@ -108,7 +108,7 @@ switchOfStuff(1);
```js ```js
function switchOfStuff(val) { function switchOfStuff(val) {
var answer = ""; let answer = "";
switch(val) { switch(val) {
case "a": case "a":

View File

@ -14,8 +14,8 @@ dashedName: appending-variables-to-strings
示例: 示例:
```js ```js
var anAdjective = "awesome!"; const anAdjective = "awesome!";
var ourStr = "freeCodeCamp is "; let ourStr = "freeCodeCamp is ";
ourStr += anAdjective; ourStr += anAdjective;
``` ```
@ -64,15 +64,14 @@ assert(code.match(/myStr\s*\+=\s*someAdjective\s*/).length > 0);
```js ```js
// Change code below this line // Change code below this line
const someAdjective = "";
var someAdjective; let myStr = "Learning to code is ";
var myStr = "Learning to code is ";
``` ```
# --solutions-- # --solutions--
```js ```js
var someAdjective = "neat"; const someAdjective = "neat";
var myStr = "Learning to code is "; let myStr = "Learning to code is ";
myStr += someAdjective; myStr += someAdjective;
``` ```

Some files were not shown because too many files have changed in this diff Show More