diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/access-array-data-with-indexes.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/access-array-data-with-indexes.chinese.md index 337de7e8c9..51e4edf21a 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/access-array-data-with-indexes.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/access-array-data-with-indexes.chinese.md @@ -2,24 +2,41 @@ id: 56bbb991ad1ed5201cd392ca title: Access Array Data with Indexes challengeType: 1 -videoUrl: '' -localeTitle: 使用索引访问数组数据 +videoUrl: 'https://scrimba.com/c/cBZQbTz' +forumTopicId: 16158 +localeTitle: 通过索引访问数组中的数据 --- ## Description -
我们可以使用indexes访问数组内部的数据。数组索引使用字符串使用的相同括号表示法编写,但不是指定字符,而是指定数组中的条目。与字符串一样,数组使用从零开始的索引,因此数组中的第一个元素是元素0
var array = [50,60,70];
阵列[0]; //等于50
var data = array [1]; //等于60
注意
数组名称和方括号之间不应有任何空格,如array [0] 。尽管JavaScript能够正确处理,但这可能会让其他程序员在阅读代码时感到困惑。
+
+我们可以使用索引 indexes 来访问数组中的数据。 + +数组索引与字符串索引一样使用中括号,但字符串索引得到的是一个字符,而数组索引得到的是一个元素。数组索引与字符串索引一样是从 0 开始的,所以数组中第一个元素的索引编号是 0。 +
+示例 + +```js +var array = [50,60,70]; +array[0]; // equals 50 +var data = array[1]; // equals 60 +``` + +提示
数组名称和方括号之间不应有任何空格,如array [0]尽管 JavaScript 能够正确处理,但可能会让看你代码的其他程序员感到困惑 +
## Instructions -
创建一个名为myData的变量,并使用括号表示法将其设置为等于myArray的第一个值。
+
+创建一个名为myData的变量,并把myArray的第一个索引上的值赋给它。 +
## Tests
```yml tests: - - text: 变量myData应该等于myArray的第一个值。 + - text: 变量myData的值应该等于myArray的第一个值。 testString: assert((function(){if(typeof myArray !== 'undefined' && typeof myData !== 'undefined' && myArray[0] === myData){return true;}else{return false;}})()); - - text: 应使用括号表示法访问变量myArray的数据。 + - text: 应使用方括号访问变量myArray中的数据。 testString: assert((function(){if(code.match(/\s*=\s*myArray\[0\]/g)){return true;}else{return false;}})()); ``` @@ -50,7 +67,7 @@ var myArray = [50,60,70];
```js -console.info('after the test'); +if(typeof myArray !== "undefined" && typeof myData !== "undefined"){(function(y,z){return 'myArray = ' + JSON.stringify(y) + ', myData = ' + JSON.stringify(z);})(myArray, myData);} ```
@@ -60,7 +77,10 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myArray = [50,60,70]; +var myData = myArray[0]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/access-multi-dimensional-arrays-with-indexes.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/access-multi-dimensional-arrays-with-indexes.chinese.md index d14bf1b8a9..8019cf438a 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/access-multi-dimensional-arrays-with-indexes.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/access-multi-dimensional-arrays-with-indexes.chinese.md @@ -2,25 +2,45 @@ id: 56592a60ddddeae28f7aa8e1 title: Access Multi-Dimensional Arrays With Indexes challengeType: 1 -videoUrl: '' -localeTitle: 访问带索引的多维数组 +videoUrl: 'https://scrimba.com/c/ckND4Cq' +forumTopicId: 16159 +localeTitle: 使用索引访问多维数组 --- ## Description -
考虑多维数组的一种方法是作为数组的数组 。使用括号访问数组时,第一组括号引用最外层(第一级)数组中的条目,另外一对括号引用内部的下一级条目。
var arr = [
[1,2,3],
[4,5,6]
[7,8,9]
[[10,11,12],13,14]
]。
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能够正确处理,但这可能会让其他程序员在阅读代码时感到困惑。
+
+可以把 多维 数组看作成是一个 数组中的数组。当使用方括号去访问数组的时候,第一个[index]访问的是第 N 个子数组,第二个[index]访问的是第 N 个子数组的第N个元素。 +示例 + +```js +var arr = [ + [1,2,3], + [4,5,6], + [7,8,9], + [[10,11,12], 13, 14] +]; +arr[3]; // equals [[10,11,12], 13, 14] +arr[3][0]; // equals [10,11,12] +arr[3][0][1]; // equals 11 +``` + +提示
数组名称和方括号之间不应该有任何空格,如array [0][0],甚至array [0] [0],都是不正确的。尽管 JavaScript 能够处理,但可能会让看你代码的其他程序员感到困惑。 +
## Instructions -
使用括号表示法从myArray选择一个元素,使myData等于8
+
+使用索引从myArray选择一个元素,使得myData的值为8。 +
## Tests
```yml tests: - - text: myData应该等于8 。 + - text: myData应该等于8。 testString: assert(myData === 8); - - text: 您应该使用括号表示法从myArray读取正确的值。 - testString: assert(/myData=myArray\[2\]\[1\]/.test(code.replace(/\s/g, ''))); + - text: 你应该使用方括号从myArray中取值。 + testString: 'assert(/myArray\[2\]\[1\]/g.test(code) && !/myData\s*=\s*(?:.*[-+*/%]|\d)/g.test(code));' ``` @@ -47,7 +67,7 @@ var myData = myArray[0][0];
```js -console.info('after the test'); +if(typeof myArray !== "undefined"){(function(){return "myData: " + myData + " myArray: " + JSON.stringify(myArray);})();} ```
@@ -57,7 +77,10 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myArray = [[1,2,3],[4,5,6], [7,8,9], [[10,11,12], 13, 14]]; +var myData = myArray[2][1]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-arrays.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-arrays.chinese.md index 76086061e0..d27bf1d8ba 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-arrays.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-arrays.chinese.md @@ -2,24 +2,54 @@ id: 56533eb9ac21ba0edf2244cd title: Accessing Nested Arrays challengeType: 1 -videoUrl: '' +videoUrl: 'https://scrimba.com/c/cLeGDtZ' +forumTopicId: 16160 localeTitle: 访问嵌套数组 --- ## Description -
正如我们在前面的示例中所看到的,对象可以包含嵌套对象和嵌套数组。与访问嵌套对象类似,可以链接数组括号表示法来访问嵌套数组。以下是如何访问嵌套数组的示例:
var ourPets = [
{
animalType:“猫”,
名称:[
“Meowzer”
“蓬松”,
“洁猫”
]
},
{
动物类型:“狗”,
名称:[
“点”,
“库巴”
“羊羊”
]
}
]。
ourPets [0] .names [1]; //“蓬松”
ourPets [1] .names [0]; //“Spot”
+
+正如我们在前面的例子所见,对象可以嵌套对象和数组。与访问嵌套对象一样,用中括号操作符同样可以访问嵌套数组。 +下面是如何访问嵌套数组的例子: + +```js +var ourPets = [ + { + animalType: "cat", + names: [ + "Meowzer", + "Fluffy", + "Kit-Cat" + ] + }, + { + animalType: "dog", + names: [ + "Spot", + "Bowser", + "Frankie" + ] + } +]; +ourPets[0].names[1]; // "Fluffy" +ourPets[1].names[0]; // "Spot" +``` + +
## Instructions -
使用对象点和数组括号表示法从变量myPlants检索第二个树。
+
+使用点操作符和中括号操作符来检索变量myPlants的第二棵树。 +
## Tests
```yml tests: - - text: secondTree应该等于“松树” + - text: secondTree应该等于 "pine"。 testString: assert(secondTree === "pine"); - - text: 使用点和括号表示法访问myPlants + - text: 使用点操作符和中括号操作符来检索变量myPlants。 testString: assert(/=\s*myPlants\[1\].list\[1\]/.test(code)); ``` @@ -65,7 +95,12 @@ var secondTree = ""; // Change this line
```js -console.info('after the test'); +(function(x) { + if(typeof x != 'undefined') { + return "secondTree = " + x; + } + return "secondTree is undefined"; +})(secondTree); ```
@@ -75,7 +110,30 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myPlants = [ + { + type: "flowers", + list: [ + "rose", + "tulip", + "dandelion" + ] + }, + { + type: "trees", + list: [ + "fir", + "pine", + "birch" + ] + } +]; + +// Only change code below this line + +var secondTree = myPlants[1].list[1]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-objects.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-objects.chinese.md index 420f93bc68..437c4b07b3 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-objects.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-objects.chinese.md @@ -2,24 +2,48 @@ id: 56533eb9ac21ba0edf2244cc title: Accessing Nested Objects challengeType: 1 -videoUrl: '' +videoUrl: 'https://scrimba.com/c/cRnRnfa' +forumTopicId: 16161 localeTitle: 访问嵌套对象 --- ## Description -
可以通过将点或括号表示法链接在一起来访问对象的子属性。这是一个嵌套对象:
var ourStorage = {
“桌子”:{
“抽屉”:“订书机”
},
“内阁”:{
“顶级抽屉”:{
“folder1”:“一个文件”,
“folder2”:“秘密”
},
“底部抽屉”:“苏打水”
}
};
ourStorage.cabinet [“top drawer”]。folder2; //“秘密”
ourStorage.desk.drawer; //“订书机”
+
+通过串联起来的点操作符或中括号操作符来访问对象的嵌套属性。 +下面是一个嵌套的对象: + +```js +var ourStorage = { + "desk": { + "drawer": "stapler" + }, + "cabinet": { + "top drawer": { + "folder1": "a file", + "folder2": "secrets" + }, + "bottom drawer": "soda" + } +}; +ourStorage.cabinet["top drawer"].folder2; // "secrets" +ourStorage.desk.drawer; // "stapler" +``` + +
## Instructions -
访问myStorage对象并将glove box属性的内容分配给gloveBoxContents变量。对于名称中包含空格的属性,请使用括号表示法。
+
+读取myStorage对象,将glove box属性的内容赋值给变量gloveBoxContents。在适用的地方使用点操作符来访问属性,否则使用中括号操作符。 +
## Tests
```yml tests: - - text: gloveBoxContents应该等于“地图” + - text: gloveBoxContents应该等于"maps"。 testString: assert(gloveBoxContents === "maps"); - - text: 使用点和括号表示法访问myStorage + - text: 应使用点操作符和中括号操作符来访问myStorage。 testString: assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code)); ``` @@ -56,7 +80,12 @@ var gloveBoxContents = undefined; // Change this line
```js -console.info('after the test'); +(function(x) { + if(typeof x != 'undefined') { + return "gloveBoxContents = " + x; + } + return "gloveBoxContents is undefined"; +})(gloveBoxContents); ```
@@ -66,7 +95,20 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myStorage = { + "car":{ + "inside":{ + "glove box":"maps", + "passenger seat":"crumbs" + }, + "outside":{ + "trunk":"jack" + } + } +}; +var gloveBoxContents = myStorage.car.inside["glove box"]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-bracket-notation.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-bracket-notation.chinese.md index cfdffa6002..67de0a43a6 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-bracket-notation.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-bracket-notation.chinese.md @@ -2,30 +2,50 @@ id: 56533eb9ac21ba0edf2244c8 title: Accessing Object Properties with Bracket Notation challengeType: 1 -videoUrl: '' -localeTitle: 使用括号表示法访问对象属性 +videoUrl: 'https://scrimba.com/c/cBvmEHP' +forumTopicId: 16163 +localeTitle: 通过方括号访问对象属性 --- ## Description -
访问对象属性的第二种方法是括号表示法( [] )。如果您尝试访问的对象的属性在其名称中有空格,则需要使用括号表示法。但是,您仍然可以在没有空格的对象属性上使用括号表示法。以下是使用括号表示法读取对象属性的示例:
var myObj = {
“太空名称”:“柯克”,
“更多空间”:“Spock”​​,
“NoSpace”:“USS Enterprise”
};
myObj [“空间名称”]; //柯克
myObj ['更多空间']; // Spock
MyObj中[ “无空间”]; // USS Enterprise
请注意,其中包含空格的属性名称必须使用引号(单引号或双引号)。
+
+第二种访问对象的方式就是中括号操作符([]),如果你想访问的属性的名称有一个空格,这时你只能使用中括号操作符([])。 +当然,如果属性名不包含空格,也可以使用中括号操作符。 +这是一个使用中括号操作符([])读取对象属性的例子: + +```js +var myObj = { + "Space Name": "Kirk", + "More Space": "Spock", + "NoSpace": "USS Enterprise" +}; +myObj["Space Name"]; // Kirk +myObj['More Space']; // Spock +myObj["NoSpace"]; // USS Enterprise +``` + +提示:属性名称中如果有空格,必须把属性名称用单引号或双引号包裹起来。 +
## Instructions -
使用括号表示法testObj属性"an entree" testObj "an entree""the drink"testObj ,并分别将它们分配给entreeValuedrinkValue
+
+用中括号操作符读取对象testObjan entree属性值和the drink属性值,并分别赋值给entreeValuedrinkValue。 +
## Tests
```yml tests: - - text: entreeValue应该是一个字符串 + - text: entreeValue应该是一个字符串。 testString: assert(typeof entreeValue === 'string' ); - - text: entreeValue的值应该是"hamburger" + - text: entreeValue的值应该是"hamburger"。 testString: assert(entreeValue === 'hamburger' ); - - text: drinkValue应该是一个字符串 + - text: drinkValue应该是一个字符串。 testString: assert(typeof drinkValue === 'string' ); - - text: drinkValue的值应该是"water" + - text: drinkValue的值应该是"water"。 testString: assert(drinkValue === 'water' ); - - text: 您应该使用括号表示法两次 + - text: 你应该使用中括号两次。 testString: assert(code.match(/testObj\s*?\[('|")[^'"]+\1\]/g).length > 1); ``` @@ -49,7 +69,6 @@ var testObj = { var entreeValue = testObj; // Change this line var drinkValue = testObj; // Change this line - ``` @@ -59,7 +78,7 @@ var drinkValue = testObj; // Change this line
```js -console.info('after the test'); +(function(a,b) { return "entreeValue = '" + a + "', drinkValue = '" + b + "'"; })(entreeValue,drinkValue); ```
@@ -69,7 +88,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var testObj = { + "an entree": "hamburger", + "my side": "veggies", + "the drink": "water" +}; +var entreeValue = testObj["an entree"]; +var drinkValue = testObj['the drink']; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-dot-notation.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-dot-notation.chinese.md index 742d0dbf36..4e121ea808 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-dot-notation.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-dot-notation.chinese.md @@ -2,30 +2,47 @@ id: 56533eb9ac21ba0edf2244c7 title: Accessing Object Properties with Dot Notation challengeType: 1 -videoUrl: '' -localeTitle: 使用点表示法访问对象属性 +videoUrl: 'https://scrimba.com/c/cGryJs8' +forumTopicId: 16164 +localeTitle: 通过点符号访问对象属性 --- ## Description -
有两种方法可以访问对象的属性:点表示法( . )和括号表示法( [] ),类似于数组。当您知道要提前访问的属性的名称时,使用点符号。以下是使用点表示法( . )读取对象属性的示例:
var myObj = {
prop1:“val1”,
prop2:“val2”
};
var prop1val = myObj.prop1; // val1
var prop2val = myObj.prop2; // val2
+
+有两种方式访问对象属性,一个是点操作符(.),一个是中括号操作符([])。 +当你知道所要读取的属性的名称的时候,使用点操作符。 +这是一个使用点操作符读取对象属性的例子: + +```js +var myObj = { + prop1: "val1", + prop2: "val2" +}; +var prop1val = myObj.prop1; // val1 +var prop2val = myObj.prop2; // val2 +``` + +
## Instructions -
使用点表示法读入testObj的属性值。将变量hatValue设置为等于对象的属性hat ,并将变量shirtValue设置为等于对象的属性shirt
+
+通过点操作符读取对象testObj,把hat的属性值赋给变量hatValue,把shirt的属性值赋给shirtValue。 +
## Tests
```yml tests: - - text: hatValue应该是一个字符串 + - text: hatValue应该是一个字符串。 testString: assert(typeof hatValue === 'string' ); - - text: hatValue的值应该是"ballcap" + - text: hatValue的值应该是"ballcap"。 testString: assert(hatValue === 'ballcap' ); - - text: shirtValue应该是一个字符串 + - text: shirtValue应该是一个字符串。 testString: assert(typeof shirtValue === 'string' ); - - text: shirtValue的值应该是"jersey" + - text: shirtValue的值应该是"jersey"。 testString: assert(shirtValue === 'jersey' ); - - text: 你应该使用点符号两次 + - text: 你应该使用点操作符两次。 testString: assert(code.match(/testObj\.\w+/g).length > 1); ``` @@ -49,7 +66,6 @@ var testObj = { var hatValue = testObj; // Change this line var shirtValue = testObj; // Change this line - ``` @@ -59,7 +75,7 @@ var shirtValue = testObj; // Change this line
```js -console.info('after the test'); +(function(a,b) { return "hatValue = '" + a + "', shirtValue = '" + b + "'"; })(hatValue,shirtValue); ```
@@ -69,7 +85,16 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var testObj = { + "hat": "ballcap", + "shirt": "jersey", + "shoes": "cleats" +}; + +var hatValue = testObj.hat; +var shirtValue = testObj.shirt; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables.chinese.md index 317b232ab7..c5015cffbc 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables.chinese.md @@ -2,32 +2,63 @@ id: 56533eb9ac21ba0edf2244c9 title: Accessing Object Properties with Variables challengeType: 1 -videoUrl: '' -localeTitle: 使用变量访问对象属性 +videoUrl: 'https://scrimba.com/c/cnQyKur' +forumTopicId: 16165 +localeTitle: 通过变量访问对象属性 --- ## Description -
对象的括号表示法的另一个用途是访问存储为变量值的属性。这对于迭代对象的属性或访问查找表非常有用。以下是使用变量访问属性的示例:
var dogs = {
Fido:“Mutt”,Hunter:“Doberman”,Snoopie:“Beagle”
};
var myDog =“猎人”;
var myBreed = dogs [myDog];
的console.log(myBreed); //“杜宾犬”
另一种可以使用此概念的方法是在程序执行期间动态收集属性的名称,如下所示:
var someObj = {
propName:“约翰”
};
function propPrefix(str){
var s =“prop”;
return s + str;
}
var someProp = propPrefix(“Name”); // someProp现在保存值'propName'
的console.log(someObj中[someProp]); // “约翰”
请注意,在使用变量名来访问属性时,我们不会使用引号,因为我们使用的是变量的 ,而不是名称
+
+中括号操作符的另一个使用方式是访问赋值给变量的属性。当你需要遍历对象的属性列表或访问查找表(lookup tables)时,这种方式极为有用。 +这有一个使用变量来访问属性的例子: + +```js +var dogs = { + Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle" +}; +var myDog = "Hunter"; +var myBreed = dogs[myDog]; +console.log(myBreed); // "Doberman" +``` + +使用此概念的另一种方法是在程序执行期间动态收集属性名称,如下所示: + +```js +var someObj = { + propName: "John" +}; +function propPrefix(str) { + var s = "prop"; + return s + str; +} +var someProp = propPrefix("Name"); // someProp now holds the value 'propName' +console.log(someObj[someProp]); // "John" +``` + +提示:当我们通过变量名访问属性的时候,不需要给变量名包裹引号。因为实际上我们使用的是变量的值,而不是变量的名称。 +
## Instructions -
使用playerNumber变量使用括号表示法在testObj查找玩家16 。然后将该名称分配给player变量。
+
+使用变量playerNumber,通过中括号操作符找到testObjplayerNumber16的值。然后把名字赋给变量player。 +
## Tests
```yml tests: - - text: playerNumber应该是一个数字 + - text: playerNumber应该是一个数字。 testString: assert(typeof playerNumber === 'number'); - - text: 变量player应该是一个字符串 + - text: 变量player应该是一个字符串。 testString: assert(typeof player === 'string'); - - text: player的价值应该是“蒙大拿” + - text: player点值应该是 "Montana"。 testString: assert(player === 'Montana'); - - text: 您应该使用括号表示法来访问testObj + - text: 你应该使用中括号访问testObj。 testString: assert(/testObj\s*?\[.*?\]/.test(code)); - - text: 您不应该直接将值Montana分配给变量player 。 + - text: 你不应该直接将Montana赋给player。 testString: assert(!code.match(/player\s*=\s*"|\'\s*Montana\s*"|\'\s*;/gi)); - - text: 您应该在括号表示法中使用变量playerNumber + - text: 你应该在中括号中使用playerNumber变量。 testString: assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code)); ``` @@ -51,7 +82,6 @@ var testObj = { var playerNumber; // Change this Line var player = testObj; // Change this Line - ``` @@ -61,7 +91,7 @@ var player = testObj; // Change this Line
```js -console.info('after the test'); +if(typeof player !== "undefined"){(function(v){return v;})(player);} ```
@@ -71,7 +101,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var testObj = { + 12: "Namath", + 16: "Montana", + 19: "Unitas" +}; +var playerNumber = 16; +var player = testObj[playerNumber]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/add-new-properties-to-a-javascript-object.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/add-new-properties-to-a-javascript-object.chinese.md index dca2e8a27e..1ad3d776f2 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/add-new-properties-to-a-javascript-object.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/add-new-properties-to-a-javascript-object.chinese.md @@ -2,25 +2,35 @@ id: 56bbb991ad1ed5201cd392d2 title: Add New Properties to a JavaScript Object challengeType: 1 -videoUrl: '' -localeTitle: 将新属性添加到JavaScript对象 +videoUrl: 'https://scrimba.com/c/cQe38UD' +forumTopicId: 301169 +localeTitle: 给对象添加新属性 --- ## Description -
您可以像修改现有JavaScript对象一样向现有JavaScript对象添加新属性。以下是我们如何为ourDog添加"bark"属性: ourDog.bark = "bow-wow";或者我们的ourDog["bark"] = "bow-wow";现在当我们评估我们的ourDog.bark ,我们会得到他的吠声,“低头哇”。
+
+你也可以像更改属性一样给对象添加属性。 +看看我们是如何给ourDog添加"bark"属性: +ourDog.bark = "bow-wow"; +或者 +ourDog["bark"] = "bow-wow"; +现在当我们访问ourDog.bark时会得到 ourDog 的 bark 值 "bow-wow". +
## Instructions -
myDog添加"bark"属性并将其设置为狗声,例如“woof”。您可以使用点或括号表示法。
+
+给myDog添加一个"bark"属性,设置它的值为狗的声音,例如:"woof"。你可以使用点或中括号操作符。 +
## Tests
```yml tests: - - text: 将属性"bark"添加到myDog 。 + - text: 给myDog添加"bark"属性。 testString: assert(myDog.bark !== undefined); - - text: 不要在设置部分添加"bark" - testString: assert(!/bark[^\n]:/.test(code)); + - text: 不能在初始化 myDog 的时候添加"bark"属性。 + testString: 'assert(!/bark[^\n]:/.test(code));' ``` @@ -61,7 +71,7 @@ var myDog = {
```js -console.info('after the test'); +(function(z){return z;})(myDog); ```
@@ -71,7 +81,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myDog = { + "name": "Happy Coder", + "legs": 4, + "tails": 1, + "friends": ["freeCodeCamp Campers"] +}; +myDog.bark = "Woof Woof"; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/add-two-numbers-with-javascript.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/add-two-numbers-with-javascript.chinese.md index 6621a1f358..0294bcc914 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/add-two-numbers-with-javascript.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/add-two-numbers-with-javascript.chinese.md @@ -2,24 +2,37 @@ id: cf1111c1c11feddfaeb3bdef title: Add Two Numbers with JavaScript challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript添加两个数字 +videoUrl: 'https://scrimba.com/c/cM2KBAG' +forumTopicId: 16650 +localeTitle: 加法运算 --- ## Description -
Number是JavaScript中的数据类型,表示数字数据。现在让我们尝试使用JavaScript添加两个数字。当放置在两个数字之间时,JavaScript使用+符号作为加法运算。
myVar = 5 + 10; //分配15
+
+Number是 JavaScript 中的一种数据类型,表示数值。 +现在让我们来尝试在 JavaScript 中做加法运算。 +JavaScript 中使用+号进行加法运算。 +示例 + +```js +myVar = 5 + 10; // assigned 15 +``` + +
## Instructions -
更改0使总和等于20
+
+改变数字0让变量 sum 的值为20。 +
## Tests
```yml tests: - - text: sum应该等于20 + - text: sum应该等于20。 testString: assert(sum === 20); - - text: 使用+运算符 + - text: 要使用+运算符。 testString: assert(/\+/.test(code)); ``` @@ -43,7 +56,7 @@ var sum = 10 + 0;
```js -console.info('after the test'); +(function(z){return 'sum = '+z;})(sum); ```
@@ -53,7 +66,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var sum = 10 + 10; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/adding-a-default-option-in-switch-statements.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/adding-a-default-option-in-switch-statements.chinese.md index 5ff806a4b1..ac362f0da1 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/adding-a-default-option-in-switch-statements.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/adding-a-default-option-in-switch-statements.chinese.md @@ -2,36 +2,58 @@ id: 56533eb9ac21ba0edf2244de title: Adding a Default Option in Switch Statements challengeType: 1 -videoUrl: '' -localeTitle: 在交换机语句中添加默认选项 +videoUrl: 'https://scrimba.com/c/c3JvVfg' +forumTopicId: 16653 +localeTitle: 在 Switch 语句中添加默认选项 --- ## Description -
switch语句中,您可能无法将所有可能的值指定为case语句。相反,您可以添加default语句,如果找不到匹配的case语句,将执行该语句。可以把它想象成if/else链中的最后一个else语句。 default语句应该是最后一种情况。
switch(num){
案例值1:
语句1;
打破;
案例值2:
语句2;
打破;
...
默认:
defaultStatement;
打破;
}
+
+在switch语句中你可能无法用 case 来指定所有情况,这时你可以添加 default 语句。当再也找不到 case 匹配的时候 default 语句会执行,非常类似于 if/else 组合中的 else 语句。 +default语句应该是最后一个 case。 + +```js +switch (num) { + case value1: + statement1; + break; + case value2: + statement2; + break; +... + default: + defaultStatement; + break; +} +``` + +
## Instructions -
写一个switch语句来设置以下条件的answer
"a" - “苹果”
"b" - “鸟”
"c" - “猫”
default - “东西”
+
+写一个 switch 语句,根据下面的条件来设置answer的switch语句:
"a" - "apple"
"b" - "bird"
"c" - "cat"
default - "stuff" +
## Tests
```yml tests: - - text: switchOfStuff("a")的值应为“apple” + - text: switchOfStuff("a")应该有一个值为 "apple"。 testString: assert(switchOfStuff("a") === "apple"); - - text: switchOfStuff("b")的值应为“bird” + - text: switchOfStuff("b")应该有一个值为 "bird"。 testString: assert(switchOfStuff("b") === "bird"); - - text: switchOfStuff("c")的值应为“cat” + - text: switchOfStuff("c")应该有一个值为 "cat"。 testString: assert(switchOfStuff("c") === "cat"); - - text: switchOfStuff("d")的值应为“stuff” + - text: switchOfStuff("d")应该有一个值为 "stuff"。 testString: assert(switchOfStuff("d") === "stuff"); - - text: switchOfStuff(4)的值应为“stuff” + - text: switchOfStuff(4)应该有一个值为 "stuff"。 testString: assert(switchOfStuff(4) === "stuff"); - - text: 您不应该使用任何ifelse语句 + - text: 不能使用任何ifelse表达式。 testString: assert(!/else/g.test(code) || !/if/g.test(code)); - - text: 您应该使用default语句 + - text: 你应该有一个default表达式。 testString: assert(switchOfStuff("string-to-trigger-default-case") === "stuff"); - - text: 你应该至少有3个break语句 + - text: 你应该有至少 3 个break表达式。 testString: assert(code.match(/break/g).length > 2); ``` @@ -68,7 +90,26 @@ switchOfStuff(1); ## Solution
+ ```js -// solution required +function switchOfStuff(val) { + var answer = ""; + + switch(val) { + case "a": + answer = "apple"; + break; + case "b": + answer = "bird"; + break; + case "c": + answer = "cat"; + break; + default: + answer = "stuff"; + } + return answer; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/appending-variables-to-strings.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/appending-variables-to-strings.chinese.md index 439c271ffe..8d3f657012 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/appending-variables-to-strings.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/appending-variables-to-strings.chinese.md @@ -2,24 +2,29 @@ id: 56533eb9ac21ba0edf2244ed title: Appending Variables to Strings challengeType: 1 -videoUrl: '' +videoUrl: 'https://scrimba.com/c/cbQmZfa' +forumTopicId: 16656 localeTitle: 将变量附加到字符串 --- ## Description -
正如我们可以在字符串文字中构建多行的字符串一样,我们也可以使用加号等于( += )运算符将变量附加到字符串。
+
+我们不仅可以创建出多行的字符串,还可以使用加等号(+=)运算符来将变量追加到字符串。 +
## Instructions -
设置someAdjective并使用+=运算符将其附加到myStr
+
+设置变量someAdjective的值,并使用+=运算符把它追加到变量myStr上。 +
## Tests
```yml tests: - - text: someAdjective应设置为至少3个字符长的字符串 + - text: someAdjective应该是一个至少包含三个字符的字符串。 testString: assert(typeof someAdjective !== 'undefined' && someAdjective.length > 2); - - text: 使用+=运算符将someAdjective附加到myStr + - text: 使用+=操作符把someAdjective追加到myStr的后面。 testString: assert(code.match(/myStr\s*\+=\s*someAdjective\s*/).length > 0); ``` @@ -51,7 +56,20 @@ var myStr = "Learning to code is ";
```js -console.info('after the test'); +(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'); +})(); ```
@@ -61,7 +79,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var anAdjective = "awesome!"; +var ourStr = "freeCodeCamp is "; +ourStr += anAdjective; + +var someAdjective = "neat"; +var myStr = "Learning to code is "; +myStr += someAdjective; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/assignment-with-a-returned-value.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/assignment-with-a-returned-value.chinese.md index f57e04d560..a75b666ba9 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/assignment-with-a-returned-value.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/assignment-with-a-returned-value.chinese.md @@ -2,25 +2,33 @@ id: 56533eb9ac21ba0edf2244c3 title: Assignment with a Returned Value challengeType: 1 -videoUrl: '' -localeTitle: 具有返回值的分配 +videoUrl: 'https://scrimba.com/c/ce2pEtB' +forumTopicId: 16658 +localeTitle: 用返回值来赋值 --- ## Description -
如果您从我们对使用赋值运算符存储值的讨论中回忆起来,则在分配值之前,将解决等号右侧的所有内容。这意味着我们可以获取函数的返回值并将其赋值给变量。假设我们预先定义了一个函数sum ,它将两个数字相加,然后: ourSum = sum(5, 12);将调用sum函数,它返回值17并将其分配给ourSum变量。
+
+如果你还记得我们在这一节使用赋值运算符存储值的讨论,赋值之前,先完成等号右边的操作。这意味着我们可把一个函数的返回值,赋值给一个变量。 +假设我们预先定义的函数sum其功能就是将两个数字相加,那么: +ourSum = sum(5, 12); +将调用sum函数,返回return了一个数值17,然后把它赋值给了ourSum变量。 +
## Instructions -
使用参数7调用processArg函数,并将其返回值分配给已processed的变量。
+
+调用processArg函数并给参数一个值7,然后把返回的值赋值给变量processed。 +
## Tests
```yml tests: - - text: processed的值应为2 + - text: processed的值应该是2。 testString: assert(processed === 2); - - text: 您应该将processArg分配给已processed - testString: assert(/processed\s*=\s*processArg\(\s*7\s*\)/.test(code)); + - text: 你应该把processArg的返回值赋给processed。 + testString: assert(/processed\s*=\s*processArg\(\s*7\s*\)\s*;/.test(code)); ``` @@ -50,6 +58,7 @@ function processArg(num) { // Only change code below this line + ``` @@ -59,7 +68,7 @@ function processArg(num) {
```js -console.info('after the test'); +(function(){return "processed = " + processed})(); ```
@@ -69,7 +78,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var processed = 0; + +function processArg(num) { + return (num + 3) / 5; +} + +processed = processArg(7); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/build-javascript-objects.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/build-javascript-objects.chinese.md index 611698c5e7..60357eba5c 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/build-javascript-objects.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/build-javascript-objects.chinese.md @@ -2,30 +2,60 @@ id: 56bbb991ad1ed5201cd392d0 title: Build JavaScript Objects challengeType: 1 -videoUrl: '' -localeTitle: 构建JavaScript对象 +videoUrl: 'https://scrimba.com/c/cWGkbtd' +forumTopicId: 16769 +localeTitle: 新建 JavaScript 对象 --- ## Description -
您之前可能听说过object这个术语。对象类似于arrays ,除了不使用索引访问和修改数据,您可以通过所谓的properties访问对象中的数据。对象对于以结构化方式存储数据很有用,并且可以表示真实世界对象,如猫。这是一个示例cat对象:
var cat = {
“名字”:“胡须”,
“腿”:4,
“尾巴”:1,
“敌人”:[“水”,“狗”]
};
在此示例中,所有属性都存储为字符串,例如 - "name""legs""tails" 。但是,您也可以使用数字作为属性。您甚至可以省略单字符串属性的引号,如下所示:
var anotherObject = {
制作:“福特”,
5:“五”,
“模特”:“焦点”
};
但是,如果您的对象具有任何非字符串属性,JavaScript将自动将它们作为字符串进行类型转换。
+
+你之前可能听说过对象object。 +对象和数组很相似,数组是通过索引来访问和修改数据,而对象是通过属性来访问和修改数据。 +对象适合用来存储结构化数据,就和真实世界的对象一模一样,比如一只猫。 +这是一个对象的示例: + +```js +var cat = { + "name": "Whiskers", + "legs": 4, + "tails": 1, + "enemies": ["Water", "Dogs"] +}; +``` + +在这个示例中所有的属性以字符串的形式储存,例如,"name""legs""tails"。但是,你也可以使用数字作为属性,你甚至可以省略字符串属性的引号,如下所示: + +```js +var anotherObject = { + make: "Ford", + 5: "five", + "model": "focus" +}; +``` + +但是,如果你的对象具有任何非字符串属性,JavaScript 将自动将它们转换为字符串类型。 +
## Instructions -
创建一个代表名为myDog的狗的对象,其中包含属性"name" (字符串), "legs""tails""friends" 。您可以将这些对象属性设置为您想要的任何值,因为"name"是一个字符串, "legs""tails"是数字, "friends"是一个数组。
+
+创建一个叫做myDog的对象,它里面有这些属性:"name""legs""tails""friends"。 +你可以设置对象属性为任何值,只需要确保"name"是字符串,"legs""tails"是数字,"friends"是数组。 +
## Tests
```yml tests: - - text: myDog应该包含属性name ,它应该是一个string 。 + - text: myDog应该包含name属性,并且是一个字符串string。 testString: assert((function(z){if(z.hasOwnProperty("name") && z.name !== undefined && typeof z.name === "string"){return true;}else{return false;}})(myDog)); - - text: myDog应该包含属性legs ,它应该是一个number 。 + - text: myDog应该包含legs属性,并且是一个数字number。 testString: assert((function(z){if(z.hasOwnProperty("legs") && z.legs !== undefined && typeof z.legs === "number"){return true;}else{return false;}})(myDog)); - - text: myDog应该包含属性tails ,它应该是一个number 。 + - text: myDog应该包含tails属性,并且是一个数字number。 testString: assert((function(z){if(z.hasOwnProperty("tails") && z.tails !== undefined && typeof z.tails === "number"){return true;}else{return false;}})(myDog)); - - text: myDog应该包含属性friends ,它应该是一个array 。 + - text: myDog应该包含friends属性,并且是一个数组array。 testString: assert((function(z){if(z.hasOwnProperty("friends") && z.friends !== undefined && Array.isArray(z.friends)){return true;}else{return false;}})(myDog)); - - text: myDog应该只包含所有给定的属性。 + - text: myDog应该只包含给出的属性。 testString: assert((function(z){return Object.keys(z).length === 4;})(myDog)); ``` @@ -54,7 +84,6 @@ var myDog = { }; - ``` @@ -64,7 +93,7 @@ var myDog = {
```js -console.info('after the test'); +(function(z){return z;})(myDog); ```
@@ -74,7 +103,14 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myDog = { + "name": "Camper", + "legs": 4, + "tails": 1, + "friends": ["everything!"] +}; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/chaining-if-else-statements.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/chaining-if-else-statements.chinese.md index 69e57fa550..9229845141 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/chaining-if-else-statements.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/chaining-if-else-statements.chinese.md @@ -2,46 +2,66 @@ id: 56533eb9ac21ba0edf2244dc title: Chaining If Else Statements challengeType: 1 -videoUrl: '' -localeTitle: 链接如果其他声明 +videoUrl: 'https://scrimba.com/c/caeJgsw' +forumTopicId: 16772 +localeTitle: 多个 if else 语句 --- ## Description -
if/else语句可以链接在一起以用于复杂的逻辑。这是多个链式if / else if语句的伪代码
if( condition1 ){
语句1
} else if( condition2 ){
语句2
} else if( condition3 ){
声明3
。 。 。
} else {
statementN
}
+
+if/else语句串联在一起可以实现复杂的逻辑,这是多个if/else if语句串联在一起的伪代码: + +```js +if (condition1) { + statement1 +} else if (condition2) { + statement2 +} else if (condition3) { + statement3 +. . . +} else { + statementN +} +``` + +
## Instructions -
写入链接if / else if语句以满足以下条件: num < 5 - return“Tiny”
num < 10 - 返回“Small”
num < 15 - 返回“中”
num < 20 - 返回“Large”
num >= 20 - 返回“巨大”
+
+把if/else if语句串联起来实现下面的逻辑: +num < 5- return "Tiny"
num < 10- return "Small"
num < 15- return "Medium"
num < 20- return "Large"
num >= 20 - return "Huge" +
## Tests
```yml tests: - - text: 你应该至少有四个else语句 + - text: 你应该有至少 4 个else表达式。 testString: assert(code.match(/else/g).length > 3); - - text: 你应该至少有四个if语句 + - text: 你应该有至少 4 个if表达式。 testString: assert(code.match(/if/g).length > 3); - - text: 你应该至少有一个return语句 + - text: 你应该有至少 1 个return表达式。 testString: assert(code.match(/return/g).length >= 1); - - text: testSize(0)应该返回“Tiny” + - text: testSize(0)应该返回 "Tiny"。 testString: assert(testSize(0) === "Tiny"); - - text: testSize(4)应该返回“Tiny” + - text: testSize(4)应该返回 "Tiny"。 testString: assert(testSize(4) === "Tiny"); - - text: testSize(5)应返回“Small” + - text: testSize(5)应该返回 "Small"。 testString: assert(testSize(5) === "Small"); - - text: testSize(8)应该返回“Small” + - text: testSize(8)应该返回 "Small"。 testString: assert(testSize(8) === "Small"); - - text: testSize(10)应该返回“Medium” + - text: testSize(10)应该返回 "Medium"。 testString: assert(testSize(10) === "Medium"); - - text: testSize(14)应返回“Medium” + - text: testSize(14)应该返回 "Medium"。 testString: assert(testSize(14) === "Medium"); - - text: testSize(15)应该返回“Large” + - text: testSize(15)应该返回 "Large"。 testString: assert(testSize(15) === "Large"); - - text: testSize(17)应该返回“Large” + - text: testSize(17)应该返回 "Large"。 testString: assert(testSize(17) === "Large"); - - text: testSize(20)应该返回“巨大” + - text: testSize(20)应该返回 "Huge"。 testString: assert(testSize(20) === "Huge"); - - text: testSize(25)应该返回“巨大” + - text: testSize(25)应该返回 "Huge"。 testString: assert(testSize(25) === "Huge"); ``` @@ -64,7 +84,6 @@ function testSize(num) { // Change this value to test testSize(7); - ``` @@ -76,7 +95,21 @@ testSize(7); ## Solution
+ ```js -// solution required +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"; + } +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comment-your-javascript-code.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comment-your-javascript-code.chinese.md index fb517a918b..4c78b94a88 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comment-your-javascript-code.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comment-your-javascript-code.chinese.md @@ -2,24 +2,44 @@ id: bd7123c9c441eddfaeb4bdef title: Comment Your JavaScript Code challengeType: 1 -videoUrl: '' -localeTitle: 评论您的JavaScript代码 +videoUrl: 'https://scrimba.com/c/c7ynnTp' +forumTopicId: 16783 +localeTitle: 给代码添加注释 --- ## Description -
注释是JavaScript有意忽略的代码行。注释是一种很好的方式,可以将注释留给自己和其他人,这些人稍后需要弄清楚代码的作用。在JavaScript中编写注释有两种方法:使用//将告诉JavaScript忽略当前行上的其余文本:
//这是一个内嵌评论。
您可以使用/*开头并以*/结尾的多行注释:
/* 这是一个
多行评论* /
最佳实践
在编写代码时,应定期添加注释以阐明代码部分的功能。良好的评论可以帮助传达您的代码的意图 - 包括他人未来的自我。
+
+被注释的代码块在 JavaScript 之中是不会执行的。在代码中写注释是一个非常好的方式让你自己和其他人理解代码。 +JavaScript 中的注释方式有以下两种: +使用//注释掉当前行的代码 + +```js +// This is an in-line comment. +``` + +你也可以使用多行注释来注释你的代码,以/*开始,用*/来结束,就像下面这样: + +```js +/* This is a +multi-line comment */ +``` + +最佳实践
写代码的时候,要定期添加注释对部分代码块进行解释。适当的注释能让别人和你自己更容易看懂代码。 +
## Instructions -
尝试创建每种评论类型之一。
+
+尝试创建这两种类型的注释。 +
## Tests
```yml tests: - - text: 创建一个包含至少五个字母的//样式注释。 + - text: 创建一个//样式的注释, 被注释的文本至少要包含 5 个字符。 testString: assert(code.match(/(\/\/)...../g)); - - text: 创建包含至少五个字母的/* */样式注释。 + - text: 创建一个/* */样式的注释, 被注释的文本至少要包含 5 个字符。 testString: assert(code.match(/(\/\*)([^\/]{5,})(?=\*\/)/gm)); ``` @@ -44,7 +64,10 @@ tests: ## Solution
+ ```js -// solution required +// Fake Comment +/* Another Comment */ ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-equality-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-equality-operator.chinese.md index df1f2b3dd8..34854f4c96 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-equality-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-equality-operator.chinese.md @@ -2,28 +2,54 @@ id: 56533eb9ac21ba0edf2244d0 title: Comparison with the Equality Operator challengeType: 1 -videoUrl: '' -localeTitle: 与平等算子的比较 +videoUrl: 'https://scrimba.com/c/cKyVMAL' +forumTopicId: 16784 +localeTitle: 相等运算符 --- ## Description -
有很多比较运算符在JavaScript中。所有这些运算符都返回布尔值truefalse值。最基本的运算符是等于运算符== 。等于运算符比较两个值,如果它们是等价的则返回true否则返回false 。请注意,相等性与赋值( = )不同,后者将运算符右侧的值分配给左侧的变量。
function equalityTest(myVal){
if(myVal == 10){
返回“平等”;
}
返回“不等于”;
}
如果myVal等于10 ,则等于运算符返回true ,因此大括号中的代码将执行,函数将返回"Equal" 。否则,该函数将返回"Not Equal" 。为了使JavaScript能够比较两种不同的data types (例如, numbersstrings ),它必须将一种类型转换为另一种类型。这被称为“类型强制”。但是,一旦它完成,它可以比较如下术语:
1 == 1 //是的
1 == 2 //假
1 =='1'//是的
“3”== 3 //是的
+
+在 JavaScript 中,有很多相互比较的操作。所有这些操作符都返回一个truefalse值。 +最基本的运算符是相等运算符:==。相等运算符比较两个值,如果它们是同等,返回true,如果它们不等,返回false。值得注意的是相等运算符不同于赋值运算符(=),赋值运算符是把等号右边的值赋给左边的变量。 + +```js +function equalityTest(myVal) { + if (myVal == 10) { + return "Equal"; + } + return "Not Equal"; +} +``` + +如果myVal等于10,相等运算符会返回true,因此大括号里面的代码会被执行,函数将返回"Equal"。否则,函数返回"Not Equal"。 +在 JavaScript 中,为了让两个不同的数据类型(例如数字字符串)的值可以作比较,它必须把一种类型转换为另一种类型。然而一旦这样做,它可以像下面这样来比较: + +```js +1 == 1 // true +1 == 2 // false +1 == '1' // true +"3" == 3 // true +``` + +
## Instructions -
equality operator添加到指示的行,以便当val等于12时,函数将返回“Equal”
+
+把相等运算符添加到指定的行,这样当val的值为12的时候,函数会返回"Equal"。 +
## Tests
```yml tests: - - text: testEqual(10)应该返回“Not Equal” + - text: testEqual(10)应该返回 "Not Equal"。 testString: assert(testEqual(10) === "Not Equal"); - - text: testEqual(12)应返回“Equal” + - text: testEqual(12)应该返回 "Equal"。 testString: assert(testEqual(12) === "Equal"); - - text: testEqual("12")应返回“Equal” + - text: testEqual("12")应该返回 "Equal"。 testString: assert(testEqual("12") === "Equal"); - - text: 您应该使用==运算符 + - text: 你应该使用==运算符。 testString: assert(code.match(/==/g) && !code.match(/===/g)); ``` @@ -46,7 +72,6 @@ function testEqual(val) { // Change this value to test testEqual(10); - ``` @@ -58,7 +83,14 @@ testEqual(10); ## Solution
+ ```js -// solution required +function testEqual(val) { + if (val == 12) { + return "Equal"; + } + return "Not Equal"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-greater-than-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-greater-than-operator.chinese.md index a6b60075fc..a41ee3ddf6 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-greater-than-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-greater-than-operator.chinese.md @@ -2,36 +2,51 @@ id: 56533eb9ac21ba0edf2244d4 title: Comparison with the Greater Than Operator challengeType: 1 -videoUrl: '' -localeTitle: 与大于运营商的比较 +videoUrl: 'https://scrimba.com/c/cp6GbH4' +forumTopicId: 16786 +localeTitle: 大于运算符 --- ## Description -
大于运算符( > )比较两个数字的值。如果左边的数字大于右边的数字,则返回true 。否则,它返回false 。与等于运算符一样,大于运算符将在比较时转换数据类型的值。 例子
5> 3 //是的
7>'3'//是的
2> 3 //假
'1'> 9 //假
+
+使用大于运算符(>)来比较两个数字。如果大于运算符左边的数字大于右边的数字,将会返回true。否则,它返回false。 +与相等运算符一样,大于运算符在比较的时候,会转换值的数据类型。 +例如 + +```js +5 > 3 // true +7 > '3' // true +2 > 3 // false +'1' > 9 // false +``` + +
## Instructions -
greater than运算符添加到指示的行,以便返回语句有意义。
+
+添加大于运算符到指定的行,使得返回的语句是有意义的。 +
## Tests
```yml tests: - - text: testGreaterThan(0)应返回“10或Under” + - text: testGreaterThan(0)应该返回 "10 or Under"。 testString: assert(testGreaterThan(0) === "10 or Under"); - - text: testGreaterThan(10)应返回“10或Under” + - text: testGreaterThan(10)应该返回 "10 or Under"。 testString: assert(testGreaterThan(10) === "10 or Under"); - - text: testGreaterThan(11)应该返回“Over 10” + - text: testGreaterThan(11)应该返回 "Over 10"。 testString: assert(testGreaterThan(11) === "Over 10"); - - text: testGreaterThan(99)应该返回“Over 10” + - text: testGreaterThan(99)应该返回 "Over 10"。 testString: assert(testGreaterThan(99) === "Over 10"); - - text: testGreaterThan(100)应该返回“Over 10” + - text: testGreaterThan(100)应该返回 "Over 10"。 testString: assert(testGreaterThan(100) === "Over 10"); - - text: testGreaterThan(101)应返回“超过100” + - text: testGreaterThan(101)应该返回 "Over 100"。 testString: assert(testGreaterThan(101) === "Over 100"); - - text: testGreaterThan(150)应该返回“超过100” + - text: testGreaterThan(150)应该返回 "Over 100"。 testString: assert(testGreaterThan(150) === "Over 100"); - - text: 您应该至少使用>运算符两次 + - text: 你应该使用>运算符至少两次。 testString: assert(code.match(/val\s*>\s*('|")*\d+('|")*/g).length > 1); ``` @@ -58,7 +73,6 @@ function testGreaterThan(val) { // Change this value to test testGreaterThan(10); - ``` @@ -70,7 +84,17 @@ testGreaterThan(10); ## Solution
+ ```js -// solution required +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"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-greater-than-or-equal-to-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-greater-than-or-equal-to-operator.chinese.md index bdd2d09621..a30ec93f2d 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-greater-than-or-equal-to-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-greater-than-or-equal-to-operator.chinese.md @@ -2,36 +2,51 @@ id: 56533eb9ac21ba0edf2244d5 title: Comparison with the Greater Than Or Equal To Operator challengeType: 1 -videoUrl: '' -localeTitle: 与大于或等于运算符的比较 +videoUrl: 'https://scrimba.com/c/c6KBqtV' +forumTopicId: 16785 +localeTitle: 大于或等于运算符 --- ## Description -
greater than or equal to运算符( >= )比较两个数字的值。如果左边的数字大于或等于右边的数字,则返回true 。否则,它返回false 。与等于运算符一样, greater than or equal to运算符将在比较时转换数据类型。 例子
6> = 6 //是的
7> ='3'//是的
2> = 3 //假
'7'> = 9 //假
+
+使用大于等于运算符(>=)来比较两个数字的大小。如果大于等于运算符左边的数字比右边的数字大或者相等,它会返回true。否则,它会返回false。 +与相等运算符相似,大于等于运算符在比较的时候会转换值的数据类型。 +例如 + +```js +6 >= 6 // true +7 >= '3' // true +2 >= 3 // false +'7' >= 9 // false +``` + +
## Instructions -
greater than or equal to运算符添加到指示的行,以便返回语句有意义。
+
+添加大于等于运算符到指定行,使得函数的返回语句有意义。 +
## Tests
```yml tests: - - text: testGreaterOrEqual(0)应返回“小于10” + - text: testGreaterOrEqual(0)应该返回 "Less than 10"。 testString: assert(testGreaterOrEqual(0) === "Less than 10"); - - text: testGreaterOrEqual(9)应返回“小于10” + - text: testGreaterOrEqual(9)应该返回 "Less than 10"。 testString: assert(testGreaterOrEqual(9) === "Less than 10"); - - text: testGreaterOrEqual(10)应返回“10或Over” + - text: testGreaterOrEqual(10)应该返回 "10 or Over"。 testString: assert(testGreaterOrEqual(10) === "10 or Over"); - - text: testGreaterOrEqual(11)应返回“10或Over” + - text: testGreaterOrEqual(11)应该返回 "10 or Over"。 testString: assert(testGreaterOrEqual(11) === "10 or Over"); - - text: testGreaterOrEqual(19)应返回“10或Over” + - text: testGreaterOrEqual(19)应该返回 "10 or Over"。 testString: assert(testGreaterOrEqual(19) === "10 or Over"); - - text: testGreaterOrEqual(100)应该返回“20或Over” + - text: testGreaterOrEqual(100)应该返回 "20 or Over"。 testString: assert(testGreaterOrEqual(100) === "20 or Over"); - - text: testGreaterOrEqual(21)应返回“20或Over” + - text: testGreaterOrEqual(21)应该返回 "20 or Over"。 testString: assert(testGreaterOrEqual(21) === "20 or Over"); - - text: 您应该使用>=运算符至少两次 + - text: 你应该使用>=运算符至少两次。 testString: assert(code.match(/val\s*>=\s*('|")*\d+('|")*/g).length > 1); ``` @@ -58,7 +73,6 @@ function testGreaterOrEqual(val) { // Change this value to test testGreaterOrEqual(10); - ``` @@ -70,7 +84,19 @@ testGreaterOrEqual(10); ## Solution
+ ```js -// solution required +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"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-inequality-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-inequality-operator.chinese.md index be1b45629a..6352980df9 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-inequality-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-inequality-operator.chinese.md @@ -2,32 +2,47 @@ id: 56533eb9ac21ba0edf2244d2 title: Comparison with the Inequality Operator challengeType: 1 -videoUrl: '' -localeTitle: 与不等式算子的比较 +videoUrl: 'https://scrimba.com/c/cdBm9Sr' +forumTopicId: 16787 +localeTitle: 不等运算符 --- ## Description -
不等运算符( != )与等于运算符相反。它意味着“不等于”并返回false ,其中相等性将返回true反之亦然 。与等式运算符一样,不等式运算符将在比较时转换数据类型的值。 例子
1!= 2 //是的
1!=“1”//假
1!='1'//假
1!= true // false
0!= false // false
+
+不相等运算符(!=)与相等运算符是相反的。这意味着不相等运算符中,如果“不为真”并且返回false的地方,在相等运算符中会返回true反之亦然。与相等运算符类似,不相等运算符在比较的时候也会转换值的数据类型。 +例如 + +```js +1 != 2 // true +1 != "1" // false +1 != '1' // false +1 != true // false +0 != false // false +``` + +
## Instructions -
if语句中添加不等式运算符!= ,以便当val不等于99时函数将返回“Not Equal”
+
+在if语句中,添加不相等运算符!=,这样函数在当val不等于 99的时候,会返回 "Not Equal"。 +
## Tests
```yml tests: - - text: testNotEqual(99)应返回“Equal” + - text: testNotEqual(99)应该返回 "Equal"。 testString: assert(testNotEqual(99) === "Equal"); - - text: testNotEqual("99")应该返回“Equal” + - text: testNotEqual("99")应该返回 "Equal"。 testString: assert(testNotEqual("99") === "Equal"); - - text: testNotEqual(12)应该返回“Not Equal” + - text: testNotEqual(12)应该返回 "Not Equal"。 testString: assert(testNotEqual(12) === "Not Equal"); - - text: testNotEqual("12")应该返回“Not Equal” + - text: testNotEqual("12")应该返回 "Not Equal"。 testString: assert(testNotEqual("12") === "Not Equal"); - - text: testNotEqual("bob")应返回“Not Equal” + - text: testNotEqual("bob")应该返回 "Not Equal"。 testString: assert(testNotEqual("bob") === "Not Equal"); - - text: 你应该使用!=运算符 + - text: 你应该使用!=运算符。 testString: assert(code.match(/(?!!==)!=/)); ``` @@ -50,7 +65,6 @@ function testNotEqual(val) { // Change this value to test testNotEqual(10); - ``` @@ -62,7 +76,14 @@ testNotEqual(10); ## Solution
+ ```js -// solution required +function testNotEqual(val) { + if (val != 99) { + return "Not Equal"; + } + return "Equal"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-less-than-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-less-than-operator.chinese.md index ffe4352d1d..deff4b740d 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-less-than-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-less-than-operator.chinese.md @@ -2,34 +2,49 @@ id: 56533eb9ac21ba0edf2244d6 title: Comparison with the Less Than Operator challengeType: 1 -videoUrl: '' -localeTitle: 与小于算子的比较 +videoUrl: 'https://scrimba.com/c/cNVRWtB' +forumTopicId: 16789 +localeTitle: 小于运算符 --- ## Description -
小于运算符( < )比较两个数字的值。如果左边的数字小于右边的数字,则返回true 。否则,它返回false 。与等于运算符一样, 少于运算符在比较时转换数据类型。 例子
2 <5 //是的
'3'<7 //是的
5 <5 //假
3 <2 //假
'8'<4 //假
+
+使用小于运算符(<)比较两个数字的大小。如果小于运算符左边的数字比右边的数字小,它会返回true。否则会返回false。与相等运算符类似,小于 运算符在做比较的时候会转换值的数据类型。 +例如 + +```js +2 < 5 // true +'3' < 7 // true +5 < 5 // false +3 < 2 // false +'8' < 4 // false +``` + +
## Instructions -
less than运算符添加到指示的行,以便返回语句有意义。
+
+添加小于运算符到指定行,使得函数的返回语句有意义。 +
## Tests
```yml tests: - - text: testLessThan(0)应该返回“25岁以下” + - text: testLessThan(0)应该返回 "Under 25"。 testString: assert(testLessThan(0) === "Under 25"); - - text: testLessThan(24)应该返回“25岁以下” + - text: testLessThan(24)应该返回 "Under 25"。 testString: assert(testLessThan(24) === "Under 25"); - - text: testLessThan(25)应该返回“55岁以下” + - text: testLessThan(25)应该返回 "Under 55"。 testString: assert(testLessThan(25) === "Under 55"); - - text: testLessThan(54)应该返回“55岁以下” + - text: testLessThan(54)应该返回 "Under 55"。 testString: assert(testLessThan(54) === "Under 55"); - - text: testLessThan(55)应返回“55或以上” + - text: testLessThan(55)应该返回 "55 or Over"。 testString: assert(testLessThan(55) === "55 or Over"); - - text: testLessThan(99)应返回“55或以上” + - text: testLessThan(99)应该返回 "55 or Over"。 testString: assert(testLessThan(99) === "55 or Over"); - - text: 您应该至少使用<运算符两次 + - text: 你应该使用<运算符至少两次。 testString: assert(code.match(/val\s*<\s*('|")*\d+('|")*/g).length > 1); ``` @@ -56,7 +71,6 @@ function testLessThan(val) { // Change this value to test testLessThan(10); - ``` @@ -68,7 +82,19 @@ testLessThan(10); ## Solution
+ ```js -// solution required +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"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-less-than-or-equal-to-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-less-than-or-equal-to-operator.chinese.md index 9645eb2eae..0997d28855 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-less-than-or-equal-to-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-less-than-or-equal-to-operator.chinese.md @@ -2,36 +2,51 @@ id: 56533eb9ac21ba0edf2244d7 title: Comparison with the Less Than Or Equal To Operator challengeType: 1 -videoUrl: '' -localeTitle: 与小于或等于运算符的比较 +videoUrl: 'https://scrimba.com/c/cNVR7Am' +forumTopicId: 16788 +localeTitle: 小于或等于运算符 --- ## Description -
less than or equal to运算符( <= )比较两个数字的值。如果左边的数字小于或等于右边的数字,则返回true 。如果左侧的数字大于右侧的数字,则返回false 。与等于运算符一样, less than or equal to转换数据类型。 例子
4 <= 5 //是的
'7'<= 7 //是的
5 <= 5 //是的
3 <= 2 //假
'8'<= 4 //假
+
+使用小于等于运算符(<=)比较两个数字的大小。如果在小于等于运算符左边的数字小于或者等于右边的数字,它会返回true。如果在小于等于运算符左边的数字大于右边的数字,它会返回false。与相等运算符类似,小于等于运算符会转换数据类型。 +例如 + +```js +4 <= 5 // true +'7' <= 7 // true +5 <= 5 // true +3 <= 2 // false +'8' <= 4 // false +``` + +
## Instructions -
less than or equal to运算符添加到指示的行,以便返回语句有意义。
+
+添加小于等于运算符到指定行,使得函数的返回语句有意义。 +
## Tests
```yml tests: - - text: testLessOrEqual(0)应该返回“小于或等于12” + - text: testLessOrEqual(0)应该返回 "Smaller Than or Equal to 12"。 testString: assert(testLessOrEqual(0) === "Smaller Than or Equal to 12"); - - text: testLessOrEqual(11)应返回“小于或等于12” + - text: testLessOrEqual(11)应该返回 "Smaller Than or Equal to 12"。 testString: assert(testLessOrEqual(11) === "Smaller Than or Equal to 12"); - - text: testLessOrEqual(12)应返回“小于或等于12” + - text: testLessOrEqual(12)应该返回 "Smaller Than or Equal to 12"。 testString: assert(testLessOrEqual(12) === "Smaller Than or Equal to 12"); - - text: testLessOrEqual(23)应返回“小于或等于24” + - text: testLessOrEqual(23)应该返回 "Smaller Than or Equal to 24"。 testString: assert(testLessOrEqual(23) === "Smaller Than or Equal to 24"); - - text: testLessOrEqual(24)应返回“小于或等于24” + - text: testLessOrEqual(24)应该返回 "Smaller Than or Equal to 24"。 testString: assert(testLessOrEqual(24) === "Smaller Than or Equal to 24"); - - text: testLessOrEqual(25)应该返回“超过24” + - text: testLessOrEqual(25)应该返回 "More Than 24"。 testString: assert(testLessOrEqual(25) === "More Than 24"); - - text: testLessOrEqual(55)应该返回“超过24” + - text: testLessOrEqual(55)应该返回 "More Than 24"。 testString: assert(testLessOrEqual(55) === "More Than 24"); - - text: 你应该至少使用<=运算符两次 + - text: 你应该使用<=运算符至少两。 testString: assert(code.match(/val\s*<=\s*('|")*\d+('|")*/g).length > 1); ``` @@ -70,7 +85,19 @@ testLessOrEqual(10); ## Solution
+ ```js -// solution required +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"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-strict-equality-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-strict-equality-operator.chinese.md index af99e042f2..ca6320fa0a 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-strict-equality-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-strict-equality-operator.chinese.md @@ -2,28 +2,42 @@ id: 56533eb9ac21ba0edf2244d1 title: Comparison with the Strict Equality Operator challengeType: 1 -videoUrl: '' -localeTitle: 与严格平等算子的比较 +videoUrl: 'https://scrimba.com/c/cy87atr' +forumTopicId: 16790 +localeTitle: 严格相等运算符 --- ## Description -
严格相等( === )是相等运算符( == )的对应物。但是,与尝试将两个值转换为常见类型的等式运算符不同,严格相等运算符不执行类型转换。如果要比较的值具有不同的类型,则认为它们不相等,并且严格相等运算符将返回false。 例子
3 === 3 //是的
3 ==='3'//假
在第二个示例中, 3Number类型, '3'String类型。
+
+严格相等运算符(===)是相对相等操作符(==)的另一种比较操作符。与相等操作符不同的是,它会同时比较元素的值和数据类型。 +如果比较的值类型不同,那么在严格相等运算符比较下它们是不相等的,会返回 false 。 +示例 + +```js +3 === 3 // true +3 === '3' // false +``` + +3是一个数字类型的,而'3'是一个字符串类型的,所以 3 不全等于 '3'。 +
## Instructions -
if语句中使用strict equality运算符,因此当val严格等于7时,函数将返回“Equal”
+
+在if语句值使用严格相等运算符,这样当val严格等于7的时候,函数会返回"Equal"。 +
## Tests
```yml tests: - - text: testStrict(10)应返回“Not Equal” + - text: testStrict(10)应该返回 "Not Equal"。 testString: assert(testStrict(10) === "Not Equal"); - - text: testStrict(7)应返回“Equal” + - text: testStrict(7)应该返回 "Equal"。 testString: assert(testStrict(7) === "Equal"); - - text: testStrict("7")应返回“Not Equal” + - text: testStrict("7")应该返回 "Not Equal"。 testString: assert(testStrict("7") === "Not Equal"); - - text: 您应该使用===运算符 + - text: 你应该使用===运算符。 testString: assert(code.match(/(val\s*===\s*\d+)|(\d+\s*===\s*val)/g).length > 0); ``` @@ -46,7 +60,6 @@ function testStrict(val) { // Change this value to test testStrict(10); - ``` @@ -58,7 +71,14 @@ testStrict(10); ## Solution
+ ```js -// solution required +function testStrict(val) { + if (val === 7) { + return "Equal"; + } + return "Not Equal"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-strict-inequality-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-strict-inequality-operator.chinese.md index 6c10a3c05b..add77bcdcb 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-strict-inequality-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-strict-inequality-operator.chinese.md @@ -2,30 +2,43 @@ id: 56533eb9ac21ba0edf2244d3 title: Comparison with the Strict Inequality Operator challengeType: 1 -videoUrl: '' -localeTitle: 与严格不等式算子的比较 +videoUrl: 'https://scrimba.com/c/cKekkUy' +forumTopicId: 16791 +localeTitle: 严格不等运算符 --- ## Description -
严格不等式运算符( !== )与严格相等运算符的逻辑相反。它意味着“严格不等于”并返回false ,其中严格相等将返回true反之亦然 。严格的不等式不会转换数据类型。 例子
3!== 3 //假
3!=='3'//是的
4!== 3 //是的
+
+严格不相等运算符(!==)与全等运算符是相反的。这意味着严格不相等并返回false的地方,用严格相等运算符会返回true反之亦然。严格不相等运算符不会转换值的数据类型。 +示例 + +```js +3 !== 3 // false +3 !== '3' // true +4 !== 3 // true +``` + +
## Instructions -
strict inequality operator添加到if语句,以便当val不严格等于17时,函数将返回“Not Equal”
+
+在if语句中,添加严格不相等运算符!==,这样如果val17严格不相等的时候,函数会返回 "Not Equal"。 +
## Tests
```yml tests: - - text: testStrictNotEqual(17)应返回“Equal” + - text: testStrictNotEqual(17)应该返回 "Equal"。 testString: assert(testStrictNotEqual(17) === "Equal"); - - text: testStrictNotEqual("17")应返回“Not Equal” + - text: testStrictNotEqual("17")应该返回 "Not Equal"。 testString: assert(testStrictNotEqual("17") === "Not Equal"); - - text: testStrictNotEqual(12)应该返回“Not Equal” + - text: testStrictNotEqual(12)应该返回 "Not Equal"。 testString: assert(testStrictNotEqual(12) === "Not Equal"); - - text: testStrictNotEqual("bob")应返回“Not Equal” + - text: testStrictNotEqual("bob")应该返回 "Not Equal"。 testString: assert(testStrictNotEqual("bob") === "Not Equal"); - - text: 你应该使用!==运算符 + - text: 应该使用 !== 运算符。 testString: assert(code.match(/(val\s*!==\s*\d+)|(\d+\s*!==\s*val)/g).length > 0); ``` @@ -40,12 +53,7 @@ tests: ```js // Setup function testStrictNotEqual(val) { - // Only Change Code Below this Line - - if (val) { - - // Only Change Code Above this Line - + if (val) { // Change this line return "Not Equal"; } return "Equal"; @@ -53,7 +61,6 @@ function testStrictNotEqual(val) { // Change this value to test testStrictNotEqual(10); - ``` @@ -65,7 +72,14 @@ testStrictNotEqual(10); ## Solution
+ ```js -// solution required +function testStrictNotEqual(val) { + if (val !== 17) { + return "Not Equal"; + } + return "Equal"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparisons-with-the-logical-and-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparisons-with-the-logical-and-operator.chinese.md index 5e4aac8149..a2547414bb 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparisons-with-the-logical-and-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparisons-with-the-logical-and-operator.chinese.md @@ -2,40 +2,65 @@ id: 56533eb9ac21ba0edf2244d8 title: Comparisons with the Logical And Operator challengeType: 1 -videoUrl: '' -localeTitle: 与逻辑和运算符的比较 +videoUrl: 'https://scrimba.com/c/cvbRVtr' +forumTopicId: 16799 +localeTitle: 逻辑与运算符 --- ## Description -
有时你需要一次测试多个东西。当且仅当其左侧和右侧的操作数true时, 逻辑和运算符( && )才返回true。如果将if语句嵌套在另一个语句中,则可以实现相同的效果:
if(num> 5){
if(num <10){
返回“是”;
}
}
返回“否”;
如果num大于5且小于10则仅返回“Yes”。相同的逻辑可以写成:
if(num> 5 && num <10){
返回“是”;
}
返回“否”;
+
+有时你需要在一次判断中做多个操作。当且仅当运算符的左边和右边都是true逻辑与 运算符(&&)才会返回true。 +同样的效果可以通过 if 语句的嵌套来实现: + +```js +if (num > 5) { + if (num < 10) { + return "Yes"; + } +} +return "No"; +``` + +只有当num的值在 6 和 9 之间(包括 6 和 9)才会返回 "Yes"。相同的逻辑可被写为: + +```js +if (num > 5 && num < 10) { + return "Yes"; +} +return "No"; +``` + +
## Instructions -
将两个if语句合并为一个语句,如果val小于或等于50且大于或等于25 ,则返回"Yes" 。否则,将返回"No"
+
+请使用逻辑与运算符把两个 if 语句合并为一个 if 语句,如果val小于或等于50并且大于或等于25,返回"Yes"。否则,将返回"No"。 +
## Tests
```yml tests: - - text: 你应该使用一次&&运算符 - testString: assert(code.match(/&&/g).length === 1); - - text: 你应该只有一个if语句 + - text: 你应该使用&&运算符一次。 + testString: assert(code.match(/&&/g).length === 1,); + - text: 你应该只有一个if表达式。 testString: assert(code.match(/if/g).length === 1); - - text: testLogicalAnd(0)应返回“否” + - text: testLogicalAnd(0)应该返回 "No"。 testString: assert(testLogicalAnd(0) === "No"); - - text: testLogicalAnd(24)应返回“否” + - text: testLogicalAnd(24)应该返回 "No"。 testString: assert(testLogicalAnd(24) === "No"); - - text: testLogicalAnd(25)应返回“是” + - text: testLogicalAnd(25)应该返回 "Yes"。 testString: assert(testLogicalAnd(25) === "Yes"); - - text: testLogicalAnd(30)应该返回“是” + - text: testLogicalAnd(30)应该返回 "Yes"。 testString: assert(testLogicalAnd(30) === "Yes"); - - text: testLogicalAnd(50)应该返回“是” + - text: testLogicalAnd(50)应该返回 "Yes"。 testString: assert(testLogicalAnd(50) === "Yes"); - - text: testLogicalAnd(51)应返回“否” + - text: testLogicalAnd(51)应该返回 "No"。 testString: assert(testLogicalAnd(51) === "No"); - - text: testLogicalAnd(75)应返回“否” + - text: testLogicalAnd(75)应该返回 "No"。 testString: assert(testLogicalAnd(75) === "No"); - - text: testLogicalAnd(80)应返回“否” + - text: testLogicalAnd(80)应该返回 "No"。 testString: assert(testLogicalAnd(80) === "No"); ``` @@ -63,7 +88,6 @@ function testLogicalAnd(val) { // Change this value to test testLogicalAnd(10); - ``` @@ -75,7 +99,14 @@ testLogicalAnd(10); ## Solution
+ ```js -// solution required +function testLogicalAnd(val) { + if (val >= 25 && val <= 50) { + return "Yes"; + } + return "No"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparisons-with-the-logical-or-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparisons-with-the-logical-or-operator.chinese.md index f5be8fd788..3bff592203 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparisons-with-the-logical-or-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/comparisons-with-the-logical-or-operator.chinese.md @@ -2,40 +2,67 @@ id: 56533eb9ac21ba0edf2244d9 title: Comparisons with the Logical Or Operator challengeType: 1 -videoUrl: '' -localeTitle: 与逻辑或运算符的比较 +videoUrl: 'https://scrimba.com/c/cEPrGTN' +forumTopicId: 16800 +localeTitle: 逻辑或运算符 --- ## Description -
逻辑OR运算符( || )返回true ,如果任一操作数true 。否则,它返回false逻辑或运算符由两个管道符号( | )组成。这通常可以在Backspace和Enter键之间找到。以下模式应该从以前的方法点看起来很熟悉:
if(num> 10){
返回“否”;
}
if(num <5){
返回“否”;
}
返回“是”;
仅当num介于510之间(包括5和10)时,才会返回“Yes”。相同的逻辑可以写成:
if(num> 10 || num <5){
返回“否”;
}
返回“是”;
+
+只要逻辑或运算符||两边任何一个为true,那么它就返回true;否则返回false。 +逻辑或运算符由两个管道符号(|)组成。这个按键位于退格键和回车键之间。 +下面这样的语句你应该很熟悉: + +```js +if (num > 10) { + return "No"; +} +if (num < 5) { + return "No"; +} +return "Yes"; +``` + +只有当num大于等于 5 或小于等于 10 时,函数返回"Yes"。相同的逻辑可以简写成: + +```js +if (num > 10 || num < 5) { + return "No"; +} +return "Yes"; +``` + +
## Instructions -
将两个if语句组合成一个语句,如果val不在1020之间(包括1020 ,则返回"Outside" 。否则,返回"Inside"
+
+请使用逻辑或运算符把两个 if 语句合并为一个 if 语句,如果val不在 10 和 20 之间(包括 10 和 20),返回"Outside"。反之,返回"Inside"。 +
## Tests
```yml tests: - - text: 你应该使用||操作员一次 + - text: 你应该使用一次||操作符。 testString: assert(code.match(/\|\|/g).length === 1); - - text: 你应该只有一个if语句 + - text: 你应该只有一个if表达式。 testString: assert(code.match(/if/g).length === 1); - - text: testLogicalOr(0)应返回“Outside” + - text: testLogicalOr(0)应该返回 "Outside"。 testString: assert(testLogicalOr(0) === "Outside"); - - text: testLogicalOr(9)应返回“Outside” + - text: testLogicalOr(9)应该返回 "Outside"。 testString: assert(testLogicalOr(9) === "Outside"); - - text: testLogicalOr(10)应返回“Inside” + - text: testLogicalOr(10)应该返回 "Inside"。 testString: assert(testLogicalOr(10) === "Inside"); - - text: testLogicalOr(15)应返回“Inside” + - text: testLogicalOr(15)应该返回 "Inside"。 testString: assert(testLogicalOr(15) === "Inside"); - - text: testLogicalOr(19)应该返回“Inside” + - text: testLogicalOr(19)应该返回 "Inside"。 testString: assert(testLogicalOr(19) === "Inside"); - - text: testLogicalOr(20)应该返回“Inside” + - text: testLogicalOr(20)应该返回 "Inside"。 testString: assert(testLogicalOr(20) === "Inside"); - - text: testLogicalOr(21)应该返回“Outside” + - text: testLogicalOr(21)应该返回 "Outside"。 testString: assert(testLogicalOr(21) === "Outside"); - - text: testLogicalOr(25)应返回“Outside” + - text: testLogicalOr(25)应该返回 "Outside"。 testString: assert(testLogicalOr(25) === "Outside"); ``` @@ -65,7 +92,6 @@ function testLogicalOr(val) { // Change this value to test testLogicalOr(15); - ``` @@ -77,7 +103,14 @@ testLogicalOr(15); ## Solution
+ ```js -// solution required +function testLogicalOr(val) { + if (val < 10 || val > 20) { + return "Outside"; + } + return "Inside"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-addition.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-addition.chinese.md index 348af920fe..b928c2e45a 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-addition.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-addition.chinese.md @@ -2,30 +2,45 @@ id: 56533eb9ac21ba0edf2244af title: Compound Assignment With Augmented Addition challengeType: 1 -videoUrl: '' -localeTitle: 具有增强加法的复合赋值 +videoUrl: 'https://scrimba.com/c/cDR6LCb' +forumTopicId: 16661 +localeTitle: 复合赋值之 += --- ## Description -
在编程中,通常使用赋值来修改变量的内容。请记住,首先评估等号右侧的所有内容,因此我们可以说: myVar = myVar + 5;添加5myVar 。由于这是一种常见的模式,因此存在一步完成数学运算和赋值的运算符。一个这样的运算符是+=运算符。
var myVar = 1;
myVar + = 5;
的console.log(myVar的); //返回6
+
+在编程当中,通常通过赋值来修改变量的内容。记住,赋值时 Javascript 会先计算=右边的内容,所以我们可以写这样的语句: +myVar = myVar + 5; +以上是最常见的运算赋值语句,即先运算、再赋值。还有一类操作符是一步到位既做运算也赋值的。 +其中一种就是+=运算符。 + +```js +var myVar = 1; +myVar += 5; +console.log(myVar); // Returns 6 +``` + +
## Instructions -
转换abc的赋值以使用+=运算符。
+
+使用+=操作符实现同样的效果。 +
## Tests
```yml tests: - - text: a应该等于15 + - text: a应该等于15。 testString: assert(a === 15); - - text: b应该等于26 + - text: b应该等于26。 testString: assert(b === 26); - - text: c应该等于19 + - text: c应该等于19。 testString: assert(c === 19); - - text: 您应该为每个变量使用+=运算符 + - text: 你应该对每个变量使用+=操作符。 testString: assert(code.match(/\+=/g).length === 3); - - text: 不要修改行上方的代码 + - text: 不要修改注释上面的代码。 testString: assert(/var a = 3;/.test(code) && /var b = 17;/.test(code) && /var c = 12;/.test(code)); ``` @@ -57,7 +72,7 @@ c = c + 7;
```js -console.info('after the test'); +(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c); ```
@@ -67,7 +82,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var a = 3; +var b = 17; +var c = 12; + +a += 12; +b += 9; +c += 7; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-division.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-division.chinese.md index f69e8d0c03..26d8523ec1 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-division.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-division.chinese.md @@ -2,30 +2,38 @@ id: 56533eb9ac21ba0edf2244b2 title: Compound Assignment With Augmented Division challengeType: 1 -videoUrl: '' -localeTitle: 具有增广划分的复合赋值 +videoUrl: 'https://scrimba.com/c/c2QvKT2' +forumTopicId: 16659 +localeTitle: 复合赋值之 /= --- ## Description -
/=运算符将变量除以另一个数字。 myVar = myVar / 5;myVar除以5 。这可以改写为: myVar /= 5;
+
+/=操作符是让变量与另一个数相除并赋值。 +myVar = myVar / 5; +变量myVar等于自身除以5的值。等价于: +myVar /= 5; +
## Instructions -
转换abc的赋值以使用/=运算符。
+
+使用/=操作符实现同样的效果。 +
## Tests
```yml tests: - - text: a应该等于4 + - text: a应该等于4。 testString: assert(a === 4); - - text: b应该等于27 + - text: b应该等于27。 testString: assert(b === 27); - - text: c应该等于3 + - text: c应该等于3。 testString: assert(c === 3); - - text: 您应该为每个变量使用/=运算符 + - text: 应该对每个变量使用/=操作符。 testString: assert(code.match(/\/=/g).length === 3); - - text: 不要修改行上方的代码 + - text: 不要修改注释上面的代码。 testString: assert(/var a = 48;/.test(code) && /var b = 108;/.test(code) && /var c = 33;/.test(code)); ``` @@ -57,7 +65,7 @@ c = c / 11;
```js -console.info('after the test'); +(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c); ```
@@ -67,7 +75,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var a = 48; +var b = 108; +var c = 33; + +a /= 12; +b /= 4; +c /= 11; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-multiplication.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-multiplication.chinese.md index 583e18c29f..940c1bc7c6 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-multiplication.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-multiplication.chinese.md @@ -2,30 +2,38 @@ id: 56533eb9ac21ba0edf2244b1 title: Compound Assignment With Augmented Multiplication challengeType: 1 -videoUrl: '' -localeTitle: 具有增广乘法的复合赋值 +videoUrl: 'https://scrimba.com/c/c83vrfa' +forumTopicId: 16662 +localeTitle: 复合赋值之 *= --- ## Description -
*=运算符将变量乘以数字。 myVar = myVar * 5;myVar乘以5 。这可以改写为: myVar *= 5;
+
+*=操作符是让变量与一个数相乘并赋值。 +myVar = myVar * 5; +变量myVar等于自身与数值5相乘的值。也可以写作这样的形式: +myVar *= 5; +
## Instructions -
转换abc的赋值以使用*=运算符。
+
+使用*=操作符实现同样的效果。 +
## Tests
```yml tests: - - text: a应该等于25 + - text: a应该等于25。 testString: assert(a === 25); - - text: b应该等于36 + - text: b应该等于36。 testString: assert(b === 36); - - text: c应该等于46 + - text: c应该等于46。 testString: assert(c === 46); - - text: 您应该为每个变量使用*=运算符 + - text: 应该对每个变量使用*=操作符。 testString: assert(code.match(/\*=/g).length === 3); - - text: 不要修改行上方的代码 + - text: 不要修改注释上面的代码。 testString: assert(/var a = 5;/.test(code) && /var b = 12;/.test(code) && /var c = 4\.6;/.test(code)); ``` @@ -48,6 +56,7 @@ a = a * 5; b = 3 * b; c = c * 10; + ``` @@ -57,7 +66,7 @@ c = c * 10;
```js -console.info('after the test'); +(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c); ```
@@ -67,7 +76,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var a = 5; +var b = 12; +var c = 4.6; + +a *= 5; +b *= 3; +c *= 10; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-subtraction.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-subtraction.chinese.md index 2289d1311b..a954384640 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-subtraction.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/compound-assignment-with-augmented-subtraction.chinese.md @@ -2,30 +2,38 @@ id: 56533eb9ac21ba0edf2244b0 title: Compound Assignment With Augmented Subtraction challengeType: 1 -videoUrl: '' -localeTitle: 具有增广减法的复合赋值 +videoUrl: 'https://scrimba.com/c/c2Qv7AV' +forumTopicId: 16660 +localeTitle: 复合赋值之 -= --- ## Description -
+=运算符一样, -=从变量中减去一个数字。 myVar = myVar - 5;将从myVar减去5 。这可以改写为: myVar -= 5;
+
+与+=操作符类似,-=操作符用来对一个变量进行减法赋值操作。 +myVar = myVar - 5; +变量myVar等于自身减去5的值。也可以写成这种形式: +myVar -= 5; +
## Instructions -
转换abc的赋值以使用-=运算符。
+
+使用-=操作符实现同样的效果。 +
## Tests
```yml tests: - - text: a应该等于5 + - text: a应该等于5。 testString: assert(a === 5); - - text: b应该等于-6 + - text: b应该等于-6。 testString: assert(b === -6); - - text: c应该等于2 + - text: c应该等于2。 testString: assert(c === 2); - - text: 您应该为每个变量使用-=运算符 + - text: 应该对每个变量使用-=操作符。 testString: assert(code.match(/-=/g).length === 3); - - text: 不要修改行上方的代码 + - text: 不要修改注释上面的代码。 testString: assert(/var a = 11;/.test(code) && /var b = 9;/.test(code) && /var c = 3;/.test(code)); ``` @@ -48,6 +56,7 @@ a = a - 6; b = b - 15; c = c - 1; + ``` @@ -57,7 +66,7 @@ c = c - 1;
```js -console.info('after the test'); +(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c); ```
@@ -67,7 +76,17 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var a = 11; +var b = 9; +var c = 3; + +a -= 6; +b -= 15; +c -= 1; + + ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-plus-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-plus-operator.chinese.md index 99b93de881..002bee660b 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-plus-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-plus-operator.chinese.md @@ -2,28 +2,40 @@ id: 56533eb9ac21ba0edf2244b7 title: Concatenating Strings with Plus Operator challengeType: 1 -videoUrl: '' -localeTitle: 用Plus运算符连接字符串 +videoUrl: 'https://scrimba.com/c/cNpM8AN' +forumTopicId: 16802 +localeTitle: 用加号运算符连接字符串 --- ## Description -
在JavaScript中,当+运算符与String值一起使用时,它被称为连接运算符。您可以通过它们连接在一起来构建其他字符串中的新字符串。
“我叫艾伦,'+'我连接起来。”
注意
留意空间。连接不会在连接字符串之间添加空格,因此您需要自己添加它们。
+
+在 JavaScript 中,当对一个String类型的值使用+操作符的时候,它被称作 拼接操作符。你可以通过拼接其他字符串来创建一个新的字符串。 +示例 + +```js +'My name is Alan,' + ' I concatenate.' +``` + +提示
注意空格。拼接操作不会在两个字符串之间添加空格,所以想加上空格的话,你需要自己在字符串里面添加。 +
## Instructions -
从字符串构建myStr "This is the start. ""This is the end."使用+运算符。
+
+使用+操作符,把字符串"This is the start. ""This is the end."连接起来并赋值给变量myStr。 +
## Tests
```yml tests: - - text: myStr应该有一个值This is the start. This is the end. + - text: myStr的值应该是This is the start. This is the end.。 testString: assert(myStr === "This is the start. This is the end."); - - text: 使用+运算符构建myStr + - text: 使用+操作符构建myStr。 testString: assert(code.match(/(["']).*(["'])\s*\+\s*(["']).*(["'])/g).length > 1); - - text: 应使用var关键字创建myStr 。 + - text: myStr应该被var关键字声明。 testString: assert(/var\s+myStr/.test(code)); - - text: 确保将结果分配给myStr变量。 + - text: 确保有给myStr赋值。 testString: assert(/myStr\s*=/.test(code)); ``` @@ -43,6 +55,7 @@ var ourStr = "I come first. " + "I come second."; var myStr; + ``` @@ -52,7 +65,13 @@ var myStr;
```js -console.info('after the test'); +(function(){ + if(typeof myStr === 'string') { + return 'myStr = "' + myStr + '"'; + } else { + return 'myStr is not a string'; + } +})(); ```
@@ -62,7 +81,10 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var ourStr = "I come first. " + "I come second."; +var myStr = "This is the start. " + "This is the end."; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.chinese.md index 3b410131d2..b3ee29d2e0 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.chinese.md @@ -2,24 +2,30 @@ id: 56533eb9ac21ba0edf2244b8 title: Concatenating Strings with the Plus Equals Operator challengeType: 1 -videoUrl: '' -localeTitle: 使用Plus Equals运算符连接字符串 +videoUrl: 'https://scrimba.com/c/cbQmmC4' +forumTopicId: 16803 +localeTitle: 用 += 运算符连接字符串 --- ## Description -
我们还可以使用+=运算符将字符串连接到现有字符串变量的末尾。这对于在多行上打破长字符串非常有帮助。 注意
留意空间。连接不会在连接字符串之间添加空格,因此您需要自己添加它们。
+
+我们还可以使用+=运算符来concatenate(拼接)字符串到现有字符串的结尾。对于那些被分割成几段的长的字符串来说,这一操作是非常有用的。 +提示
注意空格。拼接操作不会在两个字符串之间添加空格,所以如果想要加上空格的话,你需要自己在字符串里面添加。 +
## Instructions -
通过连接这两个字符串来构建myStr几行: "This is the first sentence. ""This is the second sentence."使用+=运算符。使用+=运算符,类似于它在编辑器中的显示方式。首先将第一个字符串分配给myStr ,然后添加第二个字符串。
+
+通过使用+=操作符来连接这两个字符串:
"This is the first sentence. ""This is the second sentence."并赋给变量myStr。 +
## Tests
```yml tests: - - text: myStr应该有一个值This is the first sentence. This is the second sentence. + - text: myStr的值应该是This is the first sentence. This is the second sentence.。 testString: assert(myStr === "This is the first sentence. This is the second sentence."); - - text: 使用+=运算符构建myStr + - text: 使用+=操作符创建myStr变量。 testString: assert(code.match(/\w\s*\+=\s*["']/g).length > 1 && code.match(/\w\s*\=\s*["']/g).length > 1); ``` @@ -40,6 +46,7 @@ ourStr += "I come second."; var myStr; + ``` @@ -49,7 +56,13 @@ var myStr;
```js -console.info('after the test'); +(function(){ + if(typeof myStr === 'string') { + return 'myStr = "' + myStr + '"'; + } else { + return 'myStr is not a string'; + } +})(); ```
@@ -59,7 +72,13 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var ourStr = "I come first. "; +ourStr += "I come second."; + +var myStr = "This is the first sentence. "; +myStr += "This is the second sentence."; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/constructing-strings-with-variables.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/constructing-strings-with-variables.chinese.md index 086ef3571e..d7cef899c6 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/constructing-strings-with-variables.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/constructing-strings-with-variables.chinese.md @@ -2,24 +2,29 @@ id: 56533eb9ac21ba0edf2244b9 title: Constructing Strings with Variables challengeType: 1 -videoUrl: '' +videoUrl: 'https://scrimba.com/c/cqk8rf4' +forumTopicId: 16805 localeTitle: 用变量构造字符串 --- ## Description -
有时您需要构建一个字符串, Mad Libs样式。通过使用连接运算符( + ),您可以将一个或多个变量插入到正在构建的字符串中。
+
+有时候你需要创建一个类似Mad Libs(填词游戏)风格的字符串。通过使用连接运算符 + ,你可以插入一个或多个变量来组成一个字符串。 +
## Instructions -
myName设置为等于您的名字的字符串,并在字符串"My name is "" and I am well!"之间用myName构建myStr " and I am well!"
+
+把你的名字赋值给变量myName,然后把变量myName插入到字符串"My name is "" and I am well!"之间,并把连接后的结果赋值给变量myStr。 +
## Tests
```yml tests: - - text: myName应设置为至少3个字符长的字符串 + - text: myName至少要包含三个字符。 testString: assert(typeof myName !== 'undefined' && myName.length > 2); - - text: 使用两个+运算符在其中构建myStr with myName + - text: 使用两个+操作符创建包含myNamemyStr变量。 testString: assert(code.match(/["']\s*\+\s*myName\s*\+\s*["']/g).length > 0); ``` @@ -40,6 +45,7 @@ var ourStr = "Hello, our name is " + ourName + ", how are you?"; var myName; var myStr; + ``` @@ -49,7 +55,20 @@ var myStr;
```js -console.info('after the test'); +(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'); +})(); ```
@@ -59,7 +78,10 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myName = "Bob"; +var myStr = "My name is " + myName + " and I am well!"; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/count-backwards-with-a-for-loop.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/count-backwards-with-a-for-loop.chinese.md index 366c5af36b..8b9b9216c5 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/count-backwards-with-a-for-loop.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/count-backwards-with-a-for-loop.chinese.md @@ -2,26 +2,43 @@ id: 56105e7b514f539506016a5e title: Count Backwards With a For Loop challengeType: 1 -videoUrl: '' -localeTitle: 用For循环向后计数 +videoUrl: 'https://scrimba.com/c/c2R6BHa' +forumTopicId: 16808 +localeTitle: 使用 For 循环反向遍历数组 --- ## Description -
只要我们可以定义正确的条件,for循环也可以向后计数。为了向后计数两次,我们需要更改initializationconditionfinal-expression 。我们将从i = 10开始并在i > 0循环。我们将递减i 2每个回路与i -= 2
var ourArray = [];
for(var i = 10; i> 0; i- = 2){
ourArray.push(ⅰ);
}
ourArray现在包含[10,8,6,4,2] 。让我们改变initializationfinal-expression这样我们就可以向后计数两位奇数。
+
+for循环也可以逆向迭代,只要我们定义好合适的条件。 +为了让每次倒数递减 2,我们需要改变我们的初始化条件判断计数器。 +我们让i = 10,并且当i > 0的时候才继续循环。我们使用i -= 2来让i每次循环递减 2。 + +```js +var ourArray = []; +for (var i=10; i > 0; i-=2) { + ourArray.push(i); +} +``` + +循环结束后,ourArray的值为[10,8,6,4,2]。 +让我们改变初始化计数器,这样我们就可以按照奇数从后往前两两倒着数。 +
## Instructions -
使用for循环将奇数从9到1推送到myArray
+
+使用一个for循环,把 9 到 1 的奇数添加进myArray。 +
## Tests
```yml tests: - - text: 你应该为此使用for循环。 + - text: 你应该使用for循环。 testString: assert(code.match(/for\s*\(/g).length > 1); - - text: 你应该使用数组方法push 。 + - text: 你应该使用数组方法push。 testString: assert(code.match(/myArray.push/)); - - text: 'myArray应该等于[9,7,5,3,1] 。' + - text: myArray应该等于[9,7,5,3,1]。 testString: assert.deepEqual(myArray, [9,7,5,3,1]); ``` @@ -46,6 +63,7 @@ var myArray = []; // Only change code below this line. + ``` @@ -55,7 +73,7 @@ var myArray = [];
```js -console.info('after the test'); +if(typeof myArray !== "undefined"){(function(){return myArray;})();} ```
@@ -65,7 +83,16 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var ourArray = []; +for (var i = 10; i > 0; i -= 2) { + ourArray.push(i); +} +var myArray = []; +for (var i = 9; i > 0; i -= 2) { + myArray.push(i); +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/counting-cards.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/counting-cards.chinese.md index e9f7724c89..c7125657f2 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/counting-cards.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/counting-cards.chinese.md @@ -2,15 +2,26 @@ id: 565bbe00e9cc8ac0725390f4 title: Counting Cards challengeType: 1 -videoUrl: '' -localeTitle: 计数卡 +videoUrl: 'https://scrimba.com/c/c6KE7ty' +forumTopicId: 16809 +localeTitle: 21点游戏 --- ## Description -
在赌场游戏Blackjack中,玩家可以通过跟踪牌组中剩余的高牌和低牌的相对数量来获得优势。这称为卡计数 。在牌组中剩下更多高牌有利于玩家。根据下表为每张卡分配一个值。当计数为正数时,玩家应该下注。当计数为零或负数时,玩家应该下注低。
计数变化
+1 2,3,4,5,6
0 7,8,9
-1 10,'J','Q','K','A'
你会写一个卡计数功能。它将接收一个card参数,可以是数字或字符串,并根据卡的值递增或递减全局count变量(参见表格)。然后,该函数将返回一个包含当前计数的字符串,如果计数为正则返回字符串Bet ,如果计数为零或为负,则返回Hold 。当前计数和玩家的决定( BetHold )应该由一个空格分隔。 示例输出
-3 Hold
5 Bet 提示
当值为7,8或9时,请勿将count重置为0。
不要返回数组。
不要在输出中包含引号(单引号或双引号)。
+
+在赌场 21 点游戏中,玩家可以通过计算牌桌上已经发放的卡牌的高低值来让自己在游戏中保持优势,这就叫 21 点算法。 +根据下面的表格,每张卡牌都分配了一个值。如果卡牌的值大于 0,那么玩家应该追加赌注。反之,追加少许赌注甚至不追加赌注。 +
Count ChangeCards
+12, 3, 4, 5, 6
07, 8, 9
-110, 'J', 'Q', 'K', 'A'
+你需要写一个函数实现 21 点算法,它根据参数card的值来递增或递减变量count,函数返回一个由当前countBetcount>0)或Holdcount<=0)拼接的字符串。注意count"Bet"Hold应该用空格分开。 + +例如:
-3 Hold
5 Bet
+ +提示
既然 card 的值为 7、8、9 时,count 值不变,那我们就可以忽略这种情况。 +
## Instructions -
+
+
## Tests @@ -18,19 +29,19 @@ localeTitle: 计数卡 ```yml tests: - - text: 牌序列5 Bet应该返回5 Bet + - text: Cards Sequence 2, 3, 4, 5, 6 应该返回5 Bet。 testString: assert((function(){ count = 0; cc(2);cc(3);cc(4);cc(5);var out = cc(6); if(out === "5 Bet") {return true;} return false; })()); - - text: '卡片序列7,8,9应返回0 Hold' + - text: Cards Sequence 7, 8, 9 应该返回 0 Hold。 testString: assert((function(){ count = 0; cc(7);cc(8);var out = cc(9); if(out === "0 Hold") {return true;} return false; })()); - - text: 卡序列10,J,Q,K,A应返回-5 Hold + - text: Cards Sequence 10, J, Q, K, A 应该返回 -5 Hold。 testString: assert((function(){ count = 0; cc(10);cc('J');cc('Q');cc('K');var out = cc('A'); if(out === "-5 Hold") {return true;} return false; })()); - - text: '卡序列3,7,Q,8,A应返回-1 Hold' + - text: Cards Sequence 3, 7, Q, 8, A 应该返回 -1 Hold。 testString: assert((function(){ count = 0; cc(3);cc(7);cc('Q');cc(8);var out = cc('A'); if(out === "-1 Hold") {return true;} return false; })()); - - text: 牌序列2,J, 1 Bet应该返回1 Bet + - text: Cards Sequence 2, J, 9, 2, 7 应该返回 1 Bet。 testString: assert((function(){ count = 0; cc(2);cc('J');cc(9);cc(2);var out = cc(7); if(out === "1 Bet") {return true;} return false; })()); - - text: 牌序列1 Bet应该返回1 Bet + - text: Cards Sequence 2, 2, 10 应该返回 1 Bet。 testString: assert((function(){ count = 0; cc(2);cc(2);var out = cc(10); if(out === "1 Bet") {return true;} return false; })()); - - text: '卡序列3,2,A,10,K应返回-1 Hold' + - text: Cards Sequence 3, 2, A, 10, K 应该返回 -1 Hold。 testString: assert((function(){ count = 0; cc(3);cc(2);cc('A');cc(10);var out = cc('K'); if(out === "-1 Hold") {return true;} return false; })()); ``` @@ -56,7 +67,6 @@ function cc(card) { // Add/remove calls to test your function. // Note: Only the last will display cc(2); cc(3); cc(7); cc('K'); cc('A'); - ``` @@ -68,7 +78,31 @@ cc(2); cc(3); cc(7); cc('K'); cc('A'); ## Solution
+ ```js -// solution required +var 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"; + } +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/create-decimal-numbers-with-javascript.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/create-decimal-numbers-with-javascript.chinese.md index 3a5c4a4313..cd1b274697 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/create-decimal-numbers-with-javascript.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/create-decimal-numbers-with-javascript.chinese.md @@ -2,15 +2,21 @@ id: cf1391c1c11feddfaeb4bdef title: Create Decimal Numbers with JavaScript challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript创建十进制数 +videoUrl: 'https://scrimba.com/c/ca8GEuW' +forumTopicId: 16826 +localeTitle: 创建一个小数 --- ## Description -
我们也可以在变量中存储十进制数。十进制数有时称为浮点数浮点数注意
并非所有实数都可以准确地以浮点表示。这可能导致舍入错误。 细节在这里
+
+我们也可以把小数存储到变量中。小数也被称作浮点数 。 +提示
不是所有的实数都可以用 浮点数 来表示。因为可能存在四舍五入的错误,详情查看。 +
## Instructions -
创建一个变量myDecimal并给它一个带小数部分的十进制值(例如5.7 )。
+
+创建一个变量myDecimal并给它赋值一个浮点数。(例如5.21)。 +
## Tests
@@ -19,8 +25,8 @@ localeTitle: 使用JavaScript创建十进制数 tests: - text: myDecimal应该是一个数字。 testString: assert(typeof myDecimal === "number"); - - text: myDecimal应该有一个小数点 - testString: assert(myDecimal % 1 != 0); + - text: myDecimal应该包含小数点。 + testString: assert(myDecimal % 1 != 0); ``` @@ -36,6 +42,7 @@ var ourDecimal = 5.7; // Only change code below this line + ``` @@ -45,7 +52,7 @@ var ourDecimal = 5.7;
```js -console.info('after the test'); +(function(){if(typeof myDecimal !== "undefined"){return myDecimal;}})(); ```
@@ -55,7 +62,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myDecimal = 9.9; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/declare-javascript-variables.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/declare-javascript-variables.chinese.md index 3d9aafaef2..42148555a3 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/declare-javascript-variables.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/declare-javascript-variables.chinese.md @@ -2,23 +2,40 @@ id: bd7123c9c443eddfaeb5bdef title: Declare JavaScript Variables challengeType: 1 -videoUrl: '' -localeTitle: 声明JavaScript变量 +videoUrl: 'https://scrimba.com/c/cNanrHq' +forumTopicId: 17556 +localeTitle: 声明变量 --- ## Description -
在计算机科学中, 数据是对计算机有意义的任何东西。 JavaScript提供了七种不同的数据类型 ,它们是undefinednullbooleanstringsymbolnumberobject 。例如,计算机区分数字(例如数字12 )和strings (例如"12""dog""123 cats" ,它们是字符集合。计算机可以对数字执行数学运算,但不能对字符串执行数学运算。 变量允许计算机以动态方式存储和操作数据。他们通过使用“标签”指向数据而不是使用数据本身来做到这一点。七种数据类型中的任何一种都可以存储在变量中。 Variables类似于您在数学中使用的x和y变量,这意味着它们是表示我们想要引用的数据的简单名称。计算机variables与数学variables不同之处在于它们可以在不同时间存储不同的值。我们告诉JavaScript通过将关键字var放在它前面来创建或声明变量,如下所示:
var ourName;
创建一个名为ourNamevariable 。在JavaScript中,我们以分号结束语句。 Variable名可以由数字,字母和$_ ,但不能包含空格或以数字开头。
+
+在计算机科学中,数据就是一切,它对于计算机意义重大。JavaScript 提供七种不同的数据类型,它们是undefined(未定义), null(空),boolean(布尔型),string(字符串),symbol(符号),number(数字),和object(对象)。 +例如,计算机区分数字和字符集合的字符串,例如数字12和字符串"12""dog""123 cats"。计算机可以对数字执行数学运算,但不能对字符串执行数学运算。 +变量允许计算机以一种动态的形式来存储和操作数据,通过操作指向数据的指针而不是数据本身来避免了内存泄露,以上的七种数据类型都可以存储到一个变量中。 +变量非常类似于你在数学中使用的 x,y 变量,都是以一个简单命名的名称来代替我们赋值给它的数据。计算机中的变量与数学中的变量不同的是,计算机可以在不同的时间存储不同类型的变量。 +通过在变量的前面使用关键字var,声明一个变量,例如: + +```js +var ourName; +``` + +上面代码的意思是创建一个名为ourName变量,在 JavaScript 中我们以分号结束语句。 +变量名称可以由数字、字母、美元符号$ 或者 下划线_组成,但是不能包含空格或者以数字为开头。 +
## Instructions -
使用var关键字创建名为myName的变量。 暗示
如果你遇到ourName查看我们的ourName示例。
+
+使用var 关键字来创建一个名为myName的变量。 +提示
如果遇到困难了,请看下ourName的例子是怎么写的。 +
## Tests
```yml tests: - - text: 您应该使用var关键字声明myName ,以分号结尾 - testString: assert(/var\s+myName\s*;/.test(code)); + - text: 你需要使用var关键字定义一个变量myName,并使用分号结尾。 + testString: assert(/var\s+myName\s*;/.test(code), '你需要使用var关键字定义一个变量myName。并使用分号结尾。'); ``` @@ -44,7 +61,7 @@ var ourName;
```js -console.info('after the test'); +if(typeof myName !== "undefined"){(function(v){return v;})(myName);} ```
@@ -54,7 +71,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myName; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/declare-string-variables.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/declare-string-variables.chinese.md index 568e3153af..2ca8ab4d31 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/declare-string-variables.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/declare-string-variables.chinese.md @@ -2,24 +2,31 @@ id: bd7123c9c444eddfaeb5bdef title: Declare String Variables challengeType: 1 -videoUrl: '' +videoUrl: 'https://scrimba.com/c/c2QvWU6' +forumTopicId: 17557 localeTitle: 声明字符串变量 --- ## Description -
以前我们使用过代码var myName = "your name"; "your name"被称为字符串 文字 。它是一个字符串,因为它是用单引号或双引号括起来的一系列零个或多个字符。
+
+之前我们写过这样的代码: +var myName = "your name"; +"your name"被称作字符串变量。字符串是用单引号或双引号包裹起来的一连串的零个或多个字符。 +
## Instructions -
创建两个新的string变量: myFirstNamemyLastName并分别为它们分配myFirstNamemyLastName的值。
+
+创建两个新的字符串变量myFirstNamemyLastName,并用你的姓和名分别为它们赋值。 +
## Tests
```yml tests: - - text: myFirstName应该是一个至少包含一个字符的字符串。 + - text: myFirstName应该是一个字符串,并且至少包含一个字符。 testString: assert((function(){if(typeof myFirstName !== "undefined" && typeof myFirstName === "string" && myFirstName.length > 0){return true;}else{return false;}})()); - - text: myLastName应该是一个至少包含一个字符的字符串。 + - text: myLastName应该是一个字符串,并且至少包含一个字符。 testString: assert((function(){if(typeof myLastName !== "undefined" && typeof myLastName === "string" && myLastName.length > 0){return true;}else{return false;}})()); ``` @@ -38,6 +45,7 @@ var lastName = "Turing"; // Only change code below this line + ``` @@ -47,7 +55,7 @@ var lastName = "Turing";
```js -console.info('after the test'); +if(typeof myFirstName !== "undefined" && typeof myLastName !== "undefined"){(function(){return myFirstName + ', ' + myLastName;})();} ```
@@ -57,7 +65,10 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myFirstName = "Alan"; +var myLastName = "Turing"; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/decrement-a-number-with-javascript.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/decrement-a-number-with-javascript.chinese.md index c7d2255d46..bf393199e5 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/decrement-a-number-with-javascript.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/decrement-a-number-with-javascript.chinese.md @@ -2,28 +2,37 @@ id: 56533eb9ac21ba0edf2244ad title: Decrement a Number with JavaScript challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript减少数字 +videoUrl: 'https://scrimba.com/c/cM2KeS2' +forumTopicId: 17558 +localeTitle: 数字递减 --- ## Description -
您可以使用--运算符轻松地将变量减1或减1。 i--;相当于i = i - 1; 注意
整条线变成了i--; ,消除了对等号的需要。
+
+使用自减符号--,你可以很方便地对一个变量执行自减或者-1运算。 +i--; +等效于 +i = i - 1; +提示
i--;这种写法,省去了书写=符号的必要。 +
## Instructions -
更改代码以在myVar上使用--运算符。
+
+重写代码,使用--符号对myVar执行自减操作。 +
## Tests
```yml tests: - - text: myVar应该等于10 + - text: myVar应该等于10。 testString: assert(myVar === 10); - - text: myVar = myVar - 1;应该改变 + - text: myVar = myVar - 1;语句应该被修改。 testString: assert(/var\s*myVar\s*=\s*11;\s*\/*.*\s*([-]{2}\s*myVar|myVar\s*[-]{2});/.test(code)); - - text: 在myVar上使用--运算符 + - text: 对myVar使用--运算符。 testString: assert(/[-]{2}\s*myVar|myVar\s*[-]{2}/.test(code)); - - text: 不要更改行上方的代码 + - text: 不要修改注释上面的代码。 testString: assert(/var myVar = 11;/.test(code)); ``` @@ -50,7 +59,7 @@ myVar = myVar - 1;
```js -console.info('after the test'); +(function(z){return 'myVar = ' + z;})(myVar); ```
@@ -60,7 +69,10 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myVar = 11; +myVar--; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/delete-properties-from-a-javascript-object.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/delete-properties-from-a-javascript-object.chinese.md index 9d22d5d188..dae2b8353a 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/delete-properties-from-a-javascript-object.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/delete-properties-from-a-javascript-object.chinese.md @@ -2,24 +2,30 @@ id: 56bbb991ad1ed5201cd392d3 title: Delete Properties from a JavaScript Object challengeType: 1 -videoUrl: '' -localeTitle: 从JavaScript对象中删除属性 +videoUrl: 'https://scrimba.com/c/cDqKdTv' +forumTopicId: 17560 +localeTitle: 删除对象的属性 --- ## Description -
我们还可以删除对象中的属性,如下所示: delete ourDog.bark;
+
+我们同样可以删除对象的属性,例如: +delete ourDog.bark; +
## Instructions -
myDog删除"tails"属性。您可以使用点或括号表示法。
+
+删除myDog对象的"tails"属性。 +
## Tests
```yml tests: - - text: 从myDog删除属性"tails" 。 + - text: 从myDog中删除"tails"属性。 testString: assert(typeof myDog === "object" && myDog.tails === undefined); - - text: 不要修改myDog设置 + - text: 不要修改myDog的初始化。 testString: 'assert(code.match(/"tails": 1/g).length > 1);' ``` @@ -54,6 +60,7 @@ var myDog = { // Only change code below this line. + ``` @@ -63,7 +70,7 @@ var myDog = {
```js -console.info('after the test'); +(function(z){return z;})(myDog); ```
@@ -73,7 +80,23 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var ourDog = { + "name": "Camper", + "legs": 4, + "tails": 1, + "friends": ["everything!"], + "bark": "bow-wow" +}; +var myDog = { + "name": "Happy Coder", + "legs": 4, + "tails": 1, + "friends": ["freeCodeCamp Campers"], + "bark": "woof" +}; +delete myDog.tails; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/divide-one-decimal-by-another-with-javascript.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/divide-one-decimal-by-another-with-javascript.chinese.md index be6db96555..35e3e3b9d7 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/divide-one-decimal-by-another-with-javascript.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/divide-one-decimal-by-another-with-javascript.chinese.md @@ -2,26 +2,31 @@ id: bd7993c9ca9feddfaeb7bdef title: Divide One Decimal by Another with JavaScript challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript将另一个十进制除以另一个 +videoUrl: 'https://scrimba.com/c/cBZe9AW' +forumTopicId: 18255 +localeTitle: 两个小数相除 --- ## Description -
现在让我们将一位小数除以另一位小数。
+
+现在让我们将一个小数除以另一个小数。 +
## Instructions -
更改0.0使quotient等于2.2
+
+改变数值0.0的值让变量quotient的值等于2.2. +
## Tests
```yml tests: - - text: 变quotient应该等于2.2 + - text: quotient的值应该等于2.2。 testString: assert(quotient === 2.2); - - text: 您应该使用/运算符将4.4除以2 + - text: 使用/运算符将 4.4 除以 2。 testString: assert(/4\.40*\s*\/\s*2\.*0*/.test(code)); - - text: 商数变量只应分配一次 + - text: quotient 变量应该只被赋值一次。 testString: assert(code.match(/quotient/g).length === 1); ``` @@ -35,7 +40,6 @@ tests: ```js var quotient = 0.0 / 2.0; // Fix this line - ``` @@ -45,7 +49,7 @@ var quotient = 0.0 / 2.0; // Fix this line
```js -console.info('after the test'); +(function(y){return 'quotient = '+y;})(quotient); ```
@@ -56,6 +60,7 @@ console.info('after the test');
```js -// solution required +var quotient = 4.4 / 2.0; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/divide-one-number-by-another-with-javascript.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/divide-one-number-by-another-with-javascript.chinese.md index d58a9c4db0..d1d536456b 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/divide-one-number-by-another-with-javascript.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/divide-one-number-by-another-with-javascript.chinese.md @@ -2,24 +2,38 @@ id: cf1111c1c11feddfaeb6bdef title: Divide One Number by Another with JavaScript challengeType: 1 -videoUrl: '' -localeTitle: 用JavaScript划分一个号码 +videoUrl: 'https://scrimba.com/c/cqkbdAr' +forumTopicId: 17566 +localeTitle: 除法运算 --- ## Description -
我们也可以将一个数字除以另一个数字。 JavaScript使用/符号进行除法。

myVar = 16/2; //分配8
+
+我们可以在 JavaScript 中做除法运算。 +JavaScript 中使用/符号做除法运算。 + +示例 + +```js +myVar = 16 / 2; // assigned 8 +``` + + +
## Instructions -
更改0 ,使quotient等于2
+
+改变数值0来让变量quotient的值等于2。 +
## Tests
```yml tests: - - text: 使变量quotient等于2。 + - text: 要使quotient的值等于 2。 testString: assert(quotient === 2); - - text: 使用/运算符 + - text: 使用/运算符。 testString: assert(/\d+\s*\/\s*\d+/.test(code)); ``` @@ -34,6 +48,7 @@ tests: ```js var quotient = 66 / 0; + ``` @@ -43,7 +58,7 @@ var quotient = 66 / 0;
```js -console.info('after the test'); +(function(z){return 'quotient = '+z;})(quotient); ```
@@ -53,7 +68,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var quotient = 66 / 33; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/escape-sequences-in-strings.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/escape-sequences-in-strings.chinese.md index 2f594f5cf6..9d1858c841 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/escape-sequences-in-strings.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/escape-sequences-in-strings.chinese.md @@ -2,33 +2,46 @@ id: 56533eb9ac21ba0edf2244b6 title: Escape Sequences in Strings challengeType: 1 -videoUrl: '' +videoUrl: 'https://scrimba.com/c/cvmqRh6' +forumTopicId: 17567 localeTitle: 字符串中的转义序列 --- ## Description -
引号不是可以在字符串中转义的唯一字符。使用转义字符有两个原因:首先是允许您使用您可能无法输入的字符,例如退格。其次是允许你在一个字符串中表示多个引号,而不会误解你的意思。我们在之前的挑战中学到了这一点。
产量
\' 单引号
\" 双引号
\\ 反斜线
\n 新队
\r 回车
\t 标签
\b 退格
\f 形式饲料
请注意,必须对反斜杠本身进行转义才能显示为反斜杠。
+
+引号不是字符串中唯一可以被转义的字符。使用转义字符有两个原因:首先是可以让你使用无法输入的字符,例如退格。其次是可以让你在一个字符串中表示多个引号,而不会出错。我们在之前的挑战中学到了这个。 +
代码输出
\'单引号
\"双引号
\\反斜杠
\n换行符
\r回车符
\t制表符
\b退格
\f换页符
+请注意,必须对反斜杠本身进行转义才能显示为反斜杠。 +
## Instructions -
使用转义序列将以下三行文本分配到单个变量myStr
第一行
\第二行
ThirdLine
您将需要使用转义序列正确插入特殊字符。您还需要按照上面的间距来跟踪,在转义序列或单词之间没有空格。这是写出转义序列的文本。 FirstLine newline tab backslash SecondLine newline ThirdLine
+
+使用转义字符将下面三行文本字符串赋给变量myStr。 +
FirstLine
    \SecondLine
ThirdLine
+你需要使用转义字符正确地插入特殊字符,确保间距与上面文本一致并且单词或转义字符之间没有空格。 +像这样用转义字符写出来: +FirstLine换行符制表符反斜杠SecondLine换行符ThirdLine +
## Tests
```yml tests: - - text: myStr不应包含任何空格 - testString: 'assert(!/ /.test(myStr), "myStr should not contain any spaces");' - - text: myStr应包含的字符串FirstLineSecondLineThirdLine (记住区分大小写) - testString: 'assert(/FirstLine/.test(myStr) && /SecondLine/.test(myStr) && /ThirdLine/.test(myStr), "myStr should contain the strings FirstLine, SecondLine and ThirdLine (remember case sensitivity)");' - - text: FirstLine后面应跟换行符\n - testString: 'assert(/FirstLine\n/.test(myStr), "FirstLine should be followed by the newline character \n");' - - text: myStr应该包含一个制表字符\t ,它跟在换行符后面 - testString: 'assert(/\n\t/.test(myStr), "myStr should contain a tab character \t which follows a newline character");' - - text: SecondLine应该以反斜杠字符\\开头 - testString: 'assert(/\SecondLine/.test(myStr), "SecondLine should be preceded by the backslash character \\");' - - text: SecondLineThirdLine之间应该有换行符 - testString: 'assert(/SecondLine\nThirdLine/.test(myStr), "There should be a newline character between SecondLine and ThirdLine");' + - text: myStr不能包含空格。 + testString: assert(!/ /.test(myStr)); + - text: myStr应该包含字符串FirstLine, SecondLine and ThirdLine (记得区分大小写)。 + testString: assert(/FirstLine/.test(myStr) && /SecondLine/.test(myStr) && /ThirdLine/.test(myStr)); + - text: FirstLine后面应该是一个新行\n。 + testString: assert(/FirstLine\n/.test(myStr)); + - text: myStr应该包含制表符\t并且制表符要在换行符后面。 + testString: assert(/\n\t/.test(myStr)); + - text: SecondLine前面应该是反斜杠\\。 + testString: assert(/\SecondLine/.test(myStr)); + - text: SecondLineThirdLine之间应该是换行符。 + testString: assert(/SecondLine\nThirdLine/.test(myStr)); + - text: myStr 应该只包含介绍里面展示的字符串。 + testString: assert(myStr === 'FirstLine\n\t\\SecondLine\nThirdLine'); ``` @@ -42,6 +55,7 @@ tests: ```js var myStr; // Change this line + ``` @@ -51,7 +65,9 @@ var myStr; // Change this line
```js -console.info('after the test'); +(function(){ +if (myStr !== undefined){ +console.log('myStr:\n' + myStr);}})(); ```
@@ -61,7 +77,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myStr = "FirstLine\n\t\\SecondLine\nThirdLine"; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/escaping-literal-quotes-in-strings.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/escaping-literal-quotes-in-strings.chinese.md index 23023ad4b3..cca3508843 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/escaping-literal-quotes-in-strings.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/escaping-literal-quotes-in-strings.chinese.md @@ -2,25 +2,35 @@ id: 56533eb9ac21ba0edf2244b5 title: Escaping Literal Quotes in Strings challengeType: 1 -videoUrl: '' -localeTitle: 逃避字符串中的字面引用 +videoUrl: 'https://scrimba.com/c/c2QvgSr' +forumTopicId: 17568 +localeTitle: 转义字符串中的引号 --- ## Description -
在定义字符串时,必须以单引号或双引号开头和结尾。当你需要一个文字报价会发生什么: "还是' ?你的字符串里面在JavaScript中,你可以放置一个反斜杠 (从考虑到它作为字符串报价的最终逃脱报价\在引号前)。 var sampleStr = "Alan said, \"Peter is learning JavaScript\".";这告诉JavaScript,以下引用不是字符串的结尾,而是应该出现在字符串中。所以如果要将它打印到控制台,你会得到: Alan said, "Peter is learning JavaScript".
+
+定义一个字符串必须要用单引号或双引号来包裹它。那么当你的字符串里面包含:"或者'时该怎么办呢? +在 JavaScript 中,你可以通过在引号前面使用反斜杠\)来转义引号。 +var sampleStr = "Alan said, \"Peter is learning JavaScript\"."; +有了转义符号,JavaScript 就知道这个单引号或双引号并不是字符串的结尾,而是字符串内的字符。所以,上面的字符串打印到控制台的结果为: +Alan said, "Peter is learning JavaScript". +
## Instructions -
使用反斜杠将字符串分配给myStr变量,这样如果要将其打印到控制台,您会看到: I am a "double quoted" string inside "double quotes".
+
+使用反斜杠将一个字符串赋值给变量myStr,打印到控制台,输出为: +I am a "double quoted" string inside "double quotes". +
## Tests
```yml tests: - - text: 您应该使用两个双引号( " )和四个转义双引号( \" )。 + - text: 你的代码中应该包含两个双引号 (") 以及四个转义的双引 (\")。 testString: assert(code.match(/\\"/g).length === 4 && code.match(/[^\\]"/g).length === 2); - - text: 变量myStr应该包含字符串: I am a "double quoted" string inside "double quotes". - testString: 'assert(myStr === "I am a \"double quoted\" string inside \"double quotes\".");' + - text: 变量 myStr 应该包含字符串I am a "double quoted" string inside "double quotes".。 + testString: assert(myStr === "I am a \"double quoted\" string inside \"double quotes\"."); ``` @@ -34,6 +44,7 @@ tests: ```js var myStr = ""; // Change this line + ``` @@ -43,7 +54,13 @@ var myStr = ""; // Change this line
```js -console.info('after the test'); +(function(){ + if(typeof myStr === 'string') { + console.log("myStr = \"" + myStr + "\""); + } else { + console.log("myStr is undefined"); + } +})(); ```
@@ -53,7 +70,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myStr = "I am a \"double quoted\" string inside \"double quotes\"."; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/find-the-length-of-a-string.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/find-the-length-of-a-string.chinese.md index b85f8789f0..a27e69360a 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/find-the-length-of-a-string.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/find-the-length-of-a-string.chinese.md @@ -2,25 +2,34 @@ id: bd7123c9c448eddfaeb5bdef title: Find the Length of a String challengeType: 1 -videoUrl: '' -localeTitle: 找到字符串的长度 +videoUrl: 'https://scrimba.com/c/cvmqEAd' +forumTopicId: 18182 +localeTitle: 查找字符串的长度 --- ## Description -
您可以通过在字符串变量或字符串文字后面写.length来查找String值的长度。 "Alan Peter".length; // 10例如,如果我们创建了一个变量var firstName = "Charles" ,我们可以通过使用firstName.length属性找出字符串"Charles"长度。
+
+你可以通过在字符串变量或字符串后面写上.length来获得字符串变量值的长度。 +"Alan Peter".length; // 10 +例如,我们创建了一个变量var firstName = "Charles",我们就可以通过使用firstName.length来获得"Charles"字符串的长度。 +
## Instructions -
使用.length属性计算lastName变量中的字符数,并将其分配给lastNameLength
+
+使用.length属性来获得变量lastName的长度,并把它赋值给变量lastNameLength。 +
## Tests
```yml tests: - - text: lastNameLength应该等于8。 - testString: 'assert((function(){if(typeof lastNameLength !== "undefined" && typeof lastNameLength === "number" && lastNameLength === 8){return true;}else{return false;}})(), "lastNameLength should be equal to eight.");' - - text: 您应该使用.length来获取lastName的长度,如下所示: lastName.length 。 - testString: 'assert((function(){if(code.match(/\.length/gi) && code.match(/\.length/gi).length >= 2 && code.match(/var lastNameLength \= 0;/gi) && code.match(/var lastNameLength \= 0;/gi).length >= 1){return true;}else{return false;}})(), "You should be getting the length of lastName by using .length like this: lastName.length.");' + - text: 不能改变 // Setup 部分声明的变量。 + testString: assert(code.match(/var lastNameLength = 0;/) && code.match(/var lastName = "Lovelace";/)); + - text: lastNameLength应该等于 8。 + testString: assert(typeof lastNameLength !== 'undefined' && lastNameLength === 8); + - text: 你应该使用 .length 获取 lastName 的长度,像这样 lastName.length。 + testString: assert(code.match(/=\s*lastName\.length/g) && !code.match(/lastName\s*=\s*8/)); ``` @@ -46,16 +55,7 @@ var lastName = "Lovelace"; lastNameLength = lastName; -``` - - - -### After Test -
- -```js -console.info('after the test'); ```
@@ -65,7 +65,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var firstNameLength = 0; +var firstName = "Ada"; +firstNameLength = firstName.length; + +var lastNameLength = 0; +var lastName = "Lovelace"; +lastNameLength = lastName.length; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.chinese.md index 2dd74e1eeb..257fba1f0f 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.chinese.md @@ -2,26 +2,36 @@ id: 56533eb9ac21ba0edf2244ae title: Finding a Remainder in JavaScript challengeType: 1 -videoUrl: '' -localeTitle: 在JavaScript中查找剩余内容 +videoUrl: 'https://scrimba.com/c/cWP24Ub' +forumTopicId: 18184 +localeTitle: 求余运算 --- ## Description -
余数运算符%给出了两个数的除法的余数。
5%2 = 1因为
Math.floor(5/2)= 2(商数)
2 * 2 = 4
5 - 4 = 1(剩余)
用法
在数学中,通过检查数字除以2的余数,可以检查数字是偶数还是奇数。
17%2 = 1(17为奇数)
48%2 = 0(48为偶数)
注意
余数运算符有时被错误地称为“模数”运算符。它与模数非常相似,但在负数下不能正常工作。
+
+remainder求余运算符%返回两个数相除得到的余数 +示例 +
5 % 2 = 1 因为
Math.floor(5 / 2) = 2 (商)
2 * 2 = 4
5 - 4 = 1 (余数)
+用法
在数学中,判断一个数是奇数还是偶数,只需要判断这个数除以 2 得到的余数是 0 还是 1。 +
17 % 2 = 1(17 是奇数)
48 % 2 = 0(48 是偶数)
+提示
余数运算符(remainder)有时被错误地称为“模数”运算符。它与模数非常相似,但不能用于负数的运算。 +
## Instructions -
使用余数% )运算符将remainder设置为等于11的余数除以3
+
+使用%运算符,计算 11 除以 3 的余数,并把余数赋给变量 remainder。 +
## Tests
```yml tests: - - text: 应该初始化变量remainder + - text: 变量remainder应该被初始化。 testString: assert(/var\s+?remainder/.test(code)); - - text: remainder的值应为2 + - text: remainder的值应该等于2。 testString: assert(remainder === 2); - - text: 您应该使用%运算符 + - text: 你应该使用%运算符。 testString: assert(/\s+?remainder\s*?=\s*?.*%.*;/.test(code)); ``` @@ -47,7 +57,7 @@ var remainder;
```js -console.info('after the test'); +(function(y){return 'remainder = '+y;})(remainder); ```
@@ -57,7 +67,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var remainder = 11 % 3; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-fractions-with-javascript.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-fractions-with-javascript.chinese.md index 0718a55b69..54e83f3770 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-fractions-with-javascript.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-fractions-with-javascript.chinese.md @@ -2,15 +2,22 @@ id: cf1111c1c11feddfaeb9bdef title: Generate Random Fractions with JavaScript challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript生成随机分数 +videoUrl: 'https://scrimba.com/c/cyWJJs3' +forumTopicId: 18185 +localeTitle: 使用 JavaScript 生成随机分数 --- ## Description -
随机数对于创建随机行为很有用。 JavaScript有一个Math.random()函数,它生成一个介于0 (含)和不高达1 (独占)之间的随机十进制数。因此Math.random()可以返回0但永远不会返回1 Note
与使用Equal运算符存储值一样,所有函数调用将在return执行之前解析,因此我们可以return Math.random()函数的值。
+
+随机数非常适合用来创建随机行为。 +Math.random()用来生成一个在0(包括 0)到1(不包括 1)之间的随机小数,因此Math.random()可能返回 0 但绝不会返回 1。 +提示
使用赋值运算符存储值这一节讲过,所有函数调用将在return执行之前解析,因此我们可以返回Math.random()函数的值。 +
## Instructions -
更改randomFraction以返回随机数而不是返回0
+
+更改randomFraction使其返回一个随机数而不是0。 +
## Tests
@@ -19,9 +26,9 @@ localeTitle: 使用JavaScript生成随机分数 tests: - text: randomFraction应该返回一个随机数。 testString: assert(typeof randomFraction() === "number"); - - text: randomFraction返回的randomFraction应该是小数。 + - text: randomFraction应该返回一个小数。 testString: assert((randomFraction()+''). match(/\./g)); - - text: 您应该使用Math.random来生成随机十进制数。 + - text: 需要使用Math.random生成随机的小数。 testString: assert(code.match(/Math\.random/g).length >= 0); ``` @@ -42,7 +49,6 @@ function randomFraction() { // Only change code above this line. } - ``` @@ -52,7 +58,7 @@ function randomFraction() {
```js -console.info('after the test'); +(function(){return randomFraction();})(); ```
@@ -62,7 +68,11 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +function randomFraction() { + return Math.random(); +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-with-javascript.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-with-javascript.chinese.md index aab74f7602..4c6c1751fd 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-with-javascript.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-with-javascript.chinese.md @@ -2,28 +2,38 @@ id: cf1111c1c12feddfaeb1bdef title: Generate Random Whole Numbers with JavaScript challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript生成随机整数 +videoUrl: 'https://scrimba.com/c/cRn6bfr' +forumTopicId: 18186 +localeTitle: 使用 JavaScript 生成随机整数 --- ## Description -
我们可以生成随机十进制数很好,但如果我们用它来生成随机整数,它会更有用。
  1. 使用Math.random()生成随机小数。
  2. 将随机小数乘以20
  3. 使用另一个函数Math.floor()将数字向下舍入到最接近的整数。
请记住, Math.random()永远不会返回1 ,因为我们正在向下舍入,实际上不可能得到20 。这项技术将给我们一个019之间的整数。将所有内容放在一起,这就是我们的代码: Math.floor(Math.random() * 20);我们调用Math.random() ,将结果乘以20,然后将值传递给Math.floor()函数,将值向下舍入到最接近的整数。
+
+生成随机小数很棒,但随机数更有用的地方在于生成随机整数。 +
  1. Math.random()生成一个随机小数。
  2. 把这个随机小数乘以20
  3. Math.floor()向下取整 获得它最近的整数。
+记住Math.random()永远不会返回1。同时因为我们是在用Math.floor()向下取整,所以最终我们获得的结果不可能有20。这确保了我们获得了一个在0到19之间的整数。 +把操作连缀起来,代码类似于下面: +Math.floor(Math.random() * 20); +我们先调用Math.random(),把它的结果乘以20,然后把上一步的结果传给Math.floor(),最终通过向下取整获得最近的整数。 +
## Instructions -
使用此技术生成并返回09之间的随机整数。
+
+生成一个09之间的随机整数。 +
## Tests
```yml tests: - - text: randomWholeNum的结果应该是整数。 + - text: myFunction的结果应该是一个整数。 testString: assert(typeof randomWholeNum() === "number" && (function(){var r = randomWholeNum();return Math.floor(r) === r;})()); - - text: 您应该使用Math.random来生成随机数。 + - text: 需要使用Math.random生成随机数字。 testString: assert(code.match(/Math.random/g).length > 1); - - text: 您应该将Math.random的结果乘以10,使其成为介于0和9之间的数字。 + - text: 你应该将Math.random的结果乘以 10 来生成 0 到 9 之间的随机数。 testString: 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)); - - text: 您应该使用Math.floor删除数字的小数部分。 + - text: 你需要使用Math.floor移除数字中的小数部分。 testString: assert(code.match(/Math.floor/g).length > 1); ``` @@ -44,7 +54,6 @@ function randomWholeNum() { return Math.random(); } - ``` @@ -54,7 +63,7 @@ function randomWholeNum() {
```js -console.info('after the test'); +(function(){return randomWholeNum();})(); ```
@@ -64,7 +73,12 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var randomNumberBetween0and19 = Math.floor(Math.random() * 20); +function randomWholeNum() { + return Math.floor(Math.random() * 10); +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-within-a-range.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-within-a-range.chinese.md index bb25b72917..410c0cc942 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-within-a-range.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-within-a-range.chinese.md @@ -2,28 +2,36 @@ id: cf1111c1c12feddfaeb2bdef title: Generate Random Whole Numbers within a Range challengeType: 1 -videoUrl: '' -localeTitle: 生成范围内的随机整数 +videoUrl: 'https://scrimba.com/c/cm83yu6' +forumTopicId: 18187 +localeTitle: 生成某个范围内的随机整数 --- ## Description -
我们可以生成一个落在两个特定数字范围内的随机数,而不是像我们之前那样在零和给定数字之间生成一个随机数。为此,我们将定义最小数量min和最大数量max 。这是我们将使用的公式。花一点时间阅读它并尝试理解这段代码在做什么: Math.floor(Math.random() * (max - min + 1)) + min
+
+我们之前生成的随机数是在0到某个数之间,现在我们要生成的随机数是在两个指定的数之间。 +我们需要定义一个最小值和一个最大值。 +下面是我们将要使用的方法,仔细看看并尝试理解这行代码到底在干嘛: +Math.floor(Math.random() * (max - min + 1)) + min +
## Instructions -
创建一个名为randomRange的函数,它接受一个范围myMinmyMax并返回一个大于或等于myMin的随机数,并且小于或等于myMax (包括myMax )。
+
+创建一个叫randomRange的函数,参数为 myMin 和 myMax,返回一个在myMin(包括 myMin)和myMax(包括 myMax)之间的随机数。 +
## Tests
```yml tests: - - text: randomRange可以生成的最低随机数应该等于你的最小数量myMin 。 + - text: randomRange返回的随机数应该大于或等于myMin。 testString: assert(calcMin === 5); - - text: randomRange可以生成的最高随机数应该等于最大数量myMax 。 + - text: randomRange返回的随机数应该小于或等于myMax。 testString: assert(calcMax === 15); - - text: randomRange生成的随机数应该是整数,而不是小数。 + - text: randomRange应该返回一个随机整数, 而不是小数。 testString: assert(randomRange(0,1) % 1 === 0 ); - - text: randomRange应该同时使用myMaxmyMin ,并在你的范围内返回一个随机数。 + - text: randomRange应该使用myMaxmyMin, 并且返回两者之间的随机数。 testString: 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;}})()); ``` @@ -54,7 +62,6 @@ function randomRange(myMin, myMax) { // Change these values to test your function var myRandom = randomRange(5, 15); - ``` @@ -64,7 +71,20 @@ var myRandom = randomRange(5, 15);
```js -console.info('after the test'); +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"; + } +})() ```
@@ -74,7 +94,11 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +function randomRange(myMin, myMax) { + return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.chinese.md index 23966be58e..2084becdcc 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.chinese.md @@ -2,28 +2,35 @@ id: 56533eb9ac21ba0edf2244be title: Global Scope and Functions challengeType: 1 -videoUrl: '' -localeTitle: 全球范围和职能 +videoUrl: 'https://scrimba.com/c/cQM7mCN' +forumTopicId: 18193 +localeTitle: 全局作用域和函数 --- ## Description -
在JavaScript中, 范围是指变量的可见性。在功能块之外定义的变量具有全局范围。这意味着,它们可以在JavaScript代码中随处可见。在没有var关键字的情况下使用的变量将在global范围内自动创建。这可能会在代码中的其他位置或再次运行函数时产生意外后果。您应该始终使用var声明变量。
+
+在 JavaScript 中,作用域涉及到变量的作用范围。在函数外定义的变量具有 全局 作用域。这意味着,具有全局作用域的变量可以在代码的任何地方被调用。 +这些没有使用var关键字定义的变量,会被自动创建在全局作用域中,形成全局变量。当在代码其他地方无意间定义了一个变量,刚好变量名与全局变量相同,这时会产生意想不到的后果。因此你应该总是使用var关键字来声明你的变量。 +
## Instructions -
使用var ,在任何函数之外声明一个global变量myGlobal 。使用值10初始化它。在函数fun1内部,在使用var关键字的情况下oopsGlobal分配5
+
+在函数外声明一个全局变量myGlobal,并给它一个初始值10 +在函数fun1的内部,使用var关键字来声明oopsGlobal,并赋值为5。 +
## Tests
```yml tests: - - text: 应该定义myGlobal + - text: 应定义myGlobal。 testString: assert(typeof myGlobal != "undefined"); - - text: myGlobal的值应为10 + - text: myGlobal的值应为10。 testString: assert(myGlobal === 10); - - text: 应使用var关键字声明myGlobal + - text: 应使用var关键字定义myGlobal。 testString: assert(/var\s+myGlobal/.test(code)); - - text: oopsGlobal应该是一个全局变量,其值为5 + - text: oopsGlobal应为全局变量且值为5。 testString: assert(typeof oopsGlobal != "undefined" && oopsGlobal === 5); ``` @@ -55,7 +62,6 @@ function fun2() { } console.log(output); } - ``` @@ -84,7 +90,6 @@ function uncapture() { } var oopsGlobal; capture(); - ``` @@ -93,7 +98,10 @@ capture();
```js -console.info('after the test'); +fun1(); +fun2(); +uncapture(); +(function() { return logOutput || "console.log never called"; })(); ```
@@ -103,7 +111,27 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +// Declare your variable here +var myGlobal = 10; + +function fun1() { + // Assign 5 to oopsGlobal Here + oopsGlobal = 5; +} + +// 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); +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/global-vs.-local-scope-in-functions.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/global-vs.-local-scope-in-functions.chinese.md index eae7118a27..7f4aa6b970 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/global-vs.-local-scope-in-functions.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/global-vs.-local-scope-in-functions.chinese.md @@ -2,26 +2,42 @@ id: 56533eb9ac21ba0edf2244c0 title: Global vs. Local Scope in Functions challengeType: 1 -videoUrl: '' -localeTitle: 功能中的全局与局部范围 +videoUrl: 'https://scrimba.com/c/c2QwKH2' +forumTopicId: 18194 +localeTitle: 函数中的全局作用域和局部作用域 --- ## Description -
可以使本地变量和全局变量具有相同的名称。执行此操作时, local变量优先于global变量。在这个例子中:
var someVar =“帽子”;
function myFun(){
var someVar =“Head”;
返回someVar;
}
函数myFun将返回"Head"因为存在变量的local版本。
+
+一个程序中有可能具有相同名称的局部变量 和全局变量。在这种情况下,局部变量将会优先于全局变量。 +下面为例: + +```js +var someVar = "Hat"; +function myFun() { + var someVar = "Head"; + return someVar; +} +``` + +函数myFun将会返回"Head",因为局部变量优先级更高。 +
## Instructions -
将一个局部变量添加到myOutfit函数,以使用"sweater"覆盖outerWear的值。
+
+给myOutfit添加一个局部变量来覆盖outerWear的值为"sweater"。 +
## Tests
```yml tests: - - text: 不要更改全局outerWear的值 + - text: 不要修改全局变量outerWear的值。 testString: assert(outerWear === "T-Shirt"); - - text: myOutfit应该返回"sweater" + - text: myOutfit应该返回"sweater"。 testString: assert(myOutfit() === "sweater"); - - text: 不要更改return语句 + - text: 不要修改return语句。 testString: assert(/return outerWear/.test(code)); ``` @@ -47,7 +63,6 @@ function myOutfit() { } myOutfit(); - ``` @@ -59,7 +74,13 @@ myOutfit(); ## Solution
+ ```js -// solution required +var outerWear = "T-Shirt"; +function myOutfit() { + var outerWear = "sweater"; + return outerWear; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/golf-code.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/golf-code.chinese.md index d5ff092f53..c37517cac1 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/golf-code.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/golf-code.chinese.md @@ -2,15 +2,22 @@ id: 5664820f61c48e80c9fa476c title: Golf Code challengeType: 1 -videoUrl: '' -localeTitle: 高尔夫码 +videoUrl: 'https://scrimba.com/c/c9ykNUR' +forumTopicId: 18195 +localeTitle: 高尔夫代码 --- ## Description -
高尔夫比赛中,每个洞都具有par意义,即高尔夫球手为了将球沉入洞中以完成比赛所期望的平均strokes次数。根据你的strokes高出或低于par的距离,有一个不同的昵称。您的函数将通过parstrokes参数。根据此表返回正确的字符串,该表按优先级顺序列出笔划;顶部(最高)到底部(最低):
笔画返回
1 “一杆进洞!”
<= par - 2 “鹰”
par - 1 “小鸟”
平价 “相提并论”
par + 1 “柏忌”
par + 2 “双柏忌”
> = par + 3 “回家!”
parstrokes将始终为数字和正数。为方便起见,我们添加了所有名称的数组。
+
+在高尔夫golf游戏中,每个洞都有自己的标准杆数par,代表着距离。根据你把球打进洞所挥杆的次数strokes,可以计算出你的高尔夫水平。 +函数将会传送 2 个参数,分别是标准杆数par和挥杆次数strokes,根据下面的表格返回正确的水平段位。 +
StrokesReturn
1"Hole-in-one!"
<= par - 2"Eagle"
par - 1"Birdie"
par"Par"
par + 1"Bogey"
par + 2"Double Bogey"
>= par + 3"Go Home!"
+parstrokes必须是数字而且是正数。 +
## Instructions -
+
+
## Tests @@ -18,27 +25,27 @@ localeTitle: 高尔夫码 ```yml tests: - - text: 'golfScore(4, 1)应该返回“Hole-in-one!”' + - text: golfScore(4, 1)应该返回 "Hole-in-one!"。 testString: assert(golfScore(4, 1) === "Hole-in-one!"); - - text: 'golfScore(4, 2)应该返回“Eagle”' + - text: golfScore(4, 2)应该返回 "Eagle"。 testString: assert(golfScore(4, 2) === "Eagle"); - - text: 'golfScore(5, 2)应该返回“Eagle”' + - text: golfScore(5, 2)应该返回 "Eagle"。 testString: assert(golfScore(5, 2) === "Eagle"); - - text: 'golfScore(4, 3)应该返回“Birdie”' + - text: golfScore(4, 3)应该返回 "Birdie"。 testString: assert(golfScore(4, 3) === "Birdie"); - - text: 'golfScore(4, 4)应该返回“Par”' + - text: golfScore(4, 4)应该返回 "Par"。 testString: assert(golfScore(4, 4) === "Par"); - - text: 'golfScore(1, 1)应该返回“Hole-in-one!”' + - text: golfScore(1, 1)应该返回 "Hole-in-one!"。 testString: assert(golfScore(1, 1) === "Hole-in-one!"); - - text: 'golfScore(5, 5)应该返回“Par”' + - text: golfScore(5, 5)应该返回 "Par"。 testString: assert(golfScore(5, 5) === "Par"); - - text: 'golfScore(4, 5)应该返回“Bogey”' + - text: golfScore(4, 5)应该返回 "Bogey"。 testString: assert(golfScore(4, 5) === "Bogey"); - - text: 'golfScore(4, 6)应该返回“Double Bogey”' + - text: golfScore(4, 6)应该返回 "Double Bogey"。 testString: assert(golfScore(4, 6) === "Double Bogey"); - - text: 'golfScore(4, 7)应该返回“Go Home!”' + - text: golfScore(4, 7)应该返回 "Go Home!"。 testString: assert(golfScore(4, 7) === "Go Home!"); - - text: 'golfScore(5, 9)应该返回“Go Home!”' + - text: golfScore(5, 9)应该返回 "Go Home!"。 testString: assert(golfScore(5, 9) === "Go Home!"); ``` @@ -62,7 +69,6 @@ function golfScore(par, strokes) { // Change these values to test golfScore(5, 4); - ``` @@ -74,7 +80,35 @@ golfScore(5, 4); ## Solution
+ ```js -// solution required +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!"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/increment-a-number-with-javascript.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/increment-a-number-with-javascript.chinese.md index a7340239eb..a44248f307 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/increment-a-number-with-javascript.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/increment-a-number-with-javascript.chinese.md @@ -2,28 +2,38 @@ id: 56533eb9ac21ba0edf2244ac title: Increment a Number with JavaScript challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript增加数字 +videoUrl: 'https://scrimba.com/c/ca8GLT9' +forumTopicId: 18201 +localeTitle: 数字递增 --- ## Description -
您可以轻松地增加或添加一个变量与++运算符。 i++;相当于i = i + 1; 注意
整行成为i++; ,消除了对等号的需要。
+
+使用++,我们可以很容易地对变量进行自增或者+1运算。 +i++; +等效于 +i = i + 1; +注意
i++;这种写法,省去了书写=符号的必要。 +
## Instructions -
更改代码以在myVar上使用++运算符。 暗示
了解有关算术运算符的更多信息- 增量(++)
+
+重写代码,使用++来对变量myVar进行自增操作。 +提示
了解更多关于++运算符Arithmetic operators - Increment (++). +
## Tests
```yml tests: - - text: myVar应该等于88 + - text: myVar应该等于88。 testString: assert(myVar === 88); - - text: myVar = myVar + 1;应该改变 - testString: assert(/var\s*myVar\s*=\s*87;\s*\/*.*\s*([+]{2}\s*myVar|myVar\s*[+]{2});/.test(code)); - - text: 使用++运算符 + - text: myVar = myVar + 1;语句应该被修改。 + testString: assert(/var\s*myVar\s*=\s*87;\s*\/*.*\s*myVar\+\+;/.test(code)); + - text: 使用++运算符。 testString: assert(/[+]{2}\s*myVar|myVar\s*[+]{2}/.test(code)); - - text: 不要更改行上方的代码 + - text: 不要修改注释上方的代码。 testString: assert(/var myVar = 87;/.test(code)); ``` @@ -50,7 +60,7 @@ myVar = myVar + 1;
```js -console.info('after the test'); +(function(z){return 'myVar = ' + z;})(myVar); ```
@@ -60,7 +70,10 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myVar = 87; +myVar++; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/initializing-variables-with-the-assignment-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/initializing-variables-with-the-assignment-operator.chinese.md index 77009e8f51..66c189ffe0 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/initializing-variables-with-the-assignment-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/initializing-variables-with-the-assignment-operator.chinese.md @@ -2,22 +2,29 @@ id: 56533eb9ac21ba0edf2244a9 title: Initializing Variables with the Assignment Operator challengeType: 1 -videoUrl: '' +videoUrl: 'https://scrimba.com/c/cWJ4Bfb' +forumTopicId: 301171 localeTitle: 使用赋值运算符初始化变量 --- ## Description -
通常将变量初始化为与声明的同一行中的初始值。 var myVar = 0;创建一个名为myVar的新变量,并为其指定初始值0
+
+通常在声明变量的时候会给变量初始化一个初始值。 +var myVar = 0; +创建一个名为myVar的变量并指定一个初始值0。 +
## Instructions -
使用var定义变量a并将其初始化为值9
+
+通过关键字var定义一个变量a并且给它一个初始值9。 +
## Tests
```yml tests: - - text: 将a初始化为值9 + - text: 你需要初始化a的值为9。 testString: assert(/var\s+a\s*=\s*9\s*/.test(code)); ``` @@ -44,7 +51,7 @@ var ourVar = 19;
```js -console.info('after the test'); +if(typeof a !== 'undefined') {(function(a){return "a = " + a;})(a);} else { (function() {return 'a is undefined';})(); } ```
@@ -54,7 +61,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var a = 9; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/introducing-else-if-statements.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/introducing-else-if-statements.chinese.md index 3ce0c57dae..85e7a293ff 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/introducing-else-if-statements.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/introducing-else-if-statements.chinese.md @@ -2,36 +2,52 @@ id: 56533eb9ac21ba0edf2244db title: Introducing Else If Statements challengeType: 1 -videoUrl: '' -localeTitle: 如果声明引入Else +videoUrl: 'https://scrimba.com/c/caeJ2hm' +forumTopicId: 18206 +localeTitle: 介绍 else if 语句 --- ## Description -
如果您有多个需要解决的条件,可以将if语句与else if语句链接在一起。
if(num> 15){
返回“大于15”;
} else if(num <5){
返回“小于5”;
} else {
返回“5到15之间”;
}
+
+如果你有多个条件语句,你可以通过else if语句把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语句。
+
+使用if else实现同样的效果。 +
## Tests
```yml tests: - - text: 你应该至少有两个else语句 + - text: 你应该至少有两个else表达式。 testString: assert(code.match(/else/g).length > 1); - - text: 你应该至少有两个if语句 + - text: 你应该至少有两个if表达式。 testString: assert(code.match(/if/g).length > 1); - - text: 您应该为每个条件关闭并打开花括号 - testString: assert(code.match(/if\s*\((.+)\)\s*\{[\s\S]+\}\s*else if\s*\((.+)\)\s*\{[\s\S]+\}\s*else\s*\{[\s\S]+\s*\}/)); - - text: testElseIf(0)应返回“小于5” + - text: testElseIf(0)应该返回 "Smaller than 5"。 testString: assert(testElseIf(0) === "Smaller than 5"); - - text: testElseIf(5)应该返回“5到10之间” + - text: testElseIf(5)应该返回 "Between 5 and 10"。 testString: assert(testElseIf(5) === "Between 5 and 10"); - - text: testElseIf(7)应返回“5到10之间” + - text: testElseIf(7)应该返回 "Between 5 and 10"。 testString: assert(testElseIf(7) === "Between 5 and 10"); - - text: testElseIf(10)应返回“5到10之间” + - text: testElseIf(10)应该返回 "Between 5 and 10"。 testString: assert(testElseIf(10) === "Between 5 and 10"); - - text: testElseIf(12)应返回“大于10” + - text: testElseIf(12)应该返回 "Greater than 10"。 + testString: assert(testElseIf(12) === "Greater than 10"); + - text: testElseIf(12) 应该返回 "Greater than 10"。 testString: assert(testElseIf(12) === "Greater than 10"); ``` @@ -70,7 +86,17 @@ testElseIf(7); ## Solution
+ ```js -// solution required +function testElseIf(val) { + if(val > 10) { + return "Greater than 10"; + } else if(val < 5) { + return "Smaller than 5"; + } else { + return "Between 5 and 10"; + } +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/introducing-else-statements.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/introducing-else-statements.chinese.md index 06e9d3777e..1fb965d579 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/introducing-else-statements.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/introducing-else-statements.chinese.md @@ -2,34 +2,48 @@ id: 56533eb9ac21ba0edf2244da title: Introducing Else Statements challengeType: 1 -videoUrl: '' -localeTitle: 介绍其他声明 +videoUrl: 'https://scrimba.com/c/cek4Efq' +forumTopicId: 18207 +localeTitle: 介绍 else 语句 --- ## Description -
if语句的条件为真时,将执行其后面的代码块。当那个条件是假的时候怎么办?通常什么都不会发生。使用else语句,可以执行备用代码块。
if(num> 10){
返回“大于10”;
} else {
返回“10或更少”;
}
+
+当if语句的条件为真,大括号里的代码执行,那如果条件为假呢?正常情况下什么也不会发生。使用else语句,可以执行当条件为假时相应的代码。 + +```js +if (num > 10) { + return "Bigger than 10"; +} else { + return "10 or Less"; +} +``` + +
## Instructions -
if语句组合到单个if/else语句中。
+
+请把多个if语句合并为一个if/else语句。 +
## Tests
```yml tests: - - text: 您应该只在编辑器中有一个if语句 + - text: 你应该只有一个if表达式。 testString: assert(code.match(/if/g).length === 1); - - text: 你应该使用else语句 + - text: 你应该使用一个else表达式。 testString: assert(/else/g.test(code)); - - text: testElse(4)应返回“5或更小” + - text: testElse(4)应该返回 "5 or Smaller"。 testString: assert(testElse(4) === "5 or Smaller"); - - text: testElse(5)应返回“5或更小” + - text: testElse(5)应该返回 "5 or Smaller"。 testString: assert(testElse(5) === "5 or Smaller"); - - text: testElse(6)应该返回“大于5” + - text: testElse(6)应该返回 "Bigger than 5"。 testString: assert(testElse(6) === "Bigger than 5"); - - text: testElse(10)应该返回“大于5” + - text: testElse(10)应该返回 "Bigger than 5"。 testString: assert(testElse(10) === "Bigger than 5"); - - text: 请勿更改行上方或下方的代码。 + - text: 不要修改上面和下面的代码。 testString: assert(/var result = "";/.test(code) && /return result;/.test(code)); ``` @@ -72,7 +86,17 @@ testElse(4); ## Solution
+ ```js -// solution required +function testElse(val) { + var result = ""; + if(val > 5) { + result = "Bigger than 5"; + } else { + result = "5 or Smaller"; + } + return result; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-odd-numbers-with-a-for-loop.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-odd-numbers-with-a-for-loop.chinese.md index ba40fce4d9..1f35bd7def 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-odd-numbers-with-a-for-loop.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-odd-numbers-with-a-for-loop.chinese.md @@ -2,24 +2,41 @@ id: 56104e9e514f539506016a5c title: Iterate Odd Numbers With a For Loop challengeType: 1 -videoUrl: '' -localeTitle: 使用For循环迭代奇数 +videoUrl: 'https://scrimba.com/c/cm8n7T9' +forumTopicId: 18212 +localeTitle: 使用 For 循环遍历数组的奇数 --- ## Description -
For循环不必一次迭代一个循环。通过改变我们的final-expression ,我们可以计算偶数。我们将从i = 0开始并在i < 10循环。我们会增加i的2每个回路与i += 2
var ourArray = [];
for(var i = 0; i <10; i + = 2){
ourArray.push(ⅰ);
}
ourArray现在包含[0,2,4,6,8] 。让我们改变initialization这样我们就可以用奇数来计算。
+
+for循环可以按照我们指定的顺序来迭代,通过更改我们的计数器,我们可以按照偶数顺序来迭代。 +初始化i = 0,当i < 10的时候继续循环。 +i += 2i每次循环之后增加2。 + +```js +var ourArray = []; +for (var i = 0; i < 10; i += 2) { + ourArray.push(i); +} +``` + +循环结束后,ourArray的值为[0,2,4,6,8]。 +改变计数器,这样我们可以用奇数来数。 +
## Instructions -
使用for循环将奇数从1到9推送到myArray
+
+写一个for循环,把从 1 到 9 的奇数添加到myArray。 +
## Tests
```yml tests: - - text: 你应该为此使用for循环。 + - text: 你应该使用for循环。 testString: assert(code.match(/for\s*\(/g).length > 1); - - text: 'myArray应该等于[1,3,5,7,9] 。' + - text: myArray应该等于[1,3,5,7,9]。 testString: assert.deepEqual(myArray, [1,3,5,7,9]); ``` @@ -44,6 +61,7 @@ var myArray = []; // Only change code below this line. + ``` @@ -53,7 +71,7 @@ var myArray = [];
```js -console.info('after the test'); +if(typeof myArray !== "undefined"){(function(){return myArray;})();} ```
@@ -63,7 +81,16 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var ourArray = []; +for (var i = 0; i < 10; i += 2) { + ourArray.push(i); +} +var myArray = []; +for (var i = 1; i < 10; i += 2) { + myArray.push(i); +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.chinese.md index 8485e9cf9b..a56663691e 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.chinese.md @@ -2,37 +2,49 @@ id: 5675e877dbd60be8ad28edc6 title: Iterate Through an Array with a For Loop challengeType: 1 -videoUrl: '' -localeTitle: 使用For循环遍历数组 +videoUrl: 'https://scrimba.com/c/caeR3HB' +forumTopicId: 18216 +localeTitle: 使用 For 循环遍历数组 --- ## Description -
JavaScript中的一个常见任务是遍历数组的内容。一种方法是使用for循环。此代码将数组arr每个元素输出到控制台:
var arr = [10,9,8,7,6];
for(var i = 0; i <arr.length; i ++){
(ARR [I])的console.log;
}
请记住,数组具有从零开始的编号,这意味着数组的最后一个索引是长度 - 1.我们对此循环的条件i < arr.length ,当i长度为1时停止。
+
+迭代输出一个数组的每个元素是 JavaScript 中的常见需求,for循环可以做到这一点。下面的代码将输出数组 arr的每个元素到控制台: + +```js +var arr = [10, 9, 8, 7, 6]; +for (var i = 0; i < arr.length; i++) { + console.log(arr[i]); +} +``` + +记住数组的索引从零开始的,这意味着数组的最后一个元素的下标是:数组的长度 -1。我们这个循环的 条件i < arr.length,当i的值为 长度 -1 的时候循环就停止了。在这个例子中,最后一个循环是 i === 4,也就是说,当i的值等于arr.length时,结果输出 6。 +
## Instructions -
声明并将变量total初始化为0 。使用for循环将myArr数组的每个元素的值添加到total
+
+声明并初始化一个变量total0。使用for循环,使得total的值为myArr的数组中的每个元素的值的总和。 +
## Tests
```yml tests: - - text: 应声明total并初始化为0 - testString: assert(code.match(/(var|let|const)\s*?total\s*=\s*0.*?;?/)); - - text: total应该等于20 + - text: total应该被声明, 并且初始化值为 0。 + testString: assert(code.match(/var.*?total\s*=\s*0.*?;/)); + - text: total应该等于 20。 testString: assert(total === 20); - - text: 您应该使用for循环来遍历myArr + - text: 你应该使用for循环在myArr中遍历。 testString: assert(code.match(/for\s*\(/g).length > 1 && code.match(/myArr\s*\[/)); - - text: 不要直接将total设置为20 - testString: assert(!code.replace(/\s/g, '').match(/total[=+-]0*[1-9]+/gm)); - + - text: 不能直接把total设置成 20。 + testString: assert(!code.match(/total[\s\+\-]*=\s*(\d(?!\s*[;,])|[1-9])/g)); ```
## Challenge Seed
-
```js @@ -53,12 +65,12 @@ var myArr = [ 2, 3, 4, 5, 6];
- ### After Test
```js -console.info('after the test'); +(function(){if(typeof total !== 'undefined') { return "total = " + total; } else { return "total is undefined";}})() + ```
@@ -69,6 +81,19 @@ console.info('after the test');
```js -// solution required +var ourArr = [ 9, 10, 11, 12]; +var ourTotal = 0; + +for (var i = 0; i < ourArr.length; i++) { + ourTotal += ourArr[i]; +} + +var myArr = [ 2, 3, 4, 5, 6]; +var total = 0; + +for (var i = 0; i < myArr.length; i++) { + total += myArr[i]; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-do...while-loops.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-do...while-loops.chinese.md index 980fa1df66..4ad77e02e5 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-do...while-loops.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-do...while-loops.chinese.md @@ -2,15 +2,57 @@ id: 5a2efd662fb457916e1fe604 title: Iterate with JavaScript Do...While Loops challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript迭代...循环 +videoUrl: 'https://scrimba.com/c/cDqWGcp' +forumTopicId: 301172 +localeTitle: do...while 循环 --- ## Description -
您可以使用循环多次运行相同的代码。你将学习的下一个类型的循环称为“ do...while ”循环,因为它首先将“ do ”循环内部代码的一次传递,无论如何,然后它运行“ while ”指定条件为真一旦这种情况不再真实就停止。我们来看一个例子。
var ourArray = [];
var i = 0;
做{
ourArray.push(ⅰ);
我++;
} while(i <5);
这与任何其他类型的循环一样正常,结果数组看起来像[0, 1, 2, 3, 4] 。然而,什么使得do...while与其他循环不同,但是当条件在第一次检查时失败时它的行为如何。让我们看看这个在行动。这是一个常规的while循环,只要i < 5 ,它就会在循环中运行代码。
var ourArray = [];
var i = 5;
而(i <5){
ourArray.push(ⅰ);
我++;
}
请注意,我们将i的值初始化为5.当我们执行下一行时,我们注意到i不小于5.所以我们不执行循环内的代码。结果是ourArray最终没有添加任何内容,因此当上面示例中的所有代码完成运行时,它仍然看起来像这个[] 。现在,看一下do...while循环。
var ourArray = [];
var i = 5;
做{
ourArray.push(ⅰ);
我++;
} while(i <5);
在这种情况下,我们将i的值初始化为5,就像我们使用while循环一样。当我们到达下一行时,没有检查i的值,所以我们转到花括号内的代码并执行它。我们将在数组中添加一个元素并在进行条件检查之前递增i 。然后,当我们检查i < 5看到i现在是6,这不符合条件检查。所以我们退出循环并完成。在上面的例子的末尾, ourArray的值是[5] 。本质上, do...while循环确保循环内的代码至少运行一次。让我们尝试通过将值推送到数组来实现do...while循环。
+
+这一节我们将要学习的是do...while循环,它会先执行do里面的代码,如果while表达式为真则重复执行,反之则停止执行。我们来看一个例子。 + +```js +var ourArray = []; +var i = 0; +do { + ourArray.push(i); + i++; +} while (i < 5); +``` + +这看起来和其他循环语句差不多,返回的结果是[0, 1, 2, 3, 4]do...while与其他循环不同点在于,初始条件为假时的表现,让我们通过实际的例子来看看。 +这是一个普通的 while 循环,只要i < 5,它就会在循环中运行代码。 + +```js +var ourArray = []; +var i = 5; +while (i < 5) { + ourArray.push(i); + i++; +} +``` + +注意,我们首先将i的值初始化为 5。执行下一行时,注意到i不小于 5,循环内的代码将不会执行。所以ourArray最终没有添加任何内容,因此示例中的所有代码执行完时,ourArray仍然是[]。 +现在,看一下do...while循环。 + +```js +var ourArray = []; +var i = 5; +do { + ourArray.push(i); + i++; +} while (i < 5); +``` + +在这里,和使用 while 循环时一样,我们将i的值初始化为 5。执行下一行时,没有检查i的值,直接执行花括号内的代码。数组会添加一个元素,并在进行条件检查之前递增i。然后,在条件检查时因为i等于 6 不符合条件i < 5,所以退出循环。最终ourArray的值是[5]。 +本质上,do...while循环确保循环内的代码至少运行一次。 +让我们通过do...while循环将值添加到数组中。 +
## Instructions -
将代码中的while循环更改为do...while循环,以便循环将数字10推送到myArray ,当代码完成运行时, i将等于11
+
+将代码中的while循环更改为do...while循环,实现数字 10 添加到myArray中,代码执行完时,i等于11。 +
## Tests
@@ -19,11 +61,10 @@ localeTitle: 使用JavaScript迭代...循环 tests: - text: 你应该使用do...while循环。 testString: assert(code.match(/do/g)); - - text: 'myArray应该等于[10] 。' + - text: myArray应该等于[10]。 testString: assert.deepEqual(myArray, [10]); - - text: i应该等于11 - testString: assert.equal(i, 11); - + - text: i应该等于11。 + testString: assert.deepEqual(i, 11); ```
@@ -38,13 +79,11 @@ tests: var myArray = []; var i = 10; -// Only change code below this line. - +// Only change code below this line while (i < 5) { myArray.push(i); i++; } - ``` @@ -54,7 +93,7 @@ while (i < 5) {
```js -console.info('after the test'); +if(typeof myArray !== "undefined"){(function(){return myArray;})();} ```
@@ -64,7 +103,14 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myArray = []; +var i = 10; +do { + myArray.push(i); + i++; +} while (i < 5) ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.chinese.md index cfa0d6e543..d9d22ba5b9 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.chinese.md @@ -2,24 +2,45 @@ id: cf1111c1c11feddfaeb5bdef title: Iterate with JavaScript For Loops challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript迭代循环 +videoUrl: 'https://scrimba.com/c/c9yNVCe' +forumTopicId: 18219 +localeTitle: for 循环 --- ## Description -
您可以使用循环多次运行相同的代码。最常见的JavaScript循环类型称为“ for loop ”,因为它“运行”特定次数。 For循环用三个可选表达式声明,用分号分隔: for ([initialization]; [condition]; [final-expression]) initialization语句仅在循环开始之前执行一次。它通常用于定义和设置循环变量。 condition语句在每次循环迭代开始时进行计算,并且只要计算结果为true就会继续。当迭代开始时conditionfalse时,循环将停止执行。这意味着如果conditionfalse开头,则循环将永远不会执行。 final-expression在每次循环迭代结束时执行,在下一次condition检查之前执行,通常用于递增或递减循环计数器。在下面的示例中,我们使用i = 0初始化并迭代,而条件i < 5为真。我们将在每个循环迭代中将i递增1 ,并使用i++作为final-expression
var ourArray = [];
for(var i = 0; i <5; i ++){
ourArray.push(ⅰ);
}
ourArray现在包含[0,1,2,3,4]
+
+你可以使用循环多次执行相同的代码。 +JavaScript 中最常见的循环就是 “for循环”。 +for循环中的三个表达式用分号隔开: +for ([初始化]; [条件判断]; [计数器]) +初始化语句只会在执行循环开始之前执行一次。它通常用于定义和设置你的循环变量。 +条件判断语句会在每一轮循环的开始执行,只要条件判断为true就会继续执行循环。当条件为false的时候,循环将停止执行。这意味着,如果条件在一开始就为false,这个循环将不会执行。 +计数器是在每一轮循环结束时执行,通常用于递增或递减。 +在下面的例子中,先初始化i = 0,条件i < 5为真,进入第一次循环,执行大括号里的代码,第一次循环结束。递增i的值,条件判断,就这样依次执行下去,直到条件判断为假,整个循环结束。 + +```js +var ourArray = []; +for (var i = 0; i < 5; i++) { + ourArray.push(i); +} +``` + +最终ourArray的值为[0,1,2,3,4]. +
## Instructions -
使用for循环将值1到5推送到myArray
+
+使用for循环把从 1 到 5 添加进myArray中。 +
## Tests
```yml tests: - - text: 你应该为此使用for循环。 + - text: 你应该使用for循环。 testString: assert(code.match(/for\s*\(/g).length > 1); - - text: 'myArray应该等于[1,2,3,4,5] 。' + - text: myArray应该等于[1,2,3,4,5]。 testString: assert.deepEqual(myArray, [1,2,3,4,5]); ``` @@ -44,6 +65,7 @@ var myArray = []; // Only change code below this line. + ``` @@ -53,7 +75,7 @@ var myArray = [];
```js -console.info('after the test'); +if (typeof myArray !== "undefined"){(function(){return myArray;})();} ```
@@ -63,7 +85,16 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var ourArray = []; +for (var i = 0; i < 5; i++) { + ourArray.push(i); +} +var myArray = []; +for (var i = 1; i < 6; i++) { + myArray.push(i); +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-while-loops.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-while-loops.chinese.md index c290a98a51..463995de7b 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-while-loops.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-while-loops.chinese.md @@ -2,15 +2,34 @@ id: cf1111c1c11feddfaeb1bdef title: Iterate with JavaScript While Loops challengeType: 1 -videoUrl: '' -localeTitle: 在循环时使用JavaScript进行迭代 +videoUrl: 'https://scrimba.com/c/c8QbnCM' +forumTopicId: 18220 +localeTitle: while 循环 --- ## Description -
您可以使用循环多次运行相同的代码。我们将学习的第一种类型的循环称为“ while ”循环,因为它在“while”运行时指定的条件为true,并且一旦该条件不再为真就停止。
var ourArray = [];
var i = 0;
而(i <5){
ourArray.push(ⅰ);
我++;
}
让我们尝试通过将值推送到数组来实现while循环。
+
+你可以使用循环多次执行相同的代码。 +我们将学习的第一种类型的循环称为 "while" 循环,因为它规定,当 "while" 条件为真,循环才会执行,反之不执行。 + +```js +var ourArray = []; +var i = 0; +while(i < 5) { + ourArray.push(i); + i++; +} +``` + +在上面的代码里,while 循环执行 5 次把 0 到 4 的数字添加到 ourArray 数组里。 + +让我们通过 while 循环将值添加到数组中。 +
## Instructions -
使用while循环将数字0到4推送到myArray
+
+通过一个while循环,把从 0 到 4 的值添加到myArray中。 +
## Tests
@@ -19,8 +38,8 @@ localeTitle: 在循环时使用JavaScript进行迭代 tests: - text: 你应该使用while循环。 testString: assert(code.match(/while/g)); - - text: 'myArray应该等于[0,1,2,3,4] 。' - testString: assert.deepEqual(myArray, [5,4,3,2,1,0]); + - text: myArray应该等于[0,1,2,3,4]。 + testString: assert.deepEqual(myArray, [0,1,2,3,4]); ``` @@ -37,6 +56,7 @@ var myArray = []; // Only change code below this line. + ``` @@ -46,7 +66,7 @@ var myArray = [];
```js -console.info('after the test'); +if(typeof myArray !== "undefined"){(function(){return myArray;})();} ```
@@ -56,7 +76,14 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myArray = []; +var i = 5; +while(i >= 0) { + myArray.push(i); + i--; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/local-scope-and-functions.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/local-scope-and-functions.chinese.md index 716bdb4cf9..032e75ad80 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/local-scope-and-functions.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/local-scope-and-functions.chinese.md @@ -2,25 +2,44 @@ id: 56533eb9ac21ba0edf2244bf title: Local Scope and Functions challengeType: 1 -videoUrl: '' -localeTitle: 本地范围和功能 +videoUrl: 'https://scrimba.com/c/cd62NhM' +forumTopicId: 18227 +localeTitle: 局部作用域和函数 --- ## Description -
在函数内声明的变量,以及函数参数都具有局部范围。这意味着,它们仅在该功能中可见。这是一个函数myTest带有一个名为loc的局部变量。
function myTest(){
var loc =“foo”;
的console.log(LOC);
}
MYTEST(); //记录“foo”
的console.log(LOC); // loc未定义
loc未在函数外定义。
+
+在一个函数内声明的变量,以及该函数的参数都是局部变量,意味着它们只在该函数内可见。 +这是在函数myTest内声明局部变量loc的例子: + +```js +function myTest() { + var loc = "foo"; + console.log(loc); +} +myTest(); // logs "foo" +console.log(loc); // loc is not defined +``` + +在函数外,loc是未定义的。 +
## Instructions -
myLocalScope声明一个局部变量myVar 。运行测试,然后按照编辑器中注释的说明进行操作。 暗示
如果您遇到问题,刷新页面可能会有所帮助。
+
+在函数myFunction内部声明一个局部变量myVar,并删除外部的 console.log。 +提示:
如果你遇到了问题,可以先尝试刷新页面。 +
## Tests
```yml tests: - - text: 没有全局myVar变量 + - text: 未找到全局的myVar变量。 testString: assert(typeof myVar === 'undefined'); - - text: 添加本地myVar变量 - testString: assert(/function\s+myLocalScope\s*\(\s*\)\s*\{\s[\s\S]+\s*var\s*myVar\s*(\s*|=[\s\S]+)\s*;[\s\S]+}/.test(code)); + - text: 需要定义局部的myVar变量。 + testString: assert(/var\s+myVar/.test(code)); + ``` @@ -80,7 +99,8 @@ function uncapture() {
```js -console.info('after the test'); +typeof myLocalScope === 'function' && (capture(), myLocalScope(), uncapture()); +(function() { return logOutput || "console.log never called"; })(); ```
@@ -90,7 +110,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +function myLocalScope() { + 'use strict'; + + var myVar; + console.log(myVar); +} +myLocalScope(); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/logical-order-in-if-else-statements.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/logical-order-in-if-else-statements.chinese.md index 0fd60a9eb6..82a7cff51d 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/logical-order-in-if-else-statements.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/logical-order-in-if-else-statements.chinese.md @@ -2,26 +2,68 @@ id: 5690307fddb111c6084545d7 title: Logical Order in If Else Statements challengeType: 1 -videoUrl: '' -localeTitle: 如果其他陈述中的逻辑顺序 +videoUrl: 'https://scrimba.com/c/cwNvMUV' +forumTopicId: 18228 +localeTitle: if else 语句中的逻辑顺序 --- ## Description -
订单在ifelse if语句中很重要。该函数从上到下执行,因此您需要注意首先出现的语句。以这两个函数为例。这是第一个:
function foo(x){
if(x <1){
返回“少于一个”;
} else if(x <2){
返回“少于两个”;
} else {
返回“大于或等于2”;
}
}
第二个只是切换语句的顺序:
功能栏(x){
if(x <2){
返回“少于两个”;
} else if(x <1){
返回“少于一个”;
} else {
返回“大于或等于2”;
}
}
虽然如果我们将数字传递给两者,这两个函数看起来几乎相同但我们得到不同的输出。
foo(0)//“不到一个”
bar(0)//“少于两个”
+
+ifelse if语句中代码的执行顺序是很重要的。 +在条件判断语句中,代码的执行顺序是从上到下,所以你需要考虑清楚先执行哪一句,后执行哪一句。 +这有两个例子。 +第一个例子: + +```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"; + } +} +``` + +第二个例子更改了代码的执行顺序: + +```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"; + } +} +``` + +这两个函数看起来几乎一模一样,我们传一个值进去看看它们有什么区别。 + +```js +foo(0) // "Less than one" +bar(0) // "Less than two" +``` + +
## Instructions -
更改函数中的逻辑顺序,以便在所有情况下都返回正确的语句。
+
+更改函数的逻辑顺序以便通过所有的测试用例。 +
## Tests
```yml tests: - - text: orderMyLogic(4)应返回“小于5” + - text: orderMyLogic(4)应该返回 "Less than 5"。 testString: assert(orderMyLogic(4) === "Less than 5"); - - text: orderMyLogic(6)应该返回“少于10” + - text: orderMyLogic(6)应该返回 "Less than 10"。 testString: assert(orderMyLogic(6) === "Less than 10"); - - text: orderMyLogic(11)应该返回“大于或等于10” + - text: orderMyLogic(11)应该返回 "Greater than or equal to 10"。 testString: assert(orderMyLogic(11) === "Greater than or equal to 10"); ``` @@ -46,7 +88,6 @@ function orderMyLogic(val) { // Change this value to test orderMyLogic(7); - ``` @@ -58,7 +99,17 @@ orderMyLogic(7); ## Solution
+ ```js -// solution required +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"; + } +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-pop.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-pop.chinese.md index 09d273bc9c..2a7d7c4df1 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-pop.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-pop.chinese.md @@ -2,26 +2,41 @@ id: 56bbb991ad1ed5201cd392cc title: Manipulate Arrays With pop() challengeType: 1 -videoUrl: '' -localeTitle: 使用pop()操作数组 +videoUrl: 'https://scrimba.com/c/cRbVZAB' +forumTopicId: 18236 +localeTitle: 使用 pop() 操作数组 --- ## Description -
更改数组中数据的另一种方法是使用.pop()函数。 .pop()用于“弹出”数组末尾的值。我们可以通过将其赋值给变量来存储这个“弹出”值。换句话说, .pop()从数组中删除最后一个元素并返回该元素。任何类型的条目都可以从数组“弹出” - 数字,字符串,甚至嵌套数组。
var threeArr = [1, 4, 6];
var oneDown = threeArr.pop();
console.log(oneDown); // Returns 6
console.log(threeArr); // Returns [1, 4]
+
+改变数组中数据的另一种方法是用.pop()函数。 +.pop()函数用来“抛出”一个数组末尾的值。我们可以把这个“抛出”的值赋给一个变量存储起来。换句话说就是.pop()函数移除数组末尾的元素并返回这个元素。 +数组中任何类型的元素(数值,字符串,甚至是数组)可以被“抛出来” 。 + +```js +var threeArr = [1, 4, 6]; +var oneDown = threeArr.pop(); +console.log(oneDown); // Returns 6 +console.log(threeArr); // Returns [1, 4] +``` + +
## Instructions -
使用.pop()函数从myArray删除最后一项,将“弹出”值分配给removedFromMyArray
+
+使用.pop()函数移除myArray中的最后一个元素,并且把“抛出”的值赋给removedFromMyArray。 +
## Tests
```yml tests: - - text: 'myArray应该只包含[["John", 23]] 。' + - text: myArray应该只包含[["John", 23]]。 testString: assert((function(d){if(d[0][0] == 'John' && d[0][1] === 23 && d[1] == undefined){return true;}else{return false;}})(myArray)); - - text: 在myArray上使用pop() + - text: 对myArray使用pop()函数。 testString: assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code)); - - text: 'removedFromMyArray应该只包含["cat", 2] 。' + - text: removedFromMyArray应该只包含["cat", 2]。 testString: assert((function(d){if(d[0] == 'cat' && d[1] === 2 && d[2] == undefined){return true;}else{return false;}})(removedFromMyArray)); ``` @@ -45,6 +60,7 @@ var myArray = [["John", 23], ["cat", 2]]; // Only change code below this line. var removedFromMyArray; + ``` @@ -54,7 +70,7 @@ var removedFromMyArray;
```js -console.info('after the test'); +(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray); ```
@@ -64,7 +80,10 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myArray = [["John", 23], ["cat", 2]]; +var removedFromMyArray = myArray.pop(); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-push.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-push.chinese.md index 52f5820ab0..60c4d5cb63 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-push.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-push.chinese.md @@ -2,22 +2,35 @@ id: 56bbb991ad1ed5201cd392cb title: Manipulate Arrays With push() challengeType: 1 -videoUrl: '' -localeTitle: 用push()操纵数组 +videoUrl: 'https://scrimba.com/c/cnqmVtJ' +forumTopicId: 18237 +localeTitle: 使用 push() 操作数组 --- ## Description -
将数据附加到数组末尾的简单方法是通过push()函数。 .push()接受一个或多个参数并将它们“推”到数组的末尾。
var arr = [1,2,3];
arr.push(4);
// arr现在是[1,2,3,4]
+
+一个简单的方法将数据添加到一个数组的末尾是通过push()函数。 +.push()接受一个或多个参数,并把它“推”入到数组的末尾。 + +```js +var arr = [1,2,3]; +arr.push(4); +// arr is now [1,2,3,4] +``` + +
## Instructions -
["dog", 3]推到myArray变量的末尾。
+
+把["dog", 3]“推”入到myArray变量的末尾。 +
## Tests
```yml tests: - - text: 'myArray现在应该等于[["John", 23], ["cat", 2], ["dog", 3]] 。' + - text: myArray应该等于[["John", 23], ["cat", 2], ["dog", 3]]。 testString: 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)); ``` @@ -40,6 +53,7 @@ var myArray = [["John", 23], ["cat", 2]]; // Only change code below this line. + ``` @@ -49,7 +63,7 @@ var myArray = [["John", 23], ["cat", 2]];
```js -console.info('after the test'); +(function(z){return 'myArray = ' + JSON.stringify(z);})(myArray); ```
@@ -59,7 +73,10 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myArray = [["John", 23], ["cat", 2]]; +myArray.push(["dog",3]); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-shift.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-shift.chinese.md index 1e9876e475..f64114beee 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-shift.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-shift.chinese.md @@ -2,24 +2,30 @@ id: 56bbb991ad1ed5201cd392cd title: Manipulate Arrays With shift() challengeType: 1 -videoUrl: '' -localeTitle: 使用shift()操纵数组 +videoUrl: 'https://scrimba.com/c/cRbVETW' +forumTopicId: 18238 +localeTitle: 使用 shift() 操作数组 --- ## Description -
pop()总是删除数组的最后一个元素。如果你想删除第一个怎么办?这就是.shift()用武之地。它就像.pop()一样工作,除了它删除了第一个元素而不是最后一个元素。
+
+pop()函数用来移出数组中最后一个元素。如果想要移出第一个元素要怎么办呢? +这就是.shift()的用武之地。它的工作原理就像.pop(),但它移除的是第一个元素,而不是最后一个。 +
## Instructions -
使用.shift()函数从myArray删除第一项,将“shift off”值分配给removedFromMyArray
+
+使用.shift()函数移出myArray中的第一项,并把“移出”的值赋给removedFromMyArray。 +
## Tests
```yml tests: - - text: 'myArray现在应该等于[["dog", 3]] 。' + - text: myArray应该等于[["dog", 3]]。 testString: assert((function(d){if(d[0][0] == 'dog' && d[0][1] === 3 && d[1] == undefined){return true;}else{return false;}})(myArray)); - - text: 'removedFromMyArray应该包含["John", 23] 。' + - text: removedFromMyArray应该包含["John", 23]。 testString: assert((function(d){if(d[0] == 'John' && d[1] === 23 && typeof removedFromMyArray === 'object'){return true;}else{return false;}})(removedFromMyArray)); ``` @@ -43,6 +49,7 @@ var myArray = [["John", 23], ["dog", 3]]; // Only change code below this line. var removedFromMyArray; + ``` @@ -52,7 +59,7 @@ var removedFromMyArray;
```js -console.info('after the test'); +(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray); ```
@@ -62,7 +69,12 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myArray = [["John", 23], ["dog", 3]]; + +// Only change code below this line. +var removedFromMyArray = myArray.shift(); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-unshift.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-unshift.chinese.md index d96fb44e18..7a8e7c9629 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-unshift.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-unshift.chinese.md @@ -2,22 +2,28 @@ id: 56bbb991ad1ed5201cd392ce title: Manipulate Arrays With unshift() challengeType: 1 -videoUrl: '' -localeTitle: 使用unshift操作数组() +videoUrl: 'https://scrimba.com/c/ckNDESv' +forumTopicId: 18239 +localeTitle: 使用 unshift() 操作数组 --- ## Description -
不仅可以shift元件关闭的阵列的开头的,也可以unshift元素添加到数组的开始,即在阵列的前添加元素。 .unshift()工作方式与.push()完全相同,但是不是在数组的末尾添加元素, unshift()会在数组的开头添加元素。
+
+你不仅可以shift(移出)数组中的第一个元素,你也可以unshift(移入)一个元素到数组的头部。 +.unshift()函数用起来就像.push()函数一样, 但不是在数组的末尾添加元素,而是在数组的头部添加元素。 +
## Instructions -
使用unshift()["Paul",35]添加到myArray变量的开头。
+
+使用unshift()函数把["Paul",35]加入到myArray的头部。 +
## Tests
```yml tests: - - text: 'myArray现在应该有[[“Paul”,35],[“dog”,3]]。' + - text: myArray应该包含[["Paul", 35], ["dog", 3]]。 testString: 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)); ``` @@ -42,6 +48,7 @@ myArray.shift(); // Only change code below this line. + ``` @@ -51,7 +58,7 @@ myArray.shift();
```js -console.info('after the test'); +(function(y, z){return 'myArray = ' + JSON.stringify(y);})(myArray); ```
@@ -61,7 +68,11 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myArray = [["John", 23], ["dog", 3]]; +myArray.shift(); +myArray.unshift(["Paul", 35]); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulating-complex-objects.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulating-complex-objects.chinese.md index 8e6f2e2152..4c93752673 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulating-complex-objects.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/manipulating-complex-objects.chinese.md @@ -2,38 +2,80 @@ id: 56533eb9ac21ba0edf2244cb title: Manipulating Complex Objects challengeType: 1 -videoUrl: '' -localeTitle: 操纵复杂对象 +videoUrl: 'https://scrimba.com/c/c9yNMfR' +forumTopicId: 18208 +localeTitle: 操作复杂对象 --- ## Description -
有时您可能希望将数据存储在灵活的数据结构中 。 JavaScript对象是处理灵活数据的一种方法。它们允许字符串数字布尔值数组函数对象的任意组合。这是一个复杂数据结构的示例:
var ourMusic = [
{
“艺术家”:“Daft Punk”,
“标题”:“家庭作业”,
“release_year”:1997年,
“格式”:[
“光盘”,
“盒式”
“LP”
]
“黄金”:是的
}
]。
这是一个包含一个对象的数组。该对象具有关于专辑的各种元数据 。它还有一个嵌套的"formats"数组。如果要添加更多专辑记录,可以通过向顶级数组添加记录来完成此操作。对象将数据保存在属性中,该属性具有键值格式。在上面的示例中, "artist": "Daft Punk"是具有"artist"键和"Daft Punk"值的属性。 JavaScript Object NotationJSON是用于存储数据的相关数据交换格式。
{
“艺术家”:“Daft Punk”,
“标题”:“家庭作业”,
“release_year”:1997年,
“格式”:[
“光盘”,
“盒式”
“LP”
]
“黄金”:是的
}
注意
除非它是数组中的最后一个对象,否则您需要在数组中的每个对象后面放置一个逗号。
+
+有时你可能希望将数据存储在灵活的数据结构中。JavaScript 对象是处理灵活数据的一种方法。它可以储存字符串数字布尔值函数,和对象以及这些值的任意组合。 +这是一个复杂数据结构的示例: + +```js +var ourMusic = [ + { + "artist": "Daft Punk", + "title": "Homework", + "release_year": 1997, + "formats": [ + "CD", + "Cassette", + "LP" + ], + "gold": true + } +]; +``` + +这是一个对象数组,并且对象有各种关于专辑的 详细信息。它也有一个嵌套的formats的数组。附加专辑记录可以被添加到数组的最上层。 +对象将数据以一种键-值对的形式保存。在上面的示例中,"artist": "Daft Punk"是一个具有"artist"键和"Daft Punk"值的属性。 +JavaScript Object Notation 简称JSON是用于存储数据的相关数据交换格式。 + +```json +{ + "artist": "Daft Punk", + "title": "Homework", + "release_year": 1997, + "formats": [ + "CD", + "Cassette", + "LP" + ], + "gold": true +} +``` + +提示
数组中有多个 JSON 对象的时候,对象与对象之间要用逗号隔开。 +
## Instructions -
将新相册添加到myMusic阵列。添加artisttitle字符串, release_year数字和formats字符串数组。
+
+添加一个新专辑到myMusic的JSON对象。添加artisttitle字符串,release_year数字和formats字符串数组。 +
## Tests
```yml tests: - - text: myMusic应该是一个数组 + - text: myMusic应该是一个数组。 testString: assert(Array.isArray(myMusic)); - - text: myMusic应该至少有两个元素 + - text: myMusic应该至少包含两个元素。 testString: assert(myMusic.length > 1); - - text: 'myMusic[1]应该是一个对象' + - text: myMusic[1]应该是一个对象。 testString: assert(typeof myMusic[1] === 'object'); - - text: 'myMusic[1]应该至少有4个属性' + - text: myMusic[1]至少要包含四个属性。 testString: assert(Object.keys(myMusic[1]).length > 3); - - text: 'myMusic[1]应该包含一个artist属性,它是一个字符串' + - text: myMusic[1]应该包含一个类型为字符串的artist的属性。 testString: assert(myMusic[1].hasOwnProperty('artist') && typeof myMusic[1].artist === 'string'); - - text: 'myMusic[1]应该包含一个title属性,它是一个字符串' + - text: myMusic[1]应该包含一个类型为字符串的title的属性。 testString: assert(myMusic[1].hasOwnProperty('title') && typeof myMusic[1].title === 'string'); - - text: 'myMusic[1]应该包含一个release_year属性,它是一个数字' + - text: myMusic[1]应该包含一个类型为数字的release_year 应该包含一个类型为数字的属性。 testString: assert(myMusic[1].hasOwnProperty('release_year') && typeof myMusic[1].release_year === 'number'); - - text: 'myMusic[1]应该包含一个formats属性,它是一个数组' + - text: myMusic[1]应该包含一个类型为数组的formats属性。 testString: assert(myMusic[1].hasOwnProperty('formats') && Array.isArray(myMusic[1].formats)); - - text: formats应该是一个至少包含两个元素的字符串数组 + - text: formats应该是一个至少包含两个字符串元素的数组。 testString: assert(myMusic[1].formats.every(function(item) { return (typeof item === "string")}) && myMusic[1].formats.length > 1); ``` @@ -70,7 +112,7 @@ var myMusic = [
```js -console.info('after the test'); +(function(x){ if (Array.isArray(x)) { return JSON.stringify(x); } return "myMusic is not an array"})(myMusic); ```
@@ -80,7 +122,31 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var 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", + ] + } +]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/modify-array-data-with-indexes.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/modify-array-data-with-indexes.chinese.md index 8fa94bee2b..9a3e9fd52c 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/modify-array-data-with-indexes.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/modify-array-data-with-indexes.chinese.md @@ -2,24 +2,37 @@ id: cf1111c1c11feddfaeb8bdef title: Modify Array Data With Indexes challengeType: 1 -videoUrl: '' -localeTitle: 使用索引修改数组数据 +videoUrl: 'https://scrimba.com/c/czQM4A8' +forumTopicId: 18241 +localeTitle: 通过索引修改数组中的数据 --- ## Description -
与字符串不同,数组的条目是可变的 ,可以自由更改。
var ourArray = [50,40,30];
ourArray [0] = 15; //等于[15,40,30]
注意
数组名称和方括号之间不应有任何空格,如array [0] 。尽管JavaScript能够正确处理,但这可能会让其他程序员在阅读代码时感到困惑。
+
+与字符串的数据不可变不同,数组的数据是可变的,并且可以自由地改变。 +示例 + +```js +var ourArray = [50,40,30]; +ourArray[0] = 15; // equals [15,40,30] +``` + +提示
数组名称和方括号之间不应有任何空格,如array [0]尽管 JavaScript 能够正确处理,但可能会让看你代码的其他程序员感到困惑。 +
## Instructions -
将存储在myArray索引0处的数据修改为值45
+
+修改数组myArray中索引0上的值为45。 +
## Tests
```yml tests: - - text: 'myArray现在应该是[45,64,99]。' + - text: myArray的值应该 [45,64,99]。 testString: assert((function(){if(typeof myArray != 'undefined' && myArray[0] == 45 && myArray[1] == 64 && myArray[2] == 99){return true;}else{return false;}})()); - - text: 您应该使用正确的索引来修改myArray的值。 + - text: 你应该使用正确的索引修改myArray的值。 testString: assert((function(){if(code.match(/myArray\[0\]\s*=\s*/g)){return true;}else{return false;}})()); ``` @@ -41,6 +54,7 @@ var myArray = [18,64,99]; // Only change code below this line. + ``` @@ -50,7 +64,7 @@ var myArray = [18,64,99];
```js -console.info('after the test'); +if(typeof myArray !== "undefined"){(function(){return myArray;})();} ```
@@ -60,7 +74,10 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myArray = [18,64,99]; +myArray[0] = 45; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/multiple-identical-options-in-switch-statements.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/multiple-identical-options-in-switch-statements.chinese.md index 078ae74fe9..a87e25b73f 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/multiple-identical-options-in-switch-statements.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/multiple-identical-options-in-switch-statements.chinese.md @@ -2,42 +2,62 @@ id: 56533eb9ac21ba0edf2244df title: Multiple Identical Options in Switch Statements challengeType: 1 -videoUrl: '' -localeTitle: 交换机语句中的多个相同选项 +videoUrl: 'https://scrimba.com/c/cdBKWCV' +forumTopicId: 18242 +localeTitle: 在 Switch 语句添加多个相同选项 --- ## Description -
如果从switch语句的case省略了break语句,则会执行以下case语句,直到遇到break 。如果您有多个具有相同输出的输入,则可以在switch语句中表示它们,如下所示:
switch(val){
情况1:
案例2:
案例3:
result =“1,2或3”;
打破;
案例4:
result =“4 alone”;
}
1,2和3的情况都会产生相同的结果。
+
+如果你忘了给switch的每一条case添加break,那么直到遇见break为止,后续的case会一直执行。如果你想为多个不同的输入设置相同的结果,可以这样写: + +```js +switch(val) { + case 1: + case 2: + case 3: + result = "1, 2, or 3"; + break; + case 4: + result = "4 alone"; +} +``` + +这样,1、2、3 都会有相同的结果。 +
## Instructions -
写一个switch语句来设置以下范围的answer
1-3 - “低”
4-6 - “中”
7-9 - “高”
您需要为范围中的每个数字都有一个case语句。
+
+请写一个switch语句,根据输入的val的范围得出对应的answer
1-3 - "Low"
4-6 - "Mid"
7-9 - "High" +提示:
你的case应基于范围中的每一个数字编写。 +
## Tests
```yml tests: - - text: sequentialSizes(1)应返回“Low” + - text: sequentialSizes(1)应该返回 "Low"。 testString: assert(sequentialSizes(1) === "Low"); - - text: sequentialSizes(2)应该返回“Low” + - text: sequentialSizes(2)应该返回 "Low"。 testString: assert(sequentialSizes(2) === "Low"); - - text: sequentialSizes(3)应返回“Low” + - text: sequentialSizes(3)应该返回 "Low"。 testString: assert(sequentialSizes(3) === "Low"); - - text: sequentialSizes(4)应返回“Mid” + - text: sequentialSizes(4)应该返回 "Mid"。 testString: assert(sequentialSizes(4) === "Mid"); - - text: sequentialSizes(5)应返回“Mid” + - text: sequentialSizes(5)应该返回 "Mid"。 testString: assert(sequentialSizes(5) === "Mid"); - - text: sequentialSizes(6)应返回“Mid” + - text: sequentialSizes(6)应该返回 "Mid"。 testString: assert(sequentialSizes(6) === "Mid"); - - text: sequentialSizes(7)应该返回“High” + - text: sequentialSizes(7)应该返回 "High"。 testString: assert(sequentialSizes(7) === "High"); - - text: sequentialSizes(8)应该返回“High” + - text: sequentialSizes(8)应该返回 "High"。 testString: assert(sequentialSizes(8) === "High"); - - text: sequentialSizes(9)应该返回“High” + - text: sequentialSizes(9)应该返回 "High"。 testString: assert(sequentialSizes(9) === "High"); - - text: 您不应该使用任何ifelse语句 + - text: 你不应使用ifelse语句。 testString: assert(!/else/g.test(code) || !/if/g.test(code)); - - text: 你应该有九个case陈述 + - text: 你应该编写 9 个case语句。 testString: assert(code.match(/case/g).length === 9); ``` @@ -74,7 +94,30 @@ sequentialSizes(1); ## Solution
+ ```js -// solution required +function sequentialSizes(val) { + var 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; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/multiply-two-decimals-with-javascript.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/multiply-two-decimals-with-javascript.chinese.md index 22d40ee2a4..399987c868 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/multiply-two-decimals-with-javascript.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/multiply-two-decimals-with-javascript.chinese.md @@ -2,24 +2,30 @@ id: bd7993c9c69feddfaeb7bdef title: Multiply Two Decimals with JavaScript challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript乘以两个小数 +videoUrl: 'https://scrimba.com/c/ce2GeHq' +forumTopicId: 301173 +localeTitle: 两个小数相乘 --- ## Description -
在JavaScript中,您也可以使用十进制数执行计算,就像整数一样。让我们将两位小数相乘得到它们的乘积。
+
+在 JavaScript 中,你也可以用小数进行计算,就像整数一样。 +把两个小数相乘,并得到它们乘积。 +
## Instructions -
更改0.0使产品等于5.0
+
+改变0.0的数值让变量product的值等于5.0。 +
## Tests
```yml tests: - - text: 变量product应该等于5.0 。 + - text: 变量product应该等于5.0。 testString: assert(product === 5.0); - - text: 你应该使用*运算符 + - text: 要使用*运算符。 testString: assert(/\*/.test(code)); ``` @@ -34,6 +40,7 @@ tests: ```js var product = 2.0 * 0.0; + ``` @@ -43,7 +50,7 @@ var product = 2.0 * 0.0;
```js -console.info('after the test'); +(function(y){return 'product = '+y;})(product); ```
@@ -53,7 +60,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var product = 2.0 * 2.5; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/multiply-two-numbers-with-javascript.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/multiply-two-numbers-with-javascript.chinese.md index 02bb600a9c..69ad512f89 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/multiply-two-numbers-with-javascript.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/multiply-two-numbers-with-javascript.chinese.md @@ -2,24 +2,38 @@ id: cf1231c1c11feddfaeb5bdef title: Multiply Two Numbers with JavaScript challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript将两个数字相乘 +videoUrl: 'https://scrimba.com/c/cP3y3Aq' +forumTopicId: 18243 +localeTitle: 乘法运算 --- ## Description -
我们也可以将一个数字乘以另一个数字。 JavaScript使用*符号来乘以两个数字。

myVar = 13 * 13; //指定169
+
+我们也可在 JavaScript 中使用乘法运算。 +JavaScript 使用*符号表示两数相乘。 + +示例 + +```js +myVar = 13 * 13; // assigned 169 +``` + + +
## Instructions -
更改0使产品等于80
+
+改变数值0来让变量 product 的值等于80。 +
## Tests
```yml tests: - - text: 使变量product等于80 + - text: 要使product的值等于 80。 testString: assert(product === 80); - - text: 使用*运算符 + - text: 使用*运算符。 testString: assert(/\*/.test(code)); ``` @@ -34,6 +48,7 @@ tests: ```js var product = 8 * 0; + ``` @@ -43,7 +58,7 @@ var product = 8 * 0;
```js -console.info('after the test'); +(function(z){return 'product = '+z;})(product); ```
@@ -53,7 +68,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var product = 8 * 10; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/nest-one-array-within-another-array.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/nest-one-array-within-another-array.chinese.md index ddaab9db87..020cb55066 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/nest-one-array-within-another-array.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/nest-one-array-within-another-array.chinese.md @@ -2,22 +2,27 @@ id: cf1111c1c11feddfaeb7bdef title: Nest one Array within Another Array challengeType: 1 -videoUrl: '' -localeTitle: 将一个Array嵌套在另一个Array中 +videoUrl: 'https://scrimba.com/c/crZQZf8' +forumTopicId: 18247 +localeTitle: 将一个数组嵌套在另一个数组中 --- ## Description -
您还可以在其他数组中嵌套数组,如: [["Bulls", 23], ["White Sox", 45]] 。这也称为多维数组
+
+你也可以在数组中包含其他数组,例如:[["Bulls", 23], ["White Sox", 45]]。这被称为一个多维数组。 +
## Instructions -
创建一个名为myArray的嵌套数组。
+
+创建一个名为myArray的多维数组。 +
## Tests
```yml tests: - - text: myArray应至少有一个嵌套在另一个数组中的数组。 + - text: 应该包含至少一个嵌入的数组。 testString: assert(Array.isArray(myArray) && myArray.some(Array.isArray)); ``` @@ -45,7 +50,7 @@ var myArray = [];
```js -console.info('after the test'); +if(typeof myArray !== "undefined"){(function(){return myArray;})();} ```
@@ -55,7 +60,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myArray = [[1,2,3]]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops.chinese.md index ca823e5cb0..fbb9340cea 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops.chinese.md @@ -2,26 +2,44 @@ id: 56533eb9ac21ba0edf2244e1 title: Nesting For Loops challengeType: 1 -videoUrl: '' -localeTitle: 嵌套循环 +videoUrl: 'https://scrimba.com/c/cRn6GHM' +forumTopicId: 18248 +localeTitle: 循环嵌套 --- ## Description -
如果您有一个多维数组,则可以使用与先前路点相同的逻辑来遍历数组和任何子数组。这是一个例子:
var arr = [
[1,2],[3,4],[5,6]
]。
for(var i = 0; i <arr.length; i ++){
for(var j = 0; j <arr [i] .length; j ++){
的console.log(ARR [i] [j]);
}
}
这个输出在每个子元件arr一次一个。注意,对于内部循环,我们检查arr[i].length ,因为arr[i]本身就是一个数组。
+
+如果你有一个二维数组,可以使用相同的逻辑,先遍历外面的数组,再遍历里面的子数组。下面是一个例子: + +```js +var arr = [ + [1,2], [3,4], [5,6] +]; +for (var i=0; i < arr.length; i++) { + for (var j=0; j < arr[i].length; j++) { + console.log(arr[i][j]); + } +} +``` + +一次输出arr中的每个子元素。提示,对于内部循环,我们可以通过arr[i].length来获得子数组的长度,因为arr[i]的本身就是一个数组。 +
## Instructions -
修改函数multiplyAll ,使其乘以product变量乘以arr的子数组中的每个数字
+
+修改函数multiplyAll,获得arr内部数组的每个数字相乘的结果product。 +
## Tests
```yml tests: - - text: 'multiplyAll([[1],[2],[3]])应该返回6' + - text: multiplyAll([[1],[2],[3]])应该返回 6。 testString: assert(multiplyAll([[1],[2],[3]]) === 6); - - text: 'multiplyAll([[1,2],[3,4],[5,6,7]])应返回5040' + - text: multiplyAll([[1,2],[3,4],[5,6,7]])应该返回 5040。 testString: assert(multiplyAll([[1,2],[3,4],[5,6,7]]) === 5040); - - text: 'multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]])应该返回54' + - text: multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]])应该返回 54。 testString: assert(multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]]) === 54); ``` @@ -56,7 +74,19 @@ multiplyAll([[1,2],[3,4],[5,6,7]]); ## Solution
+ ```js -// solution required +function multiplyAll(arr) { + var product = 1; + for (var i = 0; i < arr.length; i++) { + for (var j = 0; j < arr[i].length; j++) { + product *= arr[i][j]; + } + } + return product; +} + +multiplyAll([[1,2],[3,4],[5,6,7]]); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/passing-values-to-functions-with-arguments.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/passing-values-to-functions-with-arguments.chinese.md index 8f9e75c58a..1b129754bb 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/passing-values-to-functions-with-arguments.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/passing-values-to-functions-with-arguments.chinese.md @@ -2,29 +2,45 @@ id: 56533eb9ac21ba0edf2244bd title: Passing Values to Functions with Arguments challengeType: 1 -videoUrl: '' -localeTitle: 将值传递给带参数的函数 +videoUrl: 'https://scrimba.com/c/cy8rahW' +forumTopicId: 18254 +localeTitle: 将值传递给带有参数的函数 --- ## Description -
参数是变量,它们作为调用函数时要输入到函数的值的占位符。定义函数时,通常将其与一个或多个参数一起定义。调用函数时输入(或“传递” )的实际值称为参数 。这是一个带有两个参数的函数, param1param2
function testFun(param1,param2){
console.log(param1,param2);
}
然后我们可以调用testFuntestFun("Hello", "World");我们通过了两个论点, "Hello""World" 。在函数内部, param1将等于“Hello”, param2将等于“World”。请注意,您可以使用不同的参数再次调用testFun ,并且参数将采用新参数的值。
+
+函数的参数parameters在函数中充当占位符(也叫形参)的作用,参数可以为一个或多个。调用一个函数时所传入的参数为实参,实参决定着形参真正的值。简单理解:形参即形式、实参即内容。 +这是带有两个参数的函数,param1param2: + +```js +function testFun(param1, param2) { + console.log(param1, param2); +} +``` + +接着我们调用testFun: +testFun("Hello", "World"); +我们传递了两个参数,"Hello""World"。在函数内部,param1等于“Hello”,param2等于“World”。请注意,testFun函数可以多次调用,每次调用时传递的参数会决定形参的实际值。 +
## Instructions -
  1. 创建一个名为functionWithArgs ,该函数接受两个参数并将其总和输出到开发控制台。
  2. 使用两个数字作为参数调用该函数。
+
+
  1. 创建一个名为functionWithArgs的函数,它可以接收两个参数,计算参数的和,将结果输出到控制台。
  2. 调用这个函数。
+
## Tests
```yml tests: - - text: functionWithArgs应该是一个函数 + - text: functionWithArgs应该是一个函数。 testString: assert(typeof functionWithArgs === 'function'); - - text: 'functionWithArgs(1,2)应该输出3' + - text: functionWithArgs(1,2)应该输出3。 testString: if(typeof functionWithArgs === "function") { capture(); functionWithArgs(1,2); uncapture(); } assert(logOutput == 3); - - text: 'functionWithArgs(7,9)应该输出16' + - text: functionWithArgs(7,9)应该输出16。 testString: if(typeof functionWithArgs === "function") { capture(); functionWithArgs(7,9); uncapture(); } assert(logOutput == 16); - - text: 定义后,使用两个数字调用functionWithArgs 。 - testString: assert(/^\s*functionWithArgs\s*\(\s*\d+\s*,\s*\d+\s*\)\s*/m.test(code)); + - text: 在你定义functionWithArgs之后记得调用它。 + testString: assert(/^\s*functionWithArgs\s*\(\s*\d+\s*,\s*\d+\s*\)\s*;?/m.test(code)); ``` @@ -44,6 +60,7 @@ ourFunctionWithArgs(10, 5); // Outputs 5 // Only change code below this line. + ``` @@ -72,7 +89,6 @@ function uncapture() { } capture(); - ``` @@ -81,7 +97,13 @@ capture();
```js -console.info('after the test'); +uncapture(); + +if (typeof functionWithArgs !== "function") { + (function() { return "functionWithArgs is not defined"; })(); +} else { + (function() { return logOutput || "console.log never called"; })(); +} ```
@@ -91,7 +113,12 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +function functionWithArgs(a, b) { + console.log(a + b); +} +functionWithArgs(10, 5); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/practice-comparing-different-values.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/practice-comparing-different-values.chinese.md index 7d4a5d1115..f52775a086 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/practice-comparing-different-values.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/practice-comparing-different-values.chinese.md @@ -2,26 +2,47 @@ id: 599a789b454f2bbd91a3ff4d title: Practice comparing different values challengeType: 1 -videoUrl: '' -localeTitle: 练习比较不同的值 +videoUrl: 'https://scrimba.com/c/cm8PqCa' +forumTopicId: 301174 +localeTitle: 比较不同值 --- ## Description -
在最后两个挑战中,我们学习了等于运算符( == )和严格相等运算符( === )。让我们快速回顾一下这些运算符的实践。如果要比较的值不是同一类型,则相等运算符将执行类型转换,然后计算值。但是,严格相等运算符将按原样比较数据类型和值,而不将一种类型转换为另一种类型。 例子
3 =='3'//返回true,因为JavaScript执行从字符串到数字的类型转换
3 ==='3'//返回false,因为类型不同并且未执行类型转换
注意
在JavaScript中,您可以使用typeof运算符确定变量的类型或值,如下所示:
typeof 3 //返回'number'
typeof'3'//返回'string'
+
+在上两个挑战中,我们学习了相等运算符 (==) 和严格相等运算符 (===)。现在让我们快速回顾并实践一下。 +如果要比较的值不是同一类型,相等运算符会先执行数据类型转换,然后比较值。而严格相等运算符只比较值,不会进行数据类型转换。 +由此可见,相等运算符和严格相等运算符的区别是:前者会执行隐式类型转换,后者不会。 +示例 + +```js +3 == '3' // returns true because JavaScript performs type conversion from string to number +3 === '3' // returns false because the types are different and type conversion is not performed +``` + +提示
在JavaScript中,你可以使用typeof运算符确定变量的类型或值,如下所示: + +```js +typeof 3 // returns 'number' +typeof '3' // returns 'string' +``` + +
## Instructions -
编辑器中的compareEquality函数使用equality operator比较两个值。修改函数,使其仅在值严格相等时返回“Equal”。
+
+编辑器中的compareEquality函数使用相等运算符比较两个值。修改函数,使其仅在值严格相等时返回 "Equal" 。 +
## Tests
```yml tests: - - text: 'compareEquality(10, "10")应返回“Not Equal”' + - text: compareEquality(10, "10")应该返回 "Not Equal"。 testString: assert(compareEquality(10, "10") === "Not Equal"); - - text: 'compareEquality("20", 20)应该返回“Not Equal”' + - text: compareEquality("20", 20)应该返回 "Not Equal"。 testString: assert(compareEquality("20", 20) === "Not Equal"); - - text: 您应该使用===运算符 + - text: 你应该使用===运算符。 testString: assert(code.match(/===/g)); ``` @@ -44,7 +65,6 @@ function compareEquality(a, b) { // Change this value to test compareEquality(10, "10"); - ``` @@ -56,7 +76,14 @@ compareEquality(10, "10"); ## Solution
+ ```js -// solution required +function compareEquality(a,b) { + if (a === b) { + return "Equal"; + } + return "Not Equal"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/profile-lookup.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/profile-lookup.chinese.md index 704168125a..56c9332d69 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/profile-lookup.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/profile-lookup.chinese.md @@ -2,15 +2,24 @@ id: 5688e62ea601b2482ff8422b title: Profile Lookup challengeType: 1 -videoUrl: '' -localeTitle: 个人资料查询 +videoUrl: 'https://scrimba.com/c/cDqW2Cg' +forumTopicId: 18259 +localeTitle: 资料查找 --- ## Description -
我们的联系人列表中有一组代表不同人的对象。已经为您预先编写了一个以name和属性( prop )作为参数的lookUpProfile函数。该函数应检查name是否是实际联系人的firstName ,并且给定属性( prop )是该联系人的属性。如果两者都为真,则返回该属性的“值”。如果name与任何联系人不对应,则返回"No such contact"如果prop不符合找到匹配name的联系人的任何有效属性,则返回"No such property"
+
+我们有一个对象数组,里面存储着通讯录。 +函数lookUp有两个预定义参数:firstName值和prop属性 。 +函数将会检查通讯录中是否存在一个与传入的firstName相同的联系人。如果存在,那么还需要检查对应的联系人中是否存在prop属性。 +如果它们都存在,函数返回prop属性对应的值。 +如果firstName值不存在,返回"No such contact"。 +如果prop属性不存在,返回"No such property"。 +
## Instructions -
+
+
## Tests @@ -18,17 +27,17 @@ localeTitle: 个人资料查询 ```yml tests: - - text: '"Kristian", "lastName"应该返回"Vos"' + - text: "Kristian", "lastName"应该返回 "Vos"。 testString: assert(lookUpProfile('Kristian','lastName') === "Vos"); - - text: '"Sherlock", "likes"应该回归["Intriguing Cases", "Violin"]' + - text: "Sherlock", "likes"应该返回 ["Intriguing Cases", "Violin"]。 testString: assert.deepEqual(lookUpProfile("Sherlock", "likes"), ["Intriguing Cases", "Violin"]); - - text: '"Harry","likes"应该返回一个阵列' + - text: "Harry","likes"应该返回 an array。 testString: assert(typeof lookUpProfile("Harry", "likes") === "object"); - - text: '"Bob", "number"应该返回“没有这样的联系”' + - text: "Bob", "number"应该返回 "No such contact"。 testString: assert(lookUpProfile("Bob", "number") === "No such contact"); - - text: '"Bob", "potato"应该返回“没有这样的联系”' + - text: "Bob", "potato"应该返回 "No such contact"。 testString: assert(lookUpProfile("Bob", "potato") === "No such contact"); - - text: '"Akira", "address"应该返回“没有这样的财产”' + - text: "Akira", "address"应该返回 "No such property"。 testString: assert(lookUpProfile("Akira", "address") === "No such property"); ``` @@ -78,7 +87,6 @@ function lookUpProfile(name, prop){ // Change these values to test your function lookUpProfile("Akira", "likes"); - ``` @@ -90,7 +98,48 @@ lookUpProfile("Akira", "likes"); ## Solution
+ ```js -// solution required +var 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"] + }, +]; + + +//Write your function in between these comments +function lookUpProfile(name, prop){ + for(var i in contacts){ + if(contacts[i].firstName === name) { + return contacts[i][prop] || "No such property"; + } + } + return "No such contact"; +} +//Write your function in between these comments + +lookUpProfile("Akira", "likes"); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/quoting-strings-with-single-quotes.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/quoting-strings-with-single-quotes.chinese.md index 55dfdae64c..cf7c32c9dd 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/quoting-strings-with-single-quotes.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/quoting-strings-with-single-quotes.chinese.md @@ -2,24 +2,51 @@ id: 56533eb9ac21ba0edf2244b4 title: Quoting Strings with Single Quotes challengeType: 1 -videoUrl: '' -localeTitle: 单引号引用字符串 +videoUrl: 'https://scrimba.com/c/cbQmnhM' +forumTopicId: 18260 +localeTitle: 用单引号引用字符串 --- ## Description -
JavaScript中的字符串值可以使用单引号或双引号编写,只要您以相同类型的引号开头和结尾即可。与其他一些编程语言不同,单引号和双引号在JavaScript中的工作方式相同。
doubleQuoteStr =“这是一个字符串”;
singleQuoteStr ='这也是一个字符串';
你可能想要使用一种报价而不是另一种报价的原因是你想在字符串中使用这两种报价。如果要将对话保存在字符串中并将对话用引号括起,则可能会发生这种情况。它的另一个用途是在一个字符串中保存带引号中各种属性的<a>标签。
谈话='芬恩向杰克惊呼,“代数!”';
但是,如果您需要使用其中的最外层引号,这将成为一个问题。请记住,字符串在开头和结尾都有相同的引用。但是如果你在中间的某个地方有相同的引用,字符串将提前停止并抛出错误。
goodStr ='杰克问芬恩,“嘿,我们去冒险吧?”
badStr ='芬恩回答,“我们走了!”'; //引发错误
在上面的goodStr中 ,您可以使用反斜杠\作为转义字符安全地使用两个引号。 注意
反斜杠\不应与正斜杠/混淆。他们不做同样的事情。
+
+JavaScript 中的字符串可以使用开始和结束都是同类型的单引号或双引号表示,与其他一些编程语言不同的是,单引号和双引号的功能在 JavaScript 中是相同的。 + +```js +doubleQuoteStr = "This is a string"; +singleQuoteStr = 'This is also a string'; +``` + +当你需要在一个字符串中使用多个引号的时候,你可以使用单引号包裹双引号或者相反。常见的场景比如在字符串中包含对话的句子需要用引号包裹。另外比如在一个包含有<a>标签的字符串中,<a>标签的属性值需要用引号包裹。 + +```js +conversation = 'Finn exclaims to Jake, "Algebraic!"'; +``` + +但是,如果你想在字符串中使用与最外层相同的引号,会有一些问题。要知道,字符串在开头和结尾都有相同的引号,如果在中间使用了相同的引号,字符串会提前中止并抛出错误。 + +```js +goodStr = 'Jake asks Finn, "Hey, let\'s go on an adventure?"'; +badStr = 'Finn responds, "Let's go!"'; // Throws an error +``` + +在上面的goodStr中,通过使用反斜杠\转义字符可以安全地使用两种引号 +提示
不要把反斜杠\和斜杠/搞混,它们不是一回事。 +
## Instructions -
将提供的字符串更改为在开头和结尾使用单引号的字符串,并且不包含转义字符。现在,字符串中的<a>标签在任何地方都使用双引号。您需要将外引号更改为单引号,以便删除转义字符。
+
+把字符串更改为开头和结尾使用单引号的字符串,并且不包含转义字符。 +这样字符串中的<a>标签里面任何地方都可以使用双引号。你需要将最外层引号更改为单引号,以便删除转义字符。 +
## Tests
```yml tests: - - text: 删除所有backslashes\ ) + - text: 删除所有反斜杠 (\)。 testString: assert(!/\\/g.test(code) && myStr.match('\\s*\\s*Link\\s*\\s*')); - - text: '你应该有两个单引号'和四个双引号"' + - text: 应该要有两个单引号'和四个双引号"。 testString: assert(code.match(/"/g).length === 4 && code.match(/'/g).length === 2); ``` @@ -34,6 +61,7 @@ tests: ```js var myStr = "Link"; + ``` @@ -43,7 +71,7 @@ var myStr = "Link";
```js -console.info('after the test'); +(function() { return "myStr = " + myStr; })(); ```
@@ -53,7 +81,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myStr = 'Link'; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/record-collection.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/record-collection.chinese.md index 97c47318bb..732e78d2b1 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/record-collection.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/record-collection.chinese.md @@ -2,15 +2,29 @@ id: 56533eb9ac21ba0edf2244cf title: Record Collection challengeType: 1 -videoUrl: '' -localeTitle: 记录收集 +videoUrl: 'https://scrimba.com/c/c4mpysg' +forumTopicId: 18261 +localeTitle: 记录集合 --- ## Description -
您将获得一个JSON对象,表示您的音乐专辑集合的一部分。每张专辑都有几个属性和一个唯一的ID号作为其关键。并非所有相册都有完整的信息。写一个功能,它取一个专辑的id (如2548 ),一个属性prop (如"artist""tracks" ),以及一个value (如"Addicted to Love" )来修改此集合中的数据。如果prop不是"tracks"value不为空( "" ),则更新或设置该记录专辑属性的value 。您的函数必须始终返回整个集合对象。处理不完整数据有几个规则:如果prop"tracks"但是相册没有"tracks"属性,则在将新值添加到相册的相应属性之前创建一个空数组。如果prop"tracks"value不为空( "" ),则将value推到专辑现有tracks数组的末尾。如果value为空( "" ),则从相册中删除给定的prop属性。 提示
使用变量访问对象属性时使用bracket notation 。 Push是一种可以在Mozilla Developer Network上阅读的数组方法。您可以参考操作复杂对象介绍JavaScript对象表示法(JSON)进行复习。
+
+给定一个 JSON 对象,用来表示部分音乐专辑收藏。每张专辑都有几个属性和一个唯一的 id 号作为键值。并非所有专辑都有完整的信息。 +写一个函数,根据传入的id(如2548)、prop(属性,如"artist""tracks")以及value(值,如"Addicted to Love")来修改音乐专辑收藏的数据。 +如果属性prop不是"tracks"且值value不为空(""),则更新或设置该专辑属性的值value。 +你的函数必须始终返回整个音乐专辑集合对象。 +处理不完整数据有几条规则: +如果属性prop"tracks",但是专辑没有"tracks"属性,则在添加值之前先给"tracks"创建一个空数组。 +如果prop"tracks",并且值value不为空(""), 把值value添加到tracks数组中。 +如果值value为空(""),则删除专辑的这一属性prop +提示:
通过变量访问对象的属性时,应使用中括号。 +Push 是一个数组方法,详情请查看Mozilla Developer Network. +你可以参考操作复杂对象这一节的内容复习相关知识。 +
## Instructions -
+
+
## Tests @@ -18,19 +32,21 @@ localeTitle: 记录收集 ```yml tests: - - text: '在updateRecords(5439, "artist", "ABBA")artist应该是"ABBA"' - testString: collection = collectionCopy; assert(updateRecords(5439, "artist", "ABBA")[5439]["artist"] === "ABBA"); - - text: '在updateRecords(5439, "tracks", "Take a Chance on Me")tracks应该以"Take a Chance on Me"作为最后一个元素。' + - text: 执行updateRecords(5439, "artist", "ABBA")后,artist属性值应该是"ABBA"。 + testString: 'assert(code.match(/var collection = {\s*2548: {\s*album: "Slippery When Wet",\s*artist: "Bon Jovi",\s*tracks: \[\s*"Let It Rock",\s*"You Give Love a Bad Name"\s*\]\s*},\s*2468: {\s*album: "1999",\s*artist: "Prince",\s*tracks: \[\s*"1999",\s*"Little Red Corvette"\s*\]\s*},\s*1245: {\s*artist: "Robert Palmer",\s*tracks: \[ \]\s*},\s*5439: {\s*album: "ABBA Gold"\s*}\s*};/g));' + - text: 执行updateRecords(5439, "artist", "ABBA")后,artist 最后的元素应该是 "ABBA"。 + testString: assert(updateRecords(5439, "artist", "ABBA")[5439]["artist"] === "ABBA"); + - text: 执行 updateRecords(5439, "tracks", "Take a Chance on Me") 后,tracks 最后的元素应该是 "Take a Chance on Me"。 testString: assert(updateRecords(5439, "tracks", "Take a Chance on Me")[5439]["tracks"].pop() === "Take a Chance on Me"); - - text: '在updateRecords(2548, "artist", "") ,不应该设置artist' + - text: 执行updateRecords(2548, "artist", "")后,artist不应被创建。 testString: updateRecords(2548, "artist", ""); assert(!collection[2548].hasOwnProperty("artist")); - - text: '在updateRecords(1245, "tracks", "Addicted to Love")tracks应该将"Addicted to Love"作为最后一个元素。' + - text: 执行updateRecords(1245, "tracks", "Addicted to Love")后,tracks最后的元素应该是"Addicted to Love"。 testString: assert(updateRecords(1245, "tracks", "Addicted to Love")[1245]["tracks"].pop() === "Addicted to Love"); - - text: '在updateRecords(2468, "tracks", "Free")tracks应该以"1999"作为第一个元素。' + - text: 执行updateRecords(2468, "tracks", "Free")后,tracks第一个元素应该是"1999"。 testString: assert(updateRecords(2468, "tracks", "Free")[2468]["tracks"][0] === "1999"); - - text: '在updateRecords(2548, "tracks", "") ,不应设置tracks' + - text: 执行updateRecords(2548, "tracks", "")后,tracks不应被创建。 testString: updateRecords(2548, "tracks", ""); assert(!collection[2548].hasOwnProperty("tracks")); - - text: '在updateRecords(1245, "album", "Riptide")album应该是"Riptide"' + - text: 执行updateRecords(1245, "album", "Riptide")后,album应该是"Riptide"。 testString: assert(updateRecords(1245, "album", "Riptide")[1245]["album"] === "Riptide"); ``` @@ -39,38 +55,35 @@ tests: ## Challenge Seed
-
```js // Setup var collection = { - "2548": { - "album": "Slippery When Wet", - "artist": "Bon Jovi", - "tracks": [ - "Let It Rock", - "You Give Love a Bad Name" - ] - }, - "2468": { - "album": "1999", - "artist": "Prince", - "tracks": [ - "1999", - "Little Red Corvette" - ] - }, - "1245": { - "artist": "Robert Palmer", - "tracks": [ ] - }, - "5439": { - "album": "ABBA Gold" - } + 2548: { + album: "Slippery When Wet", + artist: "Bon Jovi", + tracks: [ + "Let It Rock", + "You Give Love a Bad Name" + ] + }, + 2468: { + album: "1999", + artist: "Prince", + tracks: [ + "1999", + "Little Red Corvette" + ] + }, + 1245: { + artist: "Robert Palmer", + tracks: [ ] + }, + 5439: { + album: "ABBA Gold" + } }; -// Keep a copy of the collection for tests -var collectionCopy = JSON.parse(JSON.stringify(collection)); // Only change code below this line function updateRecords(id, prop, value) { @@ -85,23 +98,50 @@ updateRecords(5439, "artist", "ABBA"); ```
- - -### After Test -
- -```js -console.info('after the test'); -``` - -
-
## Solution
```js -// solution required +var collection = { + 2548: { + album: "Slippery When Wet", + artist: "Bon Jovi", + tracks: [ + "Let It Rock", + "You Give Love a Bad Name" + ] + }, + 2468: { + album: "1999", + artist: "Prince", + tracks: [ + "1999", + "Little Red Corvette" + ] + }, + 1245: { + artist: "Robert Palmer", + tracks: [ ] + }, + 5439: { + album: "ABBA Gold" + } +}; + +// Only change code below this line +function updateRecords(id, prop, value) { + if(value === "") delete collection[id][prop]; + else if(prop === "tracks") { + collection[id][prop] = collection[id][prop] || []; + collection[id][prop].push(value); + } else { + collection[id][prop] = value; + } + + return collection; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion.chinese.md new file mode 100644 index 0000000000..5a3f2a0273 --- /dev/null +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion.chinese.md @@ -0,0 +1,110 @@ +--- +id: 5cfa3679138e7d9595b9d9d4 +title: Replace Loops using Recursion +challengeType: 1 +videoUrl: 'https://www.freecodecamp.org/news/how-recursion-works-explained-with-flowcharts-and-a-video-de61f40cb7f9/' +forumTopicId: 301175 +localeTitle: 使用递归代替循环 +--- + +## Description +
+函数调用自身的编程技巧称为递归。为了便于理解,有如下任务:计算数组内元素第 0 到第 n 的元素乘积,使用 for 循环, 可以这样做: + +```js + function multiply(arr, n) { + var product = arr[0]; + for (var i = 1; i <= n; i++) { + product *= arr[i]; + } + return product; + } +``` + +下面是递归写法,注意代码里的 multiply(arr, n) == multiply(arr, n - 1) * arr[n]。这意味着可以重写 multiply 以调用自身而无需依赖循环。 + +```js + function multiply(arr, n) { + if (n <= 0) { + return arr[0]; + } else { + return multiply(arr, n - 1) * arr[n]; + } + } +``` + +递归版本的 multiply 详述如下。在 base case 里,也就是 n <= 0 时,返回结果,也就是 arr[0]。在 n 比 0 大的情况里,函数会调用自身,参数 n 的值为 n - 1。函数以相同的方式持续调用 multiply,直到 n = 0 为止。所以,所有函数都可以返回,原始的 multiply 返回结果。 + +注意: 递归函数在没有函数调用时(在这个例子是,是当 n <= 0 时)必需有一个跳出结构,否则永远不会执行完毕。 + +
+ +## Instructions +
+ +写一个递归函数,sum(arr, n),返回递归调用数组 arr 从第 0 个到第 n 个元素和。 + +
+ +## Tests +
+ +``` yml +tests: + - text: sum([1], 0) 应该返回 1 。 + testString: assert.equal(sum([1], 0), 1); + - text: sum([2, 3, 4], 1) 应该返回 5 。 + testString: assert.equal(sum([2, 3, 4], 1), 5); + - text: 代码不应该包含任何形式的循环(for 或者 while 或者高阶函数比如 forEachmapfilter,或者 reduce)。 + testString: assert(!removeJSComments(code).match(/for|while|forEach|map|filter|reduce/g)); + - text: 应该使用递归来解决这个问题。 + testString: assert(removeJSComments(sum.toString()).match(/sum\(.*\)/g).length > 1); +``` + +
+ +## Challenge Seed +
+ +
+ +```js +function sum(arr, n) { + // Only change code below this line + + // Only change code above this line +} + +``` + +
+ +### After Test +
+ +```js +const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); + +``` + +
+ +
+ +## Solution +
+ +```js +function sum(arr, n) { + // Only change code below this line + if(n <= 0) { + return arr[0]; + } else { + return sum(arr, n - 1) + arr[n]; + } + // Only change code above this line +} + +``` + +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch.chinese.md index b2ed9b5541..26b494c246 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch.chinese.md @@ -2,40 +2,71 @@ id: 56533eb9ac21ba0edf2244e0 title: Replacing If Else Chains with Switch challengeType: 1 -videoUrl: '' -localeTitle: 如果用交换机替换其他链条 +videoUrl: 'https://scrimba.com/c/c3JE8fy' +forumTopicId: 18266 +localeTitle: 用一个 Switch 语句来替代多个 if else 语句 --- ## Description -
如果您有许多选项可供选择,那么switch语句比许多链接的if / else if语句更容易编写。下列:
if(val === 1){
answer =“a”;
} else if(val === 2){
answer =“b”;
} else {
answer =“c”;
}
可以替换为:
switch(val){
情况1:
answer =“a”;
打破;
案例2:
answer =“b”;
打破;
默认:
answer =“c”;
}
+
+如果你有多个选项需要选择,switch语句写起来会比多个串联的if/if else语句容易些,譬如: + +```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语句。
+
+把串联的if/if else语句改成switch语句。 +
## Tests
```yml tests: - - text: 您不应该在编辑器中的任何位置使用任何else语句 + - text: 不要使用else表达式。 testString: assert(!/else/g.test(code)); - - text: 您不应在编辑器中的任何位置使用任何if语句 + - text: 不要使用if表达式。 testString: assert(!/if/g.test(code)); - - text: 你应该至少有四个break语句 + - text: 你应该有至少 4 个break表达式。 testString: assert(code.match(/break/g).length >= 4); - - text: chainToSwitch("bob")应该是“Marley” + - text: chainToSwitch("bob")应该为 "Marley"。 testString: assert(chainToSwitch("bob") === "Marley"); - - text: chainToSwitch(42)应该是“答案” + - text: chainToSwitch(42)应该为 "The Answer"。 testString: assert(chainToSwitch(42) === "The Answer"); - - text: chainToSwitch(1)应该是“没有#1” + - text: chainToSwitch(1)应该为 "There is no #1"。 testString: "assert(chainToSwitch(1) === \"There is no #1\");" - - text: chainToSwitch(99)应该是“错过了我这么多!” + - text: chainToSwitch(99)应该为 "Missed me by this much!"。 testString: assert(chainToSwitch(99) === "Missed me by this much!"); - - text: chainToSwitch(7)应该是“Ate Nine” + - text: chainToSwitch(7)应该为 "Ate Nine"。 testString: assert(chainToSwitch(7) === "Ate Nine"); - - text: chainToSwitch("John")应为“”(空字符串) + - text: chainToSwitch("John")应该为 "" (empty string)。 testString: assert(chainToSwitch("John") === ""); - - text: chainToSwitch(156)应为“”(空字符串) + - text: chainToSwitch(156)应该为 "" (empty string)。 testString: assert(chainToSwitch(156) === ""); ``` @@ -82,7 +113,29 @@ chainToSwitch(7); ## Solution
+ ```js -// solution required +function chainToSwitch(val) { + var 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; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/return-a-value-from-a-function-with-return.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/return-a-value-from-a-function-with-return.chinese.md index c8127e5900..7eff67b6ba 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/return-a-value-from-a-function-with-return.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/return-a-value-from-a-function-with-return.chinese.md @@ -2,28 +2,43 @@ id: 56533eb9ac21ba0edf2244c2 title: Return a Value from a Function with Return challengeType: 1 -videoUrl: '' -localeTitle: 从带返回的函数返回值 +videoUrl: 'https://scrimba.com/c/cy87wue' +forumTopicId: 18271 +localeTitle: 函数可以返回某个值 --- ## Description -
我们可以将值传递给带参数的函数。您可以使用return语句从函数中发回一个值。
function plusThree(num){
返回num + 3;
}
var answer = plusThree(5); // 8
plusThree接受num参数并返回一个等于num + 3的值。
+
+我们可以通过函数的参数把值传入函数,也可以使用return语句把数据从一个函数中传出来。 +示例 + +```js +function plusThree(num) { + return num + 3; +} +var answer = plusThree(5); // 8 +``` + +plusThree带有一个num参数并且返回(returns)一个等于num + 3的值。 +
## Instructions -
创建一个接受一个参数的函数timesFive ,将其乘以5 ,然后返回新值。有关如何测试timesFive函数的示例,请参阅编辑器中的最后一行。
+
+创建一个函数timesFive接收一个参数, 把它乘以5之后返回,关于如何测试timesFive 函数,可以参考编辑器中最后一行的示例。 +
## Tests
```yml tests: - - text: timesFive应该是一个功能 + - text: timesFive应是一个函数。 testString: assert(typeof timesFive === 'function'); - - text: timesFive(5)应该返回25 + - text: timesFive(5)应该返回25。 testString: assert(timesFive(5) === 25); - - text: timesFive(2)应该返回10 + - text: timesFive(2)应该返回10。 testString: assert(timesFive(2) === 10); - - text: timesFive(0)应该返回0 + - text: timesFive(0)应该返回0。 testString: assert(timesFive(0) === 0); ``` @@ -46,7 +61,6 @@ function minusSeven(num) { console.log(minusSeven(10)); - ``` @@ -58,7 +72,12 @@ console.log(minusSeven(10)); ## Solution
+ ```js -// solution required +function timesFive(num) { + return num * 5; +} +timesFive(10); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions.chinese.md index 5435167c3e..38e4a3caed 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions.chinese.md @@ -2,32 +2,50 @@ id: 56533eb9ac21ba0edf2244c4 title: Return Early Pattern for Functions challengeType: 1 -videoUrl: '' -localeTitle: 返回函数的早期模式 +videoUrl: 'https://scrimba.com/c/cQe39Sq' +forumTopicId: 18272 +localeTitle: 函数执行到 return 语句就结束 --- ## Description -
当达到return语句时,当前函数的执行停止,控制返回到调用位置。
function myFun(){
的console.log( “你好”);
回归“世界”;
的console.log( “BYEBYE”)
}
myFun();
上面输出“Hello”到控制台,返回“World”,但是"byebye"永远不输出,因为函数退出return语句。
+
+当代码执行到 return 语句时,函数返回一个结果就结束运行了,return 后面的语句不会执行。 +示例 + +```js +function myFun() { + console.log("Hello"); + return "World"; + console.log("byebye") +} +myFun(); +``` + +上面的代码输出"Hello"到控制台、返回 "World",但没有输出"byebye",因为函数遇到 return 语句就退出了。 +
## Instructions -
修改函数abTest以便如果ab小于0 ,函数将立即以undefined值退出。 暗示
请记住, undefined是一个关键字 ,而不是一个字符串。
+
+修改函数abTestab小于0时,函数立即返回一个undefined并退出。 +提示
记住undefined 是一个关键字,而不是一个字符串。 +
## Tests
```yml tests: - - text: 'abTest(2,2)应返回一个数字' + - text: abTest(2,2) 应该返回一个数字。 testString: assert(typeof abTest(2,2) === 'number' ); - - text: 'abTest(2,2)应该返回8' + - text: abTest(2,2) 应该返回 8。 testString: assert(abTest(2,2) === 8 ); - - text: 'abTest(-2,2)应返回undefined' + - text: abTest(-2,2) 应该返回 undefined。 testString: assert(abTest(-2,2) === undefined ); - - text: 'abTest(2,-2)应返回undefined' + - text: abTest(2,-2) 应该返回 undefined。 testString: assert(abTest(2,-2) === undefined ); - - text: 'abTest(2,8)应该返回18' + - text: abTest(2,8) 应该返回 18。 testString: assert(abTest(2,8) === 18 ); - - text: 'abTest(3,3)应该返回12' + - text: abTest(3,3) 应该返回 12。 testString: assert(abTest(3,3) === 12 ); ``` @@ -53,7 +71,6 @@ function abTest(a, b) { // Change values below to test your code abTest(2,2); - ``` @@ -65,7 +82,14 @@ abTest(2,2); ## Solution
+ ```js -// solution required +function abTest(a, b) { + if(a < 0 || b < 0) { + return undefined; + } + return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2)); +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/returning-boolean-values-from-functions.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/returning-boolean-values-from-functions.chinese.md index 8731753fa6..5726f4c0f7 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/returning-boolean-values-from-functions.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/returning-boolean-values-from-functions.chinese.md @@ -2,26 +2,51 @@ id: 5679ceb97cbaa8c51670a16b title: Returning Boolean Values from Functions challengeType: 1 -videoUrl: '' +videoUrl: 'https://scrimba.com/c/cp62qAQ' +forumTopicId: 18273 localeTitle: 从函数返回布尔值 --- ## Description -
您可以从与Equality运算符的比较中回忆一下,所有比较运算符都返回布尔值truefalse值。有时人们使用if / else语句进行比较,如下所示:
function isEqual(a,b){
if(a === b){
返回true;
} else {
返回虚假;
}
}
但是有一种更好的方法可以做到这一点。由于===返回truefalse ,我们可以返回比较结果:
function isEqual(a,b){
返回a === b;
}
+
+你应该还记得相等运算符这道挑战题。在那里我们提到,所有比较操作符都会返回 boolean:要么是true要么是false。 +有时人们通过 if/else 语句来做比较然后返回truefalse。 + +```js +function isEqual(a,b) { + if (a === b) { + return true; + } else { + return false; + } +} +``` + +有一个更好的方法,因为===总是返回truefalse,所以我们可以直接返回比较的结果: + +```js +function isEqual(a,b) { + return a === b; +} +``` + +
## Instructions -
修复函数isLess以删除if/else语句。
+
+移除isLess函数的if/else语句但不影响函数的功能。 +
## Tests
```yml tests: - - text: 'isLess(10,15)应该返回true' + - text: isLess(10,15)应该返回 true。 testString: assert(isLess(10,15) === true); - - text: 'isLess(15,10)应该返回false' + - text: isLess(15,10)应该返回 false。 testString: assert(isLess(15, 10) === false); - - text: 您不应该使用任何ifelse语句 + - text: 不应该使用 if 或者 else 语句。 testString: assert(!/if|else/g.test(code)); ``` @@ -45,7 +70,6 @@ function isLess(a, b) { // Change these values to test isLess(10, 15); - ``` @@ -57,7 +81,11 @@ isLess(10, 15); ## Solution
+ ```js -// solution required +function isLess(a, b) { + return a < b; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/selecting-from-many-options-with-switch-statements.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/selecting-from-many-options-with-switch-statements.chinese.md index c0ac13a380..8e13c26e40 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/selecting-from-many-options-with-switch-statements.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/selecting-from-many-options-with-switch-statements.chinese.md @@ -2,15 +2,15 @@ id: 56533eb9ac21ba0edf2244dd title: Selecting from Many Options with Switch Statements challengeType: 1 -videoUrl: '' -localeTitle: 从带有开关语句的多个选项中进行选择 +videoUrl: 'https://scrimba.com/c/c4mv4fm' +forumTopicId: 18277 +localeTitle: 使用 Switch 语句从许多选项中进行选择 --- ## Description -
- -如果您有很多选择,请使用 switch 语句。 switch 语句测试一个值,并且可以包含许多定义各种可能值的 case 语句。 从第一个匹配的 case 值开始执行语句,直到遇到 break 。 -这是 switch 语句的示例: +
+如果你有非常多的选项需要选择,可以使用 switch 语句。根据不同的参数值会匹配上不同的 case 分支,语句会从第一个匹配的 case 分支开始执行,直到碰到 break 就结束。 +这是一个伪代码案例: ```js switch(lowercaseLetter) { @@ -23,29 +23,30 @@ switch(lowercaseLetter) { } ``` - case 值以严格相等性( === )进行测试。 break 告诉JavaScript停止执行语句。 如果省略 break ,将执行下一条语句。 - +测试case值使用严格相等运算符进行比较,break 关键字告诉 JavaScript 停止执行语句。如果没有 break 关键字,下一个语句会继续执行。
## Instructions -
编写一个switch语句,测试val并设置以下条件的answer
1 - “alpha”
2 - “beta”
3 - “伽玛”
4 - “三角洲”
+
+写一个测试val的 switch 语句,并且根据下面的条件来设置不同的answer
1- "alpha"
2 - "beta"
3- "gamma"
4 - "delta" +
## Tests
```yml tests: - - text: caseInSwitch(1)的值应为“alpha” + - text: caseInSwitch(1) 应该有一个值为 "alpha"。 testString: assert(caseInSwitch(1) === "alpha"); - - text: caseInSwitch(2)的值应为“beta” + - text: caseInSwitch(2) 应该有一个值为 "beta"。 testString: assert(caseInSwitch(2) === "beta"); - - text: caseInSwitch(3)的值应为“gamma” + - text: caseInSwitch(3) 应该有一个值为 "gamma"。 testString: assert(caseInSwitch(3) === "gamma"); - - text: caseInSwitch(4)的值应为“delta” + - text: caseInSwitch(4) 应该有一个值为 "delta"。 testString: assert(caseInSwitch(4) === "delta"); - - text: 您不应该使用任何ifelse语句 + - text: 不能使用任何ifelse表达式。 testString: assert(!/else/g.test(code) || !/if/g.test(code)); - - text: 你应该至少有3个break语句 + - text: 你应该有至少 3 个break表达式。 testString: assert(code.match(/break/g).length > 2); ``` @@ -82,7 +83,26 @@ caseInSwitch(1); ## Solution
+ ```js -// solution required +function caseInSwitch(val) { + var answer = ""; + + switch(val) { + case 1: + answer = "alpha"; + break; + case 2: + answer = "beta"; + break; + case 3: + answer = "gamma"; + break; + case 4: + answer = "delta"; + } + return answer; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/shopping-list.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/shopping-list.chinese.md index f9394bd562..7923692a63 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/shopping-list.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/shopping-list.chinese.md @@ -2,15 +2,22 @@ id: 56533eb9ac21ba0edf2244bc title: Shopping List challengeType: 1 -videoUrl: '' +videoUrl: 'https://scrimba.com/c/c9MEKHZ' +forumTopicId: 18280 localeTitle: 购物清单 --- ## Description -
在变量myList创建购物清单。该列表应该是包含多个子阵列的多维数组。每个子数组中的第一个元素应包含一个带有项目名称的字符串。第二个元素应该是一个代表数量的数字,即["Chocolate Bar", 15]列表中应该至少有5个子数组。
+
+创建一个名叫myList的购物清单,清单的数据格式就是多维数组。 +每个子数组中的第一个元素应该是购买的物品名称,第二个元素应该是物品的数量,类似于: +["Chocolate Bar", 15] +任务:你的购物清单至少应该有 5 个子数组。 +
## Instructions -
+
+
## Tests @@ -18,13 +25,13 @@ localeTitle: 购物清单 ```yml tests: - - text: myList应该是一个数组 + - text: myList应该一个数组。 testString: assert(isArray); - - text: 每个子数组中的第一个元素都必须是字符串 + - text: 你的每个子数组的第一个元素的类型都应该是字符串。 testString: assert(hasString); - - text: 每个子数组中的第二个元素都必须是数字 + - text: 你的每个子数组的第二个元素的类型都应该是数字。 testString: assert(hasNumber); - - text: 您的列表中必须至少有5个项目 + - text: 你的列表中至少要包含 5 个元素。 testString: assert(count > 4); ``` @@ -39,6 +46,7 @@ tests: ```js var myList = []; + ``` @@ -48,7 +56,32 @@ var myList = [];
```js -console.info('after the test'); +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); ```
@@ -58,7 +91,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myList = [ + ["Candy", 10], + ["Potatoes", 12], + ["Eggs", 12], + ["Catfood", 1], + ["Toads", 9] +]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/stand-in-line.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/stand-in-line.chinese.md index d39f504872..7467de104b 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/stand-in-line.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/stand-in-line.chinese.md @@ -2,15 +2,22 @@ id: 56533eb9ac21ba0edf2244c6 title: Stand in Line challengeType: 1 -videoUrl: '' -localeTitle: 站在队中 +videoUrl: 'https://scrimba.com/c/ca8Q8tP' +forumTopicId: 18307 +localeTitle: 排队 --- ## Description -
在计算机科学中, 队列是一个抽象的数据结构 ,其中项目按顺序保存。可以在queue的后面添加新项目,并从queue的前面取出旧项目。编写一个函数nextInLine ,它接受一个数组( arr )和一个数字( item )作为参数。将数字添加到数组的末尾,然后删除数组的第一个元素。然后, nextInLine函数应返回已删除的元素。
+
+在计算机科学中队列(queue)是一个抽象的数据结构,队列中的条目都是有秩序的。新的条目会被加到队列的末尾,旧的条目会从队列的头部被移出。 +写一个函数nextInLine,用一个数组(arr)和一个数字(item)作为参数。 +把数字添加到数组的结尾,然后移出数组的第一个元素。 +最后nextInLine函数应该返回被删除的元素。 +
## Instructions -
+
+
## Tests @@ -18,15 +25,15 @@ localeTitle: 站在队中 ```yml tests: - - text: 'nextInLine([], 5)应返回一个数字。' + - text: nextInLine([], 5)应该返回一个数字。 testString: assert.isNumber(nextInLine([],5)); - - text: 'nextInLine([], 1)应该返回1' + - text: nextInLine([], 1)应该返回1。 testString: assert(nextInLine([],1) === 1); - - text: 'nextInLine([2], 1)应返回2' + - text: nextInLine([2], 1)应该返回2。 testString: assert(nextInLine([2],1) === 2); - - text: 'nextInLine([5,6,7,8,9], 1)应该返回5' + - text: nextInLine([5,6,7,8,9], 1)应该返回5。 testString: assert(nextInLine([5,6,7,8,9],1) === 5); - - text: '在nextInLine(testArr, 10)testArr[4]应为10' + - text: 在nextInLine(testArr, 10)执行后testArr[4]应该是10。 testString: nextInLine(testArr, 10); assert(testArr[4] === 10); ``` @@ -52,7 +59,6 @@ var testArr = [1,2,3,4,5]; console.log("Before: " + JSON.stringify(testArr)); console.log(nextInLine(testArr, 6)); // Modify this line to test console.log("After: " + JSON.stringify(testArr)); - ``` @@ -81,7 +87,6 @@ function uncapture() { } capture(); - ``` @@ -90,7 +95,9 @@ capture();
```js -console.info('after the test'); +uncapture(); +testArr = [1,2,3,4,5]; +(function() { return logOutput.join("\n");})(); ```
@@ -100,7 +107,14 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var testArr = [ 1,2,3,4,5]; + +function nextInLine(arr, item) { + arr.push(item); + return arr.shift(); +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/store-multiple-values-in-one-variable-using-javascript-arrays.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/store-multiple-values-in-one-variable-using-javascript-arrays.chinese.md index 8b705e4b3c..6f59a6a2a6 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/store-multiple-values-in-one-variable-using-javascript-arrays.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/store-multiple-values-in-one-variable-using-javascript-arrays.chinese.md @@ -2,26 +2,34 @@ id: bd7993c9c69feddfaeb8bdef title: Store Multiple Values in one Variable using JavaScript Arrays challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript数组在一个变量中存储多个值 +videoUrl: 'https://scrimba.com/c/crZQWAm' +forumTopicId: 18309 +localeTitle: 使用 JavaScript 数组将多个值存储在一个变量中 --- ## Description -
使用JavaScript array变量,我们可以在一个地方存储多个数据。你开始一个带有开口方括号的数组声明,用一个结束的方括号结束,并在每个条目之间加一个逗号,如下所示: var sandwich = ["peanut butter", "jelly", "bread"]
+
+使用数组,我们可以在一个地方存储多个数据。 +以左方括号[开始定义一个数组,以右方括号]结束,里面每个元素之间用逗号隔开,例如: +var sandwich = ["peanut butter", "jelly", "bread"]. +
## Instructions -
修改新数组myArray ,使其包含stringnumbermyArray顺序)。 暗示
如果卡住,请参阅文本编辑器中的示例代码。
+
+创建一个包含字符串数字的数组myArray。 +提示
如果你遇到困难,请参考文本编辑器中的示例代码。 +
## Tests
```yml tests: - - text: myArray应该是一个array 。 + - text: myArray应该是一个数组。 testString: assert(typeof myArray == 'object'); - - text: myArray的第一项应该是一个string 。 + - text: myArray数组的第一个元素应该是一个字符串。 testString: assert(typeof myArray[0] !== 'undefined' && typeof myArray[0] == 'string'); - - text: myArray的第二项应该是一个number 。 + - text: myArray数组的第二个元素应该是一个数字。 testString: assert(typeof myArray[1] !== 'undefined' && typeof myArray[1] == 'number'); ``` @@ -49,7 +57,7 @@ var myArray = [];
```js -console.info('after the test'); +(function(z){return z;})(myArray); ```
@@ -59,7 +67,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myArray = ["The Answer", 42]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator.chinese.md index 4030dcb68a..d2789ec3a3 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator.chinese.md @@ -2,28 +2,44 @@ id: 56533eb9ac21ba0edf2244a8 title: Storing Values with the Assignment Operator challengeType: 1 -videoUrl: '' +videoUrl: 'https://scrimba.com/c/cEanysE' +forumTopicId: 18310 localeTitle: 使用赋值运算符存储值 --- ## Description -
在JavaScript中,您可以使用赋值运算符将值存储在变量中。 myVariable = 5;这将Number5分配给myVariable 。作业总是从右到左。在将值分配给运算符左侧的变量之前,将解析=运算符右侧的所有内容。
myVar = 5;
myNum = myVar;
这将为myVar分配5 ,然后再次将myVar解析为5并将其分配给myNum
+
+在 JavaScript 中,你可以使用赋值运算符将值存储在变量中。 +myVariable = 5; +这条语句把Number类型的值5赋给变量myVariable。 +赋值过程是从右到左进行的。在将值分配给运算符左侧的变量之前,将解析=运算符右侧的所有内容。 + +```js +myVar = 5; +myNum = myVar; +``` + +数值5被赋给变量myVar中,然后再次将变量myVar解析为5并将其赋给myNum变量。 +
## Instructions -
将值7分配给变量a 。分配的内容a变量b
+
+把数值7赋给变量 a。 +把变量a中的内容赋给变量b。 +
## Tests
```yml tests: - - text: 不要更改行上方的代码 + - text: 不要修改注释上方的代码。 testString: assert(/var a;/.test(code) && /var b = 2;/.test(code)); - - text: a的值应为7 + - text: a的值应该是 7。 testString: assert(typeof a === 'number' && a === 7); - - text: b的值应为7 + - text: b的值应该是 7。 testString: assert(typeof b === 'number' && b === 7); - - text: a应分配给b = + - text: 你需要用=a的值赋给b。 testString: assert(/b\s*=\s*a\s*;/g.test(code)); ``` @@ -56,7 +72,6 @@ if (typeof a != 'undefined') { if (typeof b != 'undefined') { b = undefined; } - ``` @@ -65,7 +80,7 @@ if (typeof b != 'undefined') {
```js -console.info('after the test'); +(function(a,b){return "a = " + a + ", b = " + b;})(a,b); ```
@@ -75,7 +90,12 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var a; +var b = 2; +a = 7; +b = a; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/subtract-one-number-from-another-with-javascript.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/subtract-one-number-from-another-with-javascript.chinese.md index dc707b5ca9..ad6dc602f1 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/subtract-one-number-from-another-with-javascript.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/subtract-one-number-from-another-with-javascript.chinese.md @@ -2,25 +2,39 @@ id: cf1111c1c11feddfaeb4bdef title: Subtract One Number from Another with JavaScript challengeType: 1 -videoUrl: '' -localeTitle: 使用JavaScript从另一个数字中减去一个数字 +videoUrl: 'https://scrimba.com/c/cP3yQtk' +forumTopicId: 18314 +localeTitle: 减法运算 --- ## Description -
我们也可以从另一个数字中减去一个数字。 JavaScript使用-符号进行减法。

myVar = 12 - 6; //分配6
+
+我们也可以在 JavaScript 中进行减法运算。 +JavaScript 中使用-来做减法运算。 + +示例 + +```js +myVar = 12 - 6; // assigned 6 +``` + + +
## Instructions -
更改0因此差异为12
+
+改变数字0让变量 difference 的值为12。 +
## Tests
```yml tests: - - text: 使变量difference等于12。 + - text: 要使difference的值等于 12。 testString: assert(difference === 12); - - text: 只从45中减去一个数字。 - testString: assert(/difference=45-33;?/.test(code.replace(/\s/g, ''))); + - text: 只用 45 减去一个数。 + testString: assert(/var\s*difference\s*=\s*45\s*-\s*[0-9]*;(?!\s*[a-zA-Z0-9]+)/.test(code)); ``` @@ -34,6 +48,7 @@ tests: ```js var difference = 45 - 0; + ``` @@ -43,7 +58,7 @@ var difference = 45 - 0;
```js -console.info('after the test'); +(function(z){return 'difference = '+z;})(difference); ```
@@ -53,7 +68,9 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var difference = 45 - 33; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties.chinese.md index 6157c05269..e212f77411 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties.chinese.md @@ -2,26 +2,42 @@ id: 567af2437cbaa8c51670a16c title: Testing Objects for Properties challengeType: 1 -videoUrl: '' -localeTitle: 测试属性的对象 +videoUrl: 'https://scrimba.com/c/cm8Q7Ua' +forumTopicId: 18324 +localeTitle: 测试对象的属性 --- ## Description -
有时检查给定对象的属性是否存在是有用的。我们可以使用对象的.hasOwnProperty(propname)方法来确定该对象是否具有给定的属性名称。 .hasOwnProperty()如果找到属性则返回truefalse
var myObj = {
顶部:“帽子”,
底部:“裤子”
};
myObj.hasOwnProperty( “顶部”); //真的
myObj.hasOwnProperty( “中间”); //假
+
+有时检查一个对象属性是否存在是非常有用的,我们可以用.hasOwnProperty(propname)方法来检查对象是否有该属性。如果有返回true,反之返回false。 +示例 + +```js +var myObj = { + top: "hat", + bottom: "pants" +}; +myObj.hasOwnProperty("top"); // true +myObj.hasOwnProperty("middle"); // false +``` + +
## Instructions -
修改函数checkObj以测试myObjcheckProp 。如果找到该属性,则返回该属性的值。如果没有,请返回"Not Found"
+
+修改函数checkObj检查myObj是否有checkProp属性,如果属性存在,返回属性对应的值,如果不存在,返回"Not Found"。 +
## Tests
```yml tests: - - text: checkObj("gift")应该返回"pony" 。 + - text: checkObj("gift")应该返回"pony"。 testString: assert(checkObj("gift") === "pony"); - - text: checkObj("pet")应该返回"kitten" 。 + - text: checkObj("pet")应该返回"kitten"。 testString: assert(checkObj("pet") === "kitten"); - - text: checkObj("house")应该返回"Not Found" 。 + - text: checkObj("house")应该返回"Not Found"。 testString: assert(checkObj("house") === "Not Found"); ``` @@ -49,7 +65,6 @@ function checkObj(checkProp) { // Test your code by modifying these values checkObj("gift"); - ``` @@ -61,7 +76,20 @@ checkObj("gift"); ## Solution
+ ```js -// solution required +var myObj = { + gift: "pony", + pet: "kitten", + bed: "sleigh" +}; +function checkObj(checkProp) { + if(myObj.hasOwnProperty(checkProp)) { + return myObj[checkProp]; + } else { + return "Not Found"; + } +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understand-string-immutability.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understand-string-immutability.chinese.md index c9e4195179..7dcd708bd7 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understand-string-immutability.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understand-string-immutability.chinese.md @@ -2,24 +2,43 @@ id: 56533eb9ac21ba0edf2244ba title: Understand String Immutability challengeType: 1 -videoUrl: '' -localeTitle: 理解字符串不变性 +videoUrl: 'https://scrimba.com/c/cWPVaUR' +forumTopicId: 18331 +localeTitle: 了解字符串的不变性 --- ## Description -
在JavaScript中, String值是不可变的 ,这意味着一旦创建它们就不能被更改。例如,以下代码:
var myStr =“Bob”;
myStr [0] =“J”;
无法将myStr的值更改为“Job”,因为myStr的内容无法更改。请注意,这并不意味着myStr不能改变的,只是一个字符串的单个字符不能改变。更改myStr的唯一方法是为其分配一个新字符串,如下所示:
var myStr =“Bob”;
myStr =“工作”;
+
+在 JavaScript 中,字符串的值是 不可变的,这意味着一旦字符串被创建就不能被改变。 +例如,下面的代码: + +```js +var myStr = "Bob"; +myStr[0] = "J"; +``` + +是不会把变量myStr的值改变成 "Job" 的,因为变量myStr是不可变的。注意,这并不意味着myStr永远不能被改变,只是字符串字面量 string literal 的各个字符不能被改变。改变myStr中的唯一方法是重新给它赋一个值,例如: + +```js +var myStr = "Bob"; +myStr = "Job"; +``` + +
## Instructions -
更正myStr的赋值,使其包含Hello World的字符串值,使用上面示例中显示的方法。
+
+把myStr的值改为Hello World。 +
## Tests
```yml tests: - - text: myStr应该具有Hello World的值 + - text: message:myStr的值应该是Hello World。 testString: assert(myStr === "Hello World"); - - text: 不要更改行上方的代码 + - text: 不要修改注释上面的代码。 testString: assert(/myStr = "Jello World"/.test(code)); ``` @@ -39,6 +58,7 @@ var myStr = "Jello World"; myStr[0] = "H"; // Fix Me + ``` @@ -48,7 +68,7 @@ myStr[0] = "H"; // Fix Me
```js -console.info('after the test'); +(function(v){return "myStr = " + v;})(myStr); ```
@@ -58,7 +78,10 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myStr = "Jello World"; +myStr = "Hello World"; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-boolean-values.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-boolean-values.chinese.md index c6af1edf38..e13eb32188 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-boolean-values.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-boolean-values.chinese.md @@ -2,24 +2,30 @@ id: bd7123c9c441eddfaeb5bdef title: Understanding Boolean Values challengeType: 1 -videoUrl: '' -localeTitle: 了解布尔值 +videoUrl: 'https://scrimba.com/c/c9Me8t4' +forumTopicId: 301176 +localeTitle: 理解布尔值 --- ## Description -
另一种数据类型是布尔值Booleans可能只是两个值中的一个: truefalse 。它们基本上是很少的开关,其中true为“on”而false为“off”。这两个国家是相互排斥的。 注意
Boolean值永远不会用引号写。 strings "true""false"不是Boolean ,在JavaScript中没有特殊含义。
+
+另一种数据类型是布尔(Boolean)。布尔值要么是true要么是false。它非常像电路开关,true是 “开”,false是 “关”。这两种状态是互斥的。 +注意
布尔值是不带引号的,"true""false"字符串而不是布尔值,在 JavaScript 中也没有特殊含义。 +
## Instructions -
修改welcomeToBooleans函数,以便在单击运行按钮时返回true而不是false
+
+修改welcomeToBooleans函数,让它返回true而不是false。 +
## Tests
```yml tests: - - text: welcomeToBooleans()函数应该返回一个布尔值(true / false)。 + - text: welcomeToBooleans()函数应该返回一个布尔值 (true/false)。 testString: assert(typeof welcomeToBooleans() === 'boolean'); - - text: welcomeToBooleans()应该返回true。 + - text: welcomeToBooleans()应该返回 true。 testString: assert(welcomeToBooleans() === true); ``` @@ -34,13 +40,12 @@ tests: ```js function welcomeToBooleans() { -// Only change code below this line. + // Only change code below this line. -return false; // Change this line + return false; // Change this line -// Only change code above this line. + // Only change code above this line. } - ``` @@ -50,7 +55,7 @@ return false; // Change this line
```js -console.info('after the test'); +welcomeToBooleans(); ```
@@ -60,7 +65,11 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +function welcomeToBooleans() { + return true; // Change this line +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-case-sensitivity-in-variables.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-case-sensitivity-in-variables.chinese.md index 7cbdc6a3a2..f0c175feb9 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-case-sensitivity-in-variables.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-case-sensitivity-in-variables.chinese.md @@ -2,32 +2,48 @@ id: 56533eb9ac21ba0edf2244ab title: Understanding Case Sensitivity in Variables challengeType: 1 -videoUrl: '' -localeTitle: 了解变量中的大小写敏感度 +videoUrl: 'https://scrimba.com/c/cd6GDcD' +forumTopicId: 18334 +localeTitle: 了解变量名区分大小写 --- ## Description -
在JavaScript中,所有变量和函数名称都区分大小写。这意味着资本化很重要。 MYVARMyVarmyvar 。可以有多个具有相同名称但不同外壳的不同变量。强烈建议您为清晰起见, 不要使用此语言功能。

最佳实践

camelCase中用 JavaScript编写变量名。在camelCase中 ,多字变量名称的第一个单词为小写,每个后续单词的首字母大写。 例子:
var someVariable;
var anotherVariableName;
var thisVariableNameIsSoLong;
+
+在 JavaScript 中所有的变量和函数名都是大小写敏感的。要区别对待大写字母和小写字母。 +MYVARMyVarmyvar是截然不同的变量。这有可能导致出现多个相似名字的的变量。所以强烈地建议你,为了保持代码清晰不要使用这一特性。 +

最佳实践

+使用驼峰命名法来书写一个 Javascript 变量,在驼峰命名法中,变量名的第一个单词的首写字母小写,后面的单词的第一个字母大写。 +示例: + +```js +var someVariable; +var anotherVariableName; +var thisVariableNameIsSoLong; +``` + +
## Instructions -
修改现有的声明和赋值,使其名称使用camelCase
不要创建任何新变量。
+
+修改已声明的变量,让它们的命名符合驼峰命名法的规范。 +
## Tests
```yml tests: - - text: studlyCapVar已定义且值为10 + - text: studlyCapVar应该被定义并且值为10。 testString: assert(typeof studlyCapVar !== 'undefined' && studlyCapVar === 10); - - text: properCamelCase已定义且值为"A String" + - text: properCamelCase应该被定义并且值为"A String"。 testString: assert(typeof properCamelCase !== 'undefined' && properCamelCase === "A String"); - - text: titleCaseOver已定义,其值为9000 + - text: titleCaseOver应该被定义并且值为9000。 testString: assert(typeof titleCaseOver !== 'undefined' && titleCaseOver === 9000); - - text: studlyCapVar应该在声明和赋值部分使用camelCase。 + - text: studlyCapVar在声明和赋值时都应该使用驼峰命名法。 testString: assert(code.match(/studlyCapVar/g).length === 2); - - text: properCamelCase应该在声明和赋值部分使用camelCase。 + - text: properCamelCase 在声明和赋值时都应该使用驼峰命名法。 testString: assert(code.match(/properCamelCase/g).length === 2); - - text: titleCaseOver应该在声明和赋值部分使用camelCase。 + - text: titleCaseOver 在声明和赋值时都应该使用驼峰命名法。 testString: assert(code.match(/titleCaseOver/g).length === 2); ``` @@ -49,7 +65,6 @@ var TitleCaseOver; STUDLYCAPVAR = 10; PRoperCAmelCAse = "A String"; tITLEcASEoVER = 9000; - ``` @@ -61,7 +76,15 @@ tITLEcASEoVER = 9000; ## Solution
+ ```js -// solution required +var studlyCapVar; +var properCamelCase; +var titleCaseOver; + +studlyCapVar = 10; +properCamelCase = "A String"; +titleCaseOver = 9000; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-undefined-value-returned-from-a-function.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-undefined-value-returned-from-a-function.chinese.md index 4867682866..2ff380bb16 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-undefined-value-returned-from-a-function.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-undefined-value-returned-from-a-function.chinese.md @@ -2,28 +2,44 @@ id: 598e8944f009e646fc236146 title: Understanding Undefined Value returned from a Function challengeType: 1 -videoUrl: '' -localeTitle: 了解从函数返回的未定义值 +videoUrl: 'https://scrimba.com/c/ce2p7cL' +forumTopicId: 301177 +localeTitle: 函数也可以返回 undefined --- ## Description -
函数可以包含return语句但不必包含。在函数没有return语句的情况下,当您调用它时,该函数处理内部代码但返回的值是undefined
var sum = 0;
function addSum(num){
sum = sum + num;
}
var returnedValue = addSum(3); //将修改sum,但返回值未定义
addSum是一个没有return语句的函数。该函数将更改全局sum变量,但函数的返回值undefined
+
+函数一般用return语句来返回值,但这不是必须的。在函数没有return语句的情况下,当你调用它时,该函数会执行内部代码,返回的值是undefined。 +示例 + +```js +var sum = 0; +function addSum(num) { + sum = sum + num; +} +addSum(3); // sum will be modified but returned value is undefined +``` + +addSum是一个没有return语句的函数。该函数将更改全局变量sum,函数的返回值为undefined。 +
## Instructions -
创建一个没有任何参数的函数addFive 。此函数向sum变量添加5,但其返回值undefined
+
+创建一个没有任何参数的函数addFive。此函数使sum变量加 5,但其返回值是undefined。 +
## Tests
```yml tests: - - text: addFive应该是一个函数 + - text: addFive应该是一个函数。 testString: assert(typeof addFive === 'function'); - - text: sum应该等于8 + - text: sum应该等于 8。 testString: assert(sum === 8); - - text: addFive返回值应该是undefined + - text: addFive的返回值应该是undefined。 testString: assert(addFive() === undefined); - - text: 在函数内部,向sum变量添加5 + - text: 函数给变量 sum 加 5。 testString: assert(addFive.toString().replace(/\s/g, '').match(/sum=sum\+5|sum\+=5/)); ``` @@ -44,31 +60,34 @@ function addThree() { // Only change code below this line - - // Only change code above this line -var returnedValue = addFive(); - +addThree(); +addFive(); ``` -### After Test -
- -```js -console.info('after the test'); -``` - -
- -
- ## Solution
+ ```js -// solution required +// Example +var sum = 0; +function addThree() { + sum = sum + 3; +} + +// Only change code below this line + +function addFive() { + sum = sum + 5; +} + +// Only change code above this line +addThree(); +addFive(); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-uninitialized-variables.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-uninitialized-variables.chinese.md index 2cb76233af..c8233a7a03 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-uninitialized-variables.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-uninitialized-variables.chinese.md @@ -2,28 +2,33 @@ id: 56533eb9ac21ba0edf2244aa title: Understanding Uninitialized Variables challengeType: 1 -videoUrl: '' -localeTitle: 了解未初始化的变量 +videoUrl: 'https://scrimba.com/c/cBa2JAL' +forumTopicId: 18335 +localeTitle: 理解未初始化的变量 --- ## Description -
声明JavaScript变量时,它们的初始值为undefined 。如果对undefined变量进行数学运算,则结果将为NaN ,表示“非数字” 。如果将字符串与undefined变量连接起来,您将得到一个"undefined"的文字字符串
+
+当 JavaScript 中的变量被声明的时候,程序内部会给它一个初始值undefined。当你对一个值为undefined的变量进行运算操作的时候,算出来的结果将会是NaNNaN的意思是"Not a Number"。当你用一个值是undefined的变量来做字符串拼接操作的时候,它会输出字符串"undefined"。 +
## Instructions -
初始化三个变量abc510 ,和"I am a"分别让他们不会undefined
+
+定义 3 个变量abc,并且分别给他们赋值:510"I am a",这样它们值就不会是undefined了。 +
## Tests
```yml tests: - - text: a应定义并评估其值为6 + - text: a应该被定义,并且值为6。 testString: assert(typeof a === 'number' && a === 6); - - text: 应定义和评估b的值为15 + - text: b应该被定义,并且值为15。 testString: assert(typeof b === 'number' && b === 15); - - text: c不应该包含undefined并且值应为“我是一个字符串!” + - text: c的值不能包含undefined,应该为 "I am a String!"。 testString: assert(!/undefined/.test(c) && c === "I am a String!"); - - text: 不要更改行下方的代码 + - text: 不要修改第二条注释下的代码。 testString: assert(/a = a \+ 1;/.test(code) && /b = b \+ 5;/.test(code) && /c = c \+ " String!";/.test(code)); ``` @@ -56,7 +61,7 @@ c = c + " String!";
```js -console.info('after the test'); +(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = '" + c + "'"; })(a,b,c); ```
@@ -66,7 +71,14 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var a = 5; +var b = 10; +var c = "I am a"; +a = a + 1; +b = b + 5; +c = c + " String!"; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/updating-object-properties.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/updating-object-properties.chinese.md index ee2731c9dd..1ca2b645d2 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/updating-object-properties.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/updating-object-properties.chinese.md @@ -2,24 +2,44 @@ id: 56bbb991ad1ed5201cd392d1 title: Updating Object Properties challengeType: 1 -videoUrl: '' +videoUrl: 'https://scrimba.com/c/c9yEJT4' +forumTopicId: 18336 localeTitle: 更新对象属性 --- ## Description -
在创建JavaScript对象之后,您可以随时更新其属性,就像更新任何其他变量一样。您可以使用点或括号表示法进行更新。例如,让我们看看我们的ourDog
var ourDog = {
“名字”:“露营者”,
“腿”:4,
“尾巴”:1,
“朋友们”:[“一切!”]
};
由于他是一只特别开心的狗,让我们改名为“快乐露营者”。以下是我们更新对象名称属性的方法: ourDog.name = "Happy Camper";或者我们的ourDog["name"] = "Happy Camper";现在,当我们评估我们的ourDog.name ,而不是获得“Camper”时,我们将获得他的新名字“Happy Camper”。
+
+当你创建了一个对象后,你可以用点操作符或中括号操作符来更新对象的属性。 +举个例子,让我们看看ourDog: + +```js +var 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”。您可以使用点或括号表示法。
+
+更新myDog对象的name属性,让它的名字从 "Coder" 变成 "Happy Coder"。 +
## Tests
```yml tests: - - text: 将myDog"name"属性更新为“Happy Coder”。 + - text: 更新myDog"name"属性, 使其等于 "Happy Coder"。 testString: assert(/happy coder/gi.test(myDog.name)); - - text: 不要编辑myDog定义 + - text: 不要修改myDog的定义。 testString: 'assert(/"name": "Coder"/.test(code));' ``` @@ -52,6 +72,7 @@ var myDog = { // Only change code below this line. + ``` @@ -61,7 +82,7 @@ var myDog = {
```js -console.info('after the test'); +(function(z){return z;})(myDog); ```
@@ -71,7 +92,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myDog = { + "name": "Coder", + "legs": 4, + "tails": 1, + "friends": ["freeCodeCamp Campers"] +}; +myDog.name = "Happy Coder"; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-first-character-in-a-string.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-first-character-in-a-string.chinese.md index c2dd890371..7e1e97bf62 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-first-character-in-a-string.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-first-character-in-a-string.chinese.md @@ -2,24 +2,32 @@ id: bd7123c9c549eddfaeb5bdef title: Use Bracket Notation to Find the First Character in a String challengeType: 1 -videoUrl: '' -localeTitle: 使用括号表示法查找字符串中的第一个字符 +videoUrl: 'https://scrimba.com/c/ca8JwhW' +forumTopicId: 18341 +localeTitle: 使用方括号查找字符串中的第一个字符 --- ## Description -
Bracket notation是一种在字符串中的特定index处获取字符的方法。大多数现代编程语言,如JavaScript,都不像人类那样开始计算。它们从0开始。这称为基于零的索引。例如,单词“Charles”中索引0处的字符是“C”。因此,如果var firstName = "Charles" ,则可以使用firstName[0]获取字符串第一个字母的值。
+
+方括号表示法是一种在字符串中的特定index(索引)处获取字符的方法。 +大多数现代编程语言,如JavaScript,不同于人类从 1 开始计数。它们是从 0 开始计数,这被称为 基于零 的索引。 +例如, 在单词 "Charles" 中索引 0 上的字符为 "C",所以在var firstName = "Charles"中,你可以使用firstName[0]来获得第一个位置上的字符。 +
## Instructions -
使用括号表示法查找lastName变量中的第一个字符并将其分配给firstLetterOfLastName暗示
如果卡住,请尝试查看firstLetterOfFirstName变量声明。
+
+使用方括号获取变量lastName中的第一个字符,并赋给变量firstLetterOfLastName。 +提示
如果你遇到困难了,不妨看看变量firstLetterOfFirstName是如何赋值的。 +
## Tests
```yml tests: - - text: firstLetterOfLastName变量的值应为L + - text: firstLetterOfLastName的值应该是L。 testString: assert(firstLetterOfLastName === 'L'); - - text: 您应该使用括号表示法。 + - text: 你应该使用中括号。 testString: assert(code.match(/firstLetterOfLastName\s*?=\s*?lastName\[.*?\]/)); ``` @@ -45,6 +53,7 @@ var lastName = "Lovelace"; // Only change code below this line firstLetterOfLastName = lastName; + ``` @@ -54,7 +63,7 @@ firstLetterOfLastName = lastName;
```js -console.info('after the test'); +(function(v){return v;})(firstLetterOfLastName); ```
@@ -64,7 +73,13 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var firstLetterOfLastName = ""; +var lastName = "Lovelace"; + +// Only change code below this line +firstLetterOfLastName = lastName[0]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-last-character-in-a-string.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-last-character-in-a-string.chinese.md index a1e5b81e3c..69da28b2b0 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-last-character-in-a-string.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-last-character-in-a-string.chinese.md @@ -2,24 +2,31 @@ id: bd7123c9c451eddfaeb5bdef title: Use Bracket Notation to Find the Last Character in a String challengeType: 1 -videoUrl: '' -localeTitle: 使用括号表示法查找字符串中的最后一个字符 +videoUrl: 'https://scrimba.com/c/cBZQGcv' +forumTopicId: 18342 +localeTitle: 使用方括号查找字符串中的最后一个字符 --- ## Description -
为了获得字符串的最后一个字母,您可以从字符串的长度中减去一个字母。例如,如果var firstName = "Charles" ,则可以使用firstName[firstName.length - 1]获取字符串最后一个字母的值。
+
+要获取字符串的最后一个字符,可以用字符串的长度减 1 的索引值。 +例如,在var firstName = "Charles"中,你可以这样操作firstName[firstName.length - 1]来得到字符串的最后的一个字符。 +
## Instructions -
使用括号表示法查找lastName变量中的最后一个字符。 暗示
如果卡住了,请尝试查看lastLetterOfFirstName变量声明。
+
+使用方括号lastName变量中的最后一个字符。 +提示
如果你遇到困难了,不妨看看在lastLetterOfFirstName变量上是怎么做的。 +
## Tests
```yml tests: - - text: lastLetterOfLastName应为“e”。 + - text: lastLetterOfLastName应该是"e"。 testString: assert(lastLetterOfLastName === "e"); - - text: 你必须使用.length来获取最后一个字母。 + - text: 你需要使用.length获取最后一个字符。 testString: assert(code.match(/\.length/g).length === 2); ``` @@ -42,6 +49,7 @@ var lastName = "Lovelace"; // Only change code below this line. var lastLetterOfLastName = lastName; + ``` @@ -51,7 +59,7 @@ var lastLetterOfLastName = lastName;
```js -console.info('after the test'); +(function(v){return v;})(lastLetterOfLastName); ```
@@ -61,7 +69,13 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var firstName = "Ada"; +var lastLetterOfFirstName = firstName[firstName.length - 1]; + +var lastName = "Lovelace"; +var lastLetterOfLastName = lastName[lastName.length - 1]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-nth-character-in-a-string.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-nth-character-in-a-string.chinese.md index 0b3e1f0f4f..e4379c2aae 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-nth-character-in-a-string.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-nth-character-in-a-string.chinese.md @@ -2,24 +2,31 @@ id: bd7123c9c450eddfaeb5bdef title: Use Bracket Notation to Find the Nth Character in a String challengeType: 1 -videoUrl: '' -localeTitle: 使用括号表示法查找字符串中的第N个字符 +videoUrl: 'https://scrimba.com/c/cWPVJua' +forumTopicId: 18343 +localeTitle: 使用方括号查找字符串中的第N个字符 --- ## Description -
您还可以使用括号表示法来获取字符串中其他位置的字符。请记住,计算机从0开始计数,因此第一个字符实际上是第0个字符。
+
+你也可以使用方括号来获得一个字符串中的其他位置的字符。 +请记住,程序是从0开始计数,所以获取第一个字符实际上是[0]。 +
## Instructions -
让我们尝试使用括号表示法将thirdLetterOfLastName设置为等于lastName变量的第三个字母。 暗示
如果卡住,请尝试查看secondLetterOfFirstName变量声明。
+
+让我们使用方括号,把lastName变量的第三个字符赋值给thirdLetterOfLastName。 +提示
如果你遇到困难了,看看secondLetterOfFirstName变量是如何做的。 +
## Tests
```yml tests: - - text: thirdLetterOfLastName变量的值应为v 。 + - text: thirdLetterOfLastName的值应该是v。 testString: assert(thirdLetterOfLastName === 'v'); - - text: 您应该使用括号表示法。 + - text: 你应该使用方括号。 testString: assert(code.match(/thirdLetterOfLastName\s*?=\s*?lastName\[.*?\]/)); ``` @@ -42,6 +49,7 @@ var lastName = "Lovelace"; // Only change code below this line. var thirdLetterOfLastName = lastName; + ``` @@ -51,7 +59,7 @@ var thirdLetterOfLastName = lastName;
```js -console.info('after the test'); +(function(v){return v;})(thirdLetterOfLastName); ```
@@ -61,7 +69,10 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var lastName = "Lovelace"; +var thirdLetterOfLastName = lastName[2]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-nth-to-last-character-in-a-string.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-nth-to-last-character-in-a-string.chinese.md index 07b431031f..08299805f5 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-nth-to-last-character-in-a-string.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-nth-to-last-character-in-a-string.chinese.md @@ -2,24 +2,31 @@ id: bd7123c9c452eddfaeb5bdef title: Use Bracket Notation to Find the Nth-to-Last Character in a String challengeType: 1 -videoUrl: '' -localeTitle: 使用括号表示法查找字符串中的第N个到最后一个字符 +videoUrl: 'https://scrimba.com/c/cw4vkh9' +forumTopicId: 18344 +localeTitle: 使用方括号查找字符串中的第N个字符到最后一个字符 --- ## Description -
您可以使用我们刚刚用于检索字符串中最后一个字符的相同原理来检索N到最后一个字符。例如,您可以使用firstName[firstName.length - 3]获取var firstName = "Charles"字符串的倒数第三个字母的值
+
+我们既可以获取字符串的最后一个字符,也可以用获取字符串的倒数第N个字符。 +例如,你可以这样firstName[firstName.length - 3]操作来获得var firstName = "Charles"字符串中的倒数第三个字符。 +
## Instructions -
使用括号表示法查找lastName字符串中倒数第二个字符。 暗示
如果卡住,请尝试查看thirdToLastLetterOfFirstName变量声明。
+
+使用方括号来获得lastName字符串中的倒数第二个字符。 +提示
如果你遇到困难了,不妨看看thirdToLastLetterOfFirstName变量是如何做到的。 +
## Tests
```yml tests: - - text: secondToLastLetterOfLastName应为“c”。 + - text: secondToLastLetterOfLastName应该是"c"。 testString: assert(secondToLastLetterOfLastName === 'c'); - - text: 你必须使用.length来获得倒数第二个字母。 + - text: 你需要使用.length获取倒数第二个字符。 testString: assert(code.match(/\.length/g).length === 2); ``` @@ -42,6 +49,7 @@ var lastName = "Lovelace"; // Only change code below this line var secondToLastLetterOfLastName = lastName; + ``` @@ -51,7 +59,7 @@ var secondToLastLetterOfLastName = lastName;
```js -console.info('after the test'); +(function(v){return v;})(secondToLastLetterOfLastName); ```
@@ -61,7 +69,13 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var firstName = "Ada"; +var thirdToLastLetterOfFirstName = firstName[firstName.length - 3]; + +var lastName = "Lovelace"; +var secondToLastLetterOfLastName = lastName[lastName.length - 2]; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-conditional-logic-with-if-statements.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-conditional-logic-with-if-statements.chinese.md index fff6621b10..4bc248c905 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-conditional-logic-with-if-statements.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-conditional-logic-with-if-statements.chinese.md @@ -2,30 +2,52 @@ id: cf1111c1c12feddfaeb3bdef title: Use Conditional Logic with If Statements challengeType: 1 -videoUrl: '' -localeTitle: 使用条件逻辑和If语句 +videoUrl: 'https://scrimba.com/c/cy87mf3' +forumTopicId: 18348 +localeTitle: 用 if 语句来表达条件逻辑 --- ## Description -
If语句用于在代码中做出决定。关键字if告诉JavaScript在括号中定义的特定条件下执行花括号中的代码。这些条件称为Boolean条件,它们可能只是truefalse 。当条件计算结果为true ,程序将执行花括号内的语句。当布尔条件的计算结果为false ,大括号内的语句将不会执行。 伪代码
if( condition为true ){
声明被执行
}
功能测试(myCondition){
if(myCondition){
回归“这是真的”;
}
返回“这是假的”;
}
测试(真); //返回“这是真的”
测试(假); //返回“这是假的”
当使用值true调用testif语句将评估myCondition以查看它是否为true 。因为它是true ,函数返回"It was true" 。当我们使用false值调用test时, myCondition 不为 true并且不执行花括号中的语句,函数返回"It was false"
+
+If语句用于在代码中做条件判断。关键字if告诉 JavaScript 在小括号中的条件为真的情况下去执行定义在大括号里面的代码。这种条件被称为Boolean条件,因为他们只可能是true(真)或false(假)。 +当条件的计算结果为true,程序执行大括号内的语句。当布尔条件的计算结果为false,大括号内的代码将不会执行。 +伪代码 +
if(条件为真){
语句被执行
}
+示例 + +```js +function test (myCondition) { + if (myCondition) { + return "It was true"; + } + return "It was false"; +} +test(true); // returns "It was true" +test(false); // returns "It was false" +``` + +当test被调用,并且传递进来的参数值为trueif语句会计算myCondition的结果,看它是真还是假。如果条件为true,函数会返回"It was true"。当test被调用,并且传递进来的参数值为falsemyConditiontrue,并且不执行大括号后面的语句,函数返回"It was false"。 +
## Instructions -
在函数内部创建一个if语句"Yes, that was true"如果参数wasThatTruetrue则返回"Yes, that was true" "No, that was false"否则返回"No, that was false"
+
+在函数内部创建一个if语句,如果该参数wasThatTrue值为true,返回"Yes, that was true",否则,并返回"No, that was false"。 +
## Tests
```yml tests: - - text: trueOrFalse应该是一个函数 + - text: trueOrFalse应该是一个函数。 testString: assert(typeof trueOrFalse === "function"); - - text: trueOrFalse(true)应该返回一个字符串 + - text: trueOrFalse(true)应该返回一个字符串。 testString: assert(typeof trueOrFalse(true) === "string"); - - text: trueOrFalse(false)应该返回一个字符串 + - text: trueOrFalse(false)应该返回一个字符串。 testString: assert(typeof trueOrFalse(false) === "string"); - - text: trueOrFalse(true)应该返回“是的,那是真的” + - text: trueOrFalse(true)应该返回 "Yes, that was true"。 testString: assert(trueOrFalse(true) === "Yes, that was true"); - - text: trueOrFalse(false)应该返回“No,that was false” + - text: trueOrFalse(false)应该返回 "No, that was false"。 testString: assert(trueOrFalse(false) === "No, that was false"); ``` @@ -59,7 +81,6 @@ function trueOrFalse(wasThatTrue) { // Change this value to test trueOrFalse(true); - ``` @@ -71,7 +92,14 @@ trueOrFalse(true); ## Solution
+ ```js -// solution required +function trueOrFalse(wasThatTrue) { + if (wasThatTrue) { + return "Yes, that was true"; + } + return "No, that was false"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-multiple-conditional-ternary-operators.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-multiple-conditional-ternary-operators.chinese.md index b7e7f11f3f..7d797c2155 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-multiple-conditional-ternary-operators.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-multiple-conditional-ternary-operators.chinese.md @@ -2,28 +2,67 @@ id: 587d7b7e367417b2b2512b21 title: Use Multiple Conditional (Ternary) Operators challengeType: 1 -videoUrl: '' -localeTitle: 使用多个条件(三元)运算符 +videoUrl: 'https://scrimba.com/c/cyWJBT4' +forumTopicId: 301179 +localeTitle: 使用多个三元运算符 --- ## Description -
在之前的挑战中,您使用了单个conditional operator 。您也可以将它们链接在一起以检查多种条件。以下函数使用if,else if和else语句来检查多个条件:
function findGreaterOrEqual(a,b){
if(a === b){
返回“a和b相等”;
}
否则如果(a> b){
返回“a更大”;
}
其他{
返回“b更大”;
}
}
可以使用多个conditional operators重写上述函数:
function findGreaterOrEqual(a,b){
返回(a === b)? “a和b相等”:(a> b)? “a更大”:“b更大”;
}
+
+在之前的挑战中,你使用了一个条件运算符。你也可以将多个运算符串联在一起以检查多种条件。 +下面的函数使用 if,else if 和 else 语句来检查多个条件: + +```js +function findGreaterOrEqual(a, b) { + if (a === b) { + return "a and b are equal"; + } + else if (a > b) { + return "a is greater"; + } + else { + return "b is greater"; + } +} +``` + +上面的函数使用条件运算符写法如下: + +```js +function findGreaterOrEqual(a, b) { + return (a === b) ? "a and b are equal" + : (a > b) ? "a is greater" + : "b is greater"; +} +``` + +即便如此,应谨慎使用多个三元运算符,因为在没有适当缩进的情况下使用多个三元运算符可能会使您的代码难以阅读。 例如: + +```js +function findGreaterOrEqual(a, b) { + return (a === b) ? "a and b are equal" : (a > b) ? "a is greater" : "b is greater"; +} +``` + +
## Instructions -
checkSign函数中使用多个conditional operators来检查数字是正数,负数还是零。
+
+在 checkSign 函数中使用多个条件运算符来检查数字是正数 ("positive")、负数 ("negative") 或零 ("zero")。 +
## Tests
```yml tests: - - text: checkSign应该使用多个conditional operators - testString: assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?\s*?\?\s*?.+?\s*?:\s*?.+?/gi.test(code)); - - text: checkSign(10)应该返回“positive”。请注意,资本化很重要 + - text: checkSign应该使用多个条件运算符。 + testString: 'assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?\s*?\?\s*?.+?\s*?:\s*?.+?/gi.test(code));' + - text: checkSign(10)应该返回 "positive" 注意,结果对大小写敏感。 testString: assert(checkSign(10) === 'positive'); - - text: checkSign(-12)应返回“否定”。请注意,资本化很重要 + - text: checkSign(-12)应该返回 "negative" 注意,结果对大小写敏感。 testString: assert(checkSign(-12) === 'negative'); - - text: checkSign(0)应返回“零”。请注意,资本化很重要 + - text: checkSign(0)应该返回 "zero" 注意,结果对大小写敏感。 testString: assert(checkSign(0) === 'zero'); ``` @@ -41,7 +80,6 @@ function checkSign(num) { } checkSign(10); - ``` @@ -54,6 +92,9 @@ checkSign(10);
```js -// solution required +function checkSign(num) { + return (num > 0) ? 'positive' : (num < 0) ? 'negative' : 'zero'; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown.md new file mode 100644 index 0000000000..393958821f --- /dev/null +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown.md @@ -0,0 +1,101 @@ +--- +id: 5cd9a70215d3c4e65518328f +title: Use Recursion to Create a Countdown +challengeType: 1 +forumTopicId: 305925 +localeTitle: 使用递归创建一个倒计时 +--- + +## Description +
+ +在上一个[挑战](/learn/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion),学习了怎样用递归来代替循环。现在来学习一个更复杂的函数,函数返回一个从 1 到传递给函数的指定数字的连续数字数组。 + +正如上一个挑战提到的,会有一个 base case。base case 告诉递归函数什么时候不在需要调用其自身。这是简单 情况,返回得到的值。还有 recursive call,继续用不同的参数调用自身。如果函数无误,一直执行直到 base case 为止。 + +比如,如果想写一个递归函数,返回一个数字 1n 的连续数组。这个函数需要接收一个参数n 代表起始数字。然后会持续的调用自身,传入一个比 n 更小的值一直到传入的值是 1 为止。函数如下: + +```javascript +function countup(n) { + if (n < 1) { + return []; + } else { + const countArray = countup(n - 1); + countArray.push(n); + return countArray; + } +} +console.log(countup(5)); // [ 1, 2, 3, 4, 5 ] +``` + +起初,这似乎是违反直觉的,因为 n 的值递减,但是最终数组中的值却递增。 之所以发生这种情况,是因为在递归调用返回之后,才调用 push。 在将 `n` pushed 进数组时,`count(n - 1)` 已经调用赋值成功并返回了 `[1, 2, ..., n - 1]`。 + +
+ +## Instructions +
+ +已经定义了一个函数 countdown,函数有一个参数(n)。函数应该基于参数 n 递归调用返回 n1 的连续数字的数组。如果函数以小于 1 的参数调用,函数应该返回空数组。 +比如,用 n = 5 调用函数应该返回数组 [5, 4, 3, 2, 1]。 +函数必需使用递归函数调用自身,不能使用任何形式的循环。 + +
+ +## Tests +
+ +``` yml +tests: + - text: countdown(-1) 应该返回一个空数组。 + testString: assert.isEmpty(countdown(-1)); + - text: countdown(10) 应该返回 [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]。 + testString: assert.deepStrictEqual(countdown(10), [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); + - text: countdown(5) 应该返回 [5, 4, 3, 2, 1]。 + testString: assert.deepStrictEqual(countdown(5), [5, 4, 3, 2, 1]); + - text: 代码不能包含任意形式的循环(forwhile 或者高阶函数如:forEachmapfilter 以及 reduce)。 + testString: assert(!removeJSComments(code).match(/for|while|forEach|map|filter|reduce/g)); + - text: 应该用递归解决这个问题。 + testString: assert(removeJSComments(countdown.toString()).match(/countdown\s*\(.+\)\;/)); +``` + +
+ +## Challenge Seed +
+ +
+ +```js + + +//Only change code below this line +function countdown(n){ + return; +} +console.log(countdown(5)); // [5, 4, 3, 2, 1] +``` + +
+ +### After Test +
+ +```js +const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); +``` + +
+ +
+ +## Solution +
+ +```js +//Only change code below this line +function countdown(n){ + return n < 1 ? [] : [n].concat(countdown(n - 1)); +} +``` + +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-range-of-numbers.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-range-of-numbers.chinese.md new file mode 100644 index 0000000000..5faa792394 --- /dev/null +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-range-of-numbers.chinese.md @@ -0,0 +1,81 @@ +--- +id: 5cc0bd7a49b71cb96132e54c +title: Use Recursion to Create a Range of Numbers +challengeType: 1 +forumTopicId: 301180 +localeTitle: 使用递归来创建一个数字序列 +--- + +## Description +
+ +接着上一个挑战,有另外一个机会来创建递归函数解决问题。 + +
+ +## Instructions +
+定义好了 rangeOfNumbers 函数,包含两个参数。函数应该返回一个连续数字数组,startNum 参数开始 endNum 参数截止。开始的数字小于或等于截止数字。函数必需递归调用自身,不能使用任意形式的循环。要考虑到 startNumendNum 相同的情况。 +
+ +## Tests +
+ +``` yml +tests: + - text: 函数应该返回一个数组。 + testString: assert(Array.isArray(rangeOfNumbers(5, 10))); + - text: 不能包含循环语句(for 或者 while 或者高阶函数比如 forEachmapfilter 或者 reduce)。 + testString: assert(!removeJSComments(code).match(/for|while|forEach|map|filter|reduce/g)); + - text: rangeOfNumbers 应该使用递归函数(调用自身)来完成这个挑战。 + testString: assert(removeJSComments(rangeOfNumbers.toString()).match(/rangeOfNumbers\s*\(.+\)/)); + - text: rangeOfNumbers(1, 5) 应该返回 [1, 2, 3, 4, 5]。 + testString: assert.deepStrictEqual(rangeOfNumbers(1, 5), [1, 2, 3, 4, 5]); + - text: rangeOfNumbers(6, 9) 应该返回 [6, 7, 8, 9]。 + testString: assert.deepStrictEqual(rangeOfNumbers(6, 9), [6, 7, 8, 9]); + - text: rangeOfNumbers(4, 4) 应该返回 [4]。 + testString: assert.deepStrictEqual(rangeOfNumbers(4, 4), [4]); +``` + +
+ +## Challenge Seed +
+ +
+ +```js +function rangeOfNumbers(startNum, endNum) { + return []; +}; +``` + +
+ +### After Test +
+ +```js +const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); +``` + +
+ +
+ +## Solution +
+ +```js +function rangeOfNumbers(startNum, endNum) { + if (endNum - startNum === 0) { + return [startNum]; + } else { + var numbers = rangeOfNumbers(startNum, endNum - 1); + numbers.push(endNum); + return numbers; + } +} +``` + +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-the-conditional-ternary-operator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-the-conditional-ternary-operator.chinese.md index f86058f3a9..96b4ebbc1c 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-the-conditional-ternary-operator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-the-conditional-ternary-operator.chinese.md @@ -2,30 +2,57 @@ id: 587d7b7e367417b2b2512b24 title: Use the Conditional (Ternary) Operator challengeType: 1 -videoUrl: '' -localeTitle: 使用条件(三元)运算符 +videoUrl: 'https://scrimba.com/c/c3JRmSg' +forumTopicId: 301181 +localeTitle: 使用三元运算符 --- ## Description -
条件运算符 (也称为三元运算符 )可以用作一行if-else表达式。语法是: condition ? statement-if-true : statement-if-false;以下函数使用if-else语句来检查条件:
function findGreater(a,b){
if(a> b){
返回“a更大”;
}
其他{
返回“b更大”;
}
}
这可以使用conditional operator重写:
function findGreater(a,b){
返回a> b? “a更大”:“b更大”;
}
+
+条件运算符(也称为三元运算符)的用处就像写成一行的 if-else 表达式 +语法如下所示: +condition ? statement-if-true : statement-if-false; +以下函数使用 if-else 语句来检查条件: + +```js +function findGreater(a, b) { + if(a > b) { + return "a is greater"; + } + else { + return "b is greater"; + } +} +``` + +上面的函数使用条件运算符写法如下: + +```js +function findGreater(a, b) { + return a > b ? "a is greater" : "b is greater"; +} +``` + +
## Instructions -
checkEqual函数中使用conditional operator来检查两个数字是否相等。该函数应返回true或false。
+
+在checkEqual函数中使用条件运算符检查两个数字是否相等,函数应该返回 "Equal" 或 "Not Equal" +
## Tests
```yml tests: - - text: checkEqual应该使用conditional operator + - text: checkEqual应该使用条件运算符。 testString: assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?/.test(code)); - - text: 'checkEqual(1, 2)应该返回false' + - text: checkEqual(1, 2)应该返回 "Not Equal"。 testString: assert(checkEqual(1, 2) === "Not Equal"); - - text: 'checkEqual(1, 1)应该返回true' + - text: checkEqual(1, 1)应该返回 "Equal"。 testString: assert(checkEqual(1, 1) === "Equal"); - - text: 'checkEqual(1, -1)应该返回false' + - text: checkEqual(1, -1)应该返回 "Not Equal"。 testString: assert(checkEqual(1, -1) === "Not Equal"); - ```
@@ -41,7 +68,6 @@ function checkEqual(a, b) { } checkEqual(1, 2); - ``` @@ -54,6 +80,9 @@ checkEqual(1, 2);
```js -// solution required +function checkEqual(a, b) { + return a === b ? "Equal" : "Not Equal"; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-the-parseint-function-with-a-radix.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-the-parseint-function-with-a-radix.chinese.md index 0c7a073f57..269ffa44ec 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-the-parseint-function-with-a-radix.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-the-parseint-function-with-a-radix.chinese.md @@ -2,30 +2,40 @@ id: 587d7b7e367417b2b2512b22 title: Use the parseInt Function with a Radix challengeType: 1 -videoUrl: '' -localeTitle: 将parseInt函数与Radix一起使用 +videoUrl: 'https://scrimba.com/c/c6K4Kh3' +forumTopicId: 301182 +localeTitle: 使用 parseInt 函数并传入一个基数 --- ## Description -
parseInt()函数解析字符串并返回一个整数。它需要一个基数的第二个参数,它指定字符串中数字的基数。基数可以是2到36之间的整数。函数调用如下: parseInt(string, radix);这是一个例子: var a = parseInt("11", 2);基数变量表示“11”在二进制系统或基数2中。此示例将字符串“11”转换为整数3。
+
+parseInt()函数解析一个字符串并返回一个整数。它同时可接受第二个参数,一个介于2和36之间的整数,表示字符串的基数。 +函数调用如下所示: +parseInt(string, radix); +示例: +var a = parseInt("11", 2); +参数 2 表示 "11" 使用二进制数值系统。此示例将字符串 "11" 转换为整数 3。 +
## Instructions -
convertToInteger函数中使用parseInt() ,以便将二进制数转换为整数并返回它。
+
+在convertToIntegerparseInt()函数把二进制数转换成十进制并返回。 +
## Tests
```yml tests: - - text: convertToInteger应该使用parseInt()函数 + - text: convertToInteger中应该使用parseInt()函数。 testString: assert(/parseInt/g.test(code)); - - text: convertToInteger("10011")应该返回一个数字 + - text: convertToInteger("10011")应该返回一个数字。 testString: assert(typeof(convertToInteger("10011")) === "number"); - - text: convertToInteger("10011")应该返回19 + - text: convertToInteger("10011")应该返回 19。 testString: assert(convertToInteger("10011") === 19); - - text: convertToInteger("111001")应该返回57 + - text: convertToInteger("111001")应该返回 57。 testString: assert(convertToInteger("111001") === 57); - - text: convertToInteger("JamesBond")应该返回NaN + - text: convertToInteger("JamesBond")应该返回 NaN。 testString: assert.isNaN(convertToInteger("JamesBond")); ``` @@ -43,7 +53,6 @@ function convertToInteger(str) { } convertToInteger("10011"); - ``` @@ -56,6 +65,9 @@ convertToInteger("10011");
```js -// solution required +function convertToInteger(str) { + return parseInt(str, 2); +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-the-parseint-function.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-the-parseint-function.chinese.md index 9e4db19400..673884e5d0 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-the-parseint-function.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-the-parseint-function.chinese.md @@ -2,30 +2,37 @@ id: 587d7b7e367417b2b2512b23 title: Use the parseInt Function challengeType: 1 -videoUrl: '' -localeTitle: 使用parseInt函数 +videoUrl: 'https://scrimba.com/c/cm83LSW' +forumTopicId: 301183 +localeTitle: 使用 parseInt 函数 --- ## Description -
parseInt()函数解析字符串并返回一个整数。这是一个例子: var a = parseInt("007");上面的函数将字符串“007”转换为整数7.如果字符串中的第一个字符无法转换为数字,则返回NaN
+
+parseInt()函数解析一个字符串返回一个整数下面是一个示例: +var a = parseInt("007"); +上面的函数把字符串 "007" 转换成数字 7。 如果字符串参数的第一个字符是字符串类型的,结果将不会转换成数字,而是返回NaN. +
## Instructions -
convertToInteger函数中使用parseInt() ,以便将输入字符串str转换为整数,然后返回它。
+
+在convertToInteger函数中使用parseInt()将字符串str转换为正数并返回。 +
## Tests
```yml tests: - - text: convertToInteger应该使用parseInt()函数 + - text: convertToInteger应该使用parseInt()函数。 testString: assert(/parseInt/g.test(code)); - - text: convertToInteger("56")应该返回一个数字 + - text: convertToInteger("56")应该返回一个数字。 testString: assert(typeof(convertToInteger("56")) === "number"); - - text: convertToInteger("56")应该返回56 + - text: convertToInteger("56")应该返回 56。 testString: assert(convertToInteger("56") === 56); - - text: convertToInteger("77")应该返回77 + - text: convertToInteger("77")应该返回 77。 testString: assert(convertToInteger("77") === 77); - - text: convertToInteger("JamesBond")应该返回NaN + - text: convertToInteger("JamesBond")应该返回 NaN。 testString: assert.isNaN(convertToInteger("JamesBond")); ``` @@ -43,7 +50,6 @@ function convertToInteger(str) { } convertToInteger("56"); - ``` @@ -56,6 +62,9 @@ convertToInteger("56");
```js -// solution required +function convertToInteger(str) { + return parseInt(str); +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups.chinese.md index dcb90d8de9..3a72781926 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups.chinese.md @@ -2,38 +2,63 @@ id: 56533eb9ac21ba0edf2244ca title: Using Objects for Lookups challengeType: 1 -videoUrl: '' +videoUrl: 'https://scrimba.com/c/cdBk8sM' +forumTopicId: 18373 localeTitle: 使用对象进行查找 --- ## Description -
对象可以被认为是键/值存储,就像字典一样。如果您有表格数据,则可以使用对象“查找”值而不是switch语句或if/else链。当您知道输入数据限制在特定范围内时,这非常有用。以下是简单反向字母查找的示例:
var alpha = {
1: “Z”,
2: “Y”,
3: “X”,
4: “W”,
...
24: “C”,
25: “B”,
26: “A”
};
阿尔法[2]; //“Y”
阿尔法[24]; // “C”

var value = 2;
阿尔法[值]。 //“Y”
+
+对象和字典一样,可以用来存储键/值对。如果你的数据跟对象一样,你可以用对象来查找你想要的值,而不是使用switch或if/else语句。当你知道你的输入数据在某个范围时,这种查找方式极为有效。 +这是简单的反向字母表: + +```js +var alpha = { + 1:"Z", + 2:"Y", + 3:"X", + 4:"W", + ... + 24:"C", + 25:"B", + 26:"A" +}; +alpha[2]; // "Y" +alpha[24]; // "C" + +var value = 2; +alpha[value]; // "Y" +``` + +
## Instructions -
将switch语句转换为名为lookup的对象。使用它来查找val并将关联的字符串分配给result变量。
+
+把 switch 语句转化为lookup对象。使用它来查找val属性的值,并赋值给result变量。 +
## Tests
```yml tests: - - text: phoneticLookup("alpha")应该等于"Adams" + - text: phoneticLookup("alpha")应该等于"Adams"。 testString: assert(phoneticLookup("alpha") === 'Adams'); - - text: phoneticLookup("bravo")应该等于"Boston" + - text: phoneticLookup("bravo")应该等于"Boston"。 testString: assert(phoneticLookup("bravo") === 'Boston'); - - text: phoneticLookup("charlie")应该等于"Chicago" + - text: phoneticLookup("charlie")应该等于"Chicago"。 testString: assert(phoneticLookup("charlie") === 'Chicago'); - - text: phoneticLookup("delta")应该等于"Denver" + - text: phoneticLookup("delta")应该等于"Denver"。 testString: assert(phoneticLookup("delta") === 'Denver'); - - text: phoneticLookup("echo")应该等于"Easy" + - text: phoneticLookup("echo")应该等于"Easy"。 testString: assert(phoneticLookup("echo") === 'Easy'); - - text: phoneticLookup("foxtrot")应该等于"Frank" + - text: phoneticLookup("foxtrot")应该等于"Frank"。 testString: assert(phoneticLookup("foxtrot") === 'Frank'); - - text: phoneticLookup("")应该等于undefined + - text: phoneticLookup("")应该等于undefined。 testString: assert(typeof phoneticLookup("") === 'undefined'); - - text: 您不应该修改return语句 + - text: 请不要修改return语句。 testString: assert(code.match(/return\sresult;/)); - - text: 您不应该使用caseswitchif语句 + - text: 请不要使用caseswitch,或if语句。 testString: assert(!/case|switch|if/g.test(code.replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g,''))); ``` @@ -77,7 +102,6 @@ function phoneticLookup(val) { // Change this value to test phoneticLookup("charlie"); - ``` @@ -89,7 +113,24 @@ phoneticLookup("charlie"); ## Solution
+ ```js -// solution required +function phoneticLookup(val) { + var result = ""; + + var lookup = { + alpha: "Adams", + bravo: "Boston", + charlie: "Chicago", + delta: "Denver", + echo: "Easy", + foxtrot: "Frank" + }; + + result = lookup[val]; + + return result; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/word-blanks.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/word-blanks.chinese.md index e879a74385..7758cd8a87 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/word-blanks.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/word-blanks.chinese.md @@ -2,27 +2,43 @@ id: 56533eb9ac21ba0edf2244bb title: Word Blanks challengeType: 1 -videoUrl: '' -localeTitle: 字空白 +videoUrl: 'https://scrimba.com/c/caqn8zuP' +forumTopicId: 18377 +localeTitle: 填词造句 --- ## Description -
我们现在将使用我们的弦乐知识来构建一个“ 疯狂的自由 ”风格的文字游戏,我们称之为“Word Blanks”。您将创建一个(可选幽默的)“填空”样式句子。在“疯狂的自由人”游戏中,您将获得包含名词,动词,形容词和副词等缺失单词的句子。然后,您可以用完成的句子有意义的方式用您选择的单词填写缺失的部分。想想这句话- “这是真的____,我们____ ____自己”。这句话有三个缺失的部分 - 形容词,动词和副词,我们可以添加我们选择的单词来完成它。然后我们可以将完成的句子分配给变量,如下所示:
var sentence =“它真的是”+“热”+“,我们”+“笑”+“自己”+“傻。”;
+
+现在,我们来用字符串的相关知识实现一个 "Mad Libs" 类的文字游戏,称为 "Word Blanks"。 你将创建一个(可选幽默的)“填空”样式句子。 +在 "Mad Libs" 游戏中,提供一个缺少一些单词的句子,缺少的单词包括名词,动词,形容词和副词等。然后,你选择一些单词填写句子缺失的地方,使句子完整并且有意义。 +思考一下这句话 - "It was really ____, and we ____ ourselves ____"。这句话有三个缺失的部分 - 形容词,动词和副词,选择合适单词填入完成它。然后将完成的句子赋值给变量,如下所示: + +```js +var sentence = "It was really " + "hot" + ", and we " + "laughed" + " ourselves " + "silly" + "."; +``` + +
## Instructions -
在这个挑战中,我们为您提供名词,动词,形容词和副词。您需要使用您选择的单词以及我们提供的单词来形成完整的句子。您将需要使用字符串连接运算符+来使用提供的变量构建新字符串: myNounmyAdjectivemyVerbmyAdverb 。然后,您将形成的字符串分配给result变量。您还需要考虑字符串中的空格,以便最后一句话在所有单词之间有空格。结果应该是一个完整的句子。
+
+在这个挑战中,我们为你提供名词,动词,形容词和副词。你需要使用合适单词以及我们提供的单词来形成完整的句子。 +你需要使用字符串连接运算符+来拼接字符串变量:myNounmyAdjectivemyVerb,和myAdverb来构建一个新字符串。然后,将新字符串赋给result变量。 +你还需要考虑字符串中的空格,确保句子的所有单词之间有空格。结果应该是一个完整的句子。 +
## Tests
```yml tests: - - text: 'wordBlanks("","","","")应该返回一个字符串。' - testString: assert(typeof wordBlanks("","","","") === "string"); - - text: 'wordBlanks("dog", "big", "ran", "quickly")应包含由非单词字符(以及madlib中的任何其他单词)分隔的所有传入单词。' - testString: assert(/\bdog\b/.test(test1) && /\bbig\b/.test(test1) && /\bran\b/.test(test1) && /\bquickly\b/.test(test1)); - - text: 'wordBlanks("cat", "little", "hit", "slowly")应包含由非单词字符(以及madlib中的任何其他单词)分隔的所有传入单词。' - testString: assert(/\bcat\b/.test(test2) && /\blittle\b/.test(test2) && /\bhit\b/.test(test2) && /\bslowly\b/.test(test2)); + - text: wordBlanks("","","","")应该返回一个字符串。 + testString: assert(typeof wordBlanks === 'string'); + - text: 不能改变 myNounmyVerbmyAdjective 或者 myAdverb 的值。 + testString: assert(myNoun === "dog" && myVerb === "ran" && myAdjective === "big" && myAdverb === "quickly"); + - text: 不能直接使用 "dog"、"ran"、"big" 或者 "quickly" 来创建 wordBlanks。 + testString: const newCode = removeAssignments(code); assert(!/dog/.test(newCode) && !/ran/.test(newCode) && !/big/.test(newCode) && !/quickly/.test(newCode)); + - text: wordBlanks 应包含分配给变量 myNounmyVerbmyAdjectivemyAdverb 的所有单词,并用非单词字符(以及 madlib 中的其它单词)分隔。 + testString: assert(/\bdog\b/.test(wordBlanks) && /\bbig\b/.test(wordBlanks) && /\bran\b/.test(wordBlanks) && /\bquickly\b/.test(wordBlanks)); ``` @@ -34,27 +50,25 @@ tests:
```js -function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) { - // Your code below this line - var result = ""; +var myNoun = "dog"; +var myAdjective = "big"; +var myVerb = "ran"; +var myAdverb = "quickly"; - // Your code above this line - return result; -} - -// Change the words here to test your function -wordBlanks("dog", "big", "ran", "quickly"); +var wordBlanks = ""; // Only change this line; ```
- -### After Test
```js -console.info('after the test'); +const removeAssignments = str => str + .replace(/myNoun\s*=\s*["']dog["']/g, '') + .replace(/myAdjective\s*=\s*["']big["']/g, '') + .replace(/myVerb\s*=\s*["']ran["']/g, '') + .replace(/myAdverb\s*=\s*["']quickly["']/g, ''); ```
@@ -64,7 +78,15 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +var myNoun = "dog"; +var myAdjective = "big"; +var myVerb = "ran"; +var myAdverb = "quickly"; + +var wordBlanks = "Once there was a " + myNoun + " which was very " + myAdjective + ". "; +wordBlanks += "It " + myVerb + " " + myAdverb + " around the yard."; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions.chinese.md index 4d77614a56..51c174146c 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions.chinese.md @@ -2,26 +2,42 @@ id: 56bbb991ad1ed5201cd392cf title: Write Reusable JavaScript with Functions challengeType: 1 -videoUrl: '' -localeTitle: 用函数编写可重用的JavaScript +videoUrl: 'https://scrimba.com/c/cL6dqfy' +forumTopicId: 18378 +localeTitle: 用函数编写可重用代码 --- ## Description -
在JavaScript中,我们可以将代码划分为称为函数的可重用部分。这是一个函数的例子:
function functionName(){
console.log(“Hello World”);
}
您可以使用其名称后跟括号来调用或调用此函数,如下所示: functionName();每次调用该函数时,它都会在开发控制台上打印出"Hello World"消息。每次调用函数时,都会执行大括号之间的所有代码。
+
+在 JavaScript 中,我们可以把代码的重复部分抽取出来,放到一个函数中。 +举个例子: + +```js +function functionName() { + console.log("Hello World"); +} +``` + +你可以通过函数名functionName加上后面的小括号来调用这个函数,就像这样: +functionName(); +每次调用函数时,它都会在控制台上打印消息"Hello World"。每次调用函数时,大括号之间的所有代码都将被执行。 +
## Instructions -
  1. 创建一个名为reusableFunction的函数,它将"Hi World"打印到开发控制台。
  2. 调用该功能。
+
+
  1. 先创建一个名为reusableFunction的函数,这个函数可以打印"Hi World"到控制台上。
  2. 然后调用这个函数。
+
## Tests
```yml tests: - - text: reusableFunction应该是一个函数 + - text: reusableFunction应该是一个函数。 testString: assert(typeof reusableFunction === 'function'); - - text: reusableFunction应该将“Hi World”输出到开发控制台 + - text: reusableFunction应该在控制台中输出 "Hi World"。 testString: assert(hiWorldWasLogged); - - text: 定义后调用reusableFunction + - text: 在你定义reusableFunction之后记得调用它。 testString: assert(/^\s*reusableFunction\(\)\s*/m.test(code)); ``` @@ -52,10 +68,12 @@ ourReusableFunction(); ```js var logOutput = ""; -var originalConsole = console +var originalConsole = console; +var nativeLog = console.log; +var hiWorldWasLogged = false; function capture() { - var nativeLog = console.log; console.log = function (message) { + if(message === 'Hi World') hiWorldWasLogged = true; if(message && message.trim) logOutput = message.trim(); if(nativeLog.apply) { nativeLog.apply(originalConsole, arguments); @@ -67,11 +85,10 @@ function capture() { } function uncapture() { - console.log = originalConsole.log; + console.log = nativeLog; } capture(); - ``` @@ -80,7 +97,13 @@ capture();
```js -console.info('after the test'); +uncapture(); + +if (typeof reusableFunction !== "function") { + (function() { return "reusableFunction is not defined"; })(); +} else { + (function() { return logOutput || "console.log never called"; })(); +} ```
@@ -90,7 +113,12 @@ console.info('after the test'); ## Solution
+ ```js -// solution required +function reusableFunction() { + console.log("Hi World"); +} +reusableFunction(); ``` +