Add languages Russian, Arabic, Chinese, Portuguese (#18305)
This commit is contained in:
committed by
mrugesh mohapatra
parent
09d3eca712
commit
2ca3a2093f
@@ -0,0 +1,67 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392ca
|
||||
title: Access Array Data with Indexes
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/access-array-data-with-indexes'
|
||||
videoUrl: ''
|
||||
localeTitle: 使用索引访问数组数据
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们可以使用<code>indexes</code>访问数组内部的数据。数组索引使用字符串使用的相同括号表示法编写,但不是指定字符,而是指定数组中的条目。与字符串一样,数组使用<dfn>从零开始的</dfn>索引,因此数组中的第一个元素是元素<code>0</code> 。 <strong>例</strong> <blockquote> var array = [50,60,70]; <br>阵列[0]; //等于50 <br> var data = array [1]; //等于60 </blockquote> <strong>注意</strong> <br>数组名称和方括号之间不应有任何空格,如<code>array [0]</code> 。尽管JavaScript能够正确处理,但这可能会让其他程序员在阅读代码时感到困惑。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个名为<code>myData</code>的变量,并使用括号表示法将其设置为等于<code>myArray</code>的第一个值。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 变量<code>myData</code>应该等于<code>myArray</code>的第一个值。
|
||||
testString: 'assert((function(){if(typeof myArray !== "undefined" && typeof myData !== "undefined" && myArray[0] === myData){return true;}else{return false;}})(), "The variable <code>myData</code> should equal the first value of <code>myArray</code>.");'
|
||||
- text: 应使用括号表示法访问变量<code>myArray</code>的数据。
|
||||
testString: 'assert((function(){if(code.match(/\s*=\s*myArray\[0\]/g)){return true;}else{return false;}})(), "The data in variable <code>myArray</code> should be accessed using bracket notation.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [50,60,70];
|
||||
var ourData = ourArray[0]; // equals 50
|
||||
|
||||
// Setup
|
||||
var myArray = [50,60,70];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 56592a60ddddeae28f7aa8e1
|
||||
title: Access Multi-Dimensional Arrays With Indexes
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/access-array-data-with-indexes'
|
||||
videoUrl: ''
|
||||
localeTitle: 访问带索引的多维数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">考虑<dfn>多维</dfn>数组的一种方法是作为<em>数组的数组</em> 。使用括号访问数组时,第一组括号引用最外层(第一级)数组中的条目,另外一对括号引用内部的下一级条目。 <strong>例</strong> <blockquote> var arr = [ <br> [1,2,3], <br> [4,5,6] <br> [7,8,9] <br> [[10,11,12],13,14] <br> ]。 <br> ARR [3]; //等于[[10,11,12],13,14] <br> ARR [3] [0]; //等于[10,11,12] <br> ARR [3] [0] [1]; //等于11 </blockquote> <strong>注意</strong> <br>数组名称和方括号之间不应该有任何空格,如<code>array [0][0]</code> ,甚至不允许使用此<code>array [0] [0]</code> 。尽管JavaScript能够正确处理,但这可能会让其他程序员在阅读代码时感到困惑。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用括号表示法从<code>myArray</code>选择一个元素,使<code>myData</code>等于<code>8</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myData</code>应该等于<code>8</code> 。
|
||||
testString: 'assert(myData === 8, "<code>myData</code> should be equal to <code>8</code>.");'
|
||||
- text: 您应该使用括号表示法从<code>myArray</code>读取正确的值。
|
||||
testString: 'assert(/myArray\[2\]\[1\]/g.test(code) && !/myData\s*=\s*(?:.*[-+*/%]|\d)/g.test(code), "You should be using bracket notation to read the correct value from <code>myArray</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];
|
||||
|
||||
// Only change code below this line.
|
||||
var myData = myArray[0][0];
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244cd
|
||||
title: Accessing Nested Arrays
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/access-array-data-with-indexes'
|
||||
videoUrl: ''
|
||||
localeTitle: 访问嵌套数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">正如我们在前面的示例中所看到的,对象可以包含嵌套对象和嵌套数组。与访问嵌套对象类似,可以链接数组括号表示法来访问嵌套数组。以下是如何访问嵌套数组的示例: <blockquote> var ourPets = [ <br> { <br> animalType:“猫”, <br>名称:[ <br> “Meowzer” <br> “蓬松”, <br> “洁猫” <br> ] <br> }, <br> { <br>动物类型:“狗”, <br>名称:[ <br> “点”, <br> “库巴” <br> “羊羊” <br> ] <br> } <br> ]。 <br> ourPets [0] .names [1]; //“蓬松” <br> ourPets [1] .names [0]; //“Spot” </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用对象点和数组括号表示法从变量<code>myPlants</code>检索第二个树。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>secondTree</code>应该等于“松树”
|
||||
testString: 'assert(secondTree === "pine", "<code>secondTree</code> should equal "pine"");'
|
||||
- text: 使用点和括号表示法访问<code>myPlants</code>
|
||||
testString: 'assert(/=\s*myPlants\[1\].list\[1\]/.test(code), "Use dot and bracket notation to access <code>myPlants</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myPlants = [
|
||||
{
|
||||
type: "flowers",
|
||||
list: [
|
||||
"rose",
|
||||
"tulip",
|
||||
"dandelion"
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "trees",
|
||||
list: [
|
||||
"fir",
|
||||
"pine",
|
||||
"birch"
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var secondTree = ""; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244cc
|
||||
title: Accessing Nested Objects
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/accessing-nested-objects-in-json'
|
||||
videoUrl: ''
|
||||
localeTitle: 访问嵌套对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">可以通过将点或括号表示法链接在一起来访问对象的子属性。这是一个嵌套对象: <blockquote> var ourStorage = { <br> “桌子”:{ <br> “抽屉”:“订书机” <br> }, <br> “内阁”:{ <br> “顶级抽屉”:{ <br> “folder1”:“一个文件”, <br> “folder2”:“秘密” <br> }, <br> “底部抽屉”:“苏打水” <br> } <br> }; <br> ourStorage.cabinet [“top drawer”]。folder2; //“秘密” <br> ourStorage.desk.drawer; //“订书机” </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">访问<code>myStorage</code>对象并将<code>glove box</code>属性的内容分配给<code>gloveBoxContents</code>变量。对于名称中包含空格的属性,请使用括号表示法。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>gloveBoxContents</code>应该等于“地图”
|
||||
testString: 'assert(gloveBoxContents === "maps", "<code>gloveBoxContents</code> should equal "maps"");'
|
||||
- text: 使用点和括号表示法访问<code>myStorage</code>
|
||||
testString: 'assert(/=\s*myStorage\.car\.inside\[\s*("|")glove box\1\s*\]/g.test(code), "Use dot and bracket notation to access <code>myStorage</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myStorage = {
|
||||
"car": {
|
||||
"inside": {
|
||||
"glove box": "maps",
|
||||
"passenger seat": "crumbs"
|
||||
},
|
||||
"outside": {
|
||||
"trunk": "jack"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var gloveBoxContents = undefined; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c8
|
||||
title: Accessing Object Properties with Bracket Notation
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/accessing-objects-properties-with-bracket-notation'
|
||||
videoUrl: ''
|
||||
localeTitle: 使用括号表示法访问对象属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">访问对象属性的第二种方法是括号表示法( <code>[]</code> )。如果您尝试访问的对象的属性在其名称中有空格,则需要使用括号表示法。但是,您仍然可以在没有空格的对象属性上使用括号表示法。以下是使用括号表示法读取对象属性的示例: <blockquote> var myObj = { <br> “太空名称”:“柯克”, <br> “更多空间”:“Spock”, <br> “NoSpace”:“USS Enterprise” <br> }; <br> myObj [“空间名称”]; //柯克<br> myObj ['更多空间']; // Spock <br> MyObj中[ “无空间”]; // USS Enterprise </blockquote>请注意,其中包含空格的属性名称必须使用引号(单引号或双引号)。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用括号表示法<code>testObj</code>属性<code>"an entree"</code> <code>testObj</code> <code>"an entree"</code>和<code>"the drink"</code>的<code>testObj</code> ,并分别将它们分配给<code>entreeValue</code>和<code>drinkValue</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>entreeValue</code>应该是一个字符串
|
||||
testString: 'assert(typeof entreeValue === "string" , "<code>entreeValue</code> should be a string");'
|
||||
- text: <code>entreeValue</code>的值应该是<code>"hamburger"</code>
|
||||
testString: 'assert(entreeValue === "hamburger" , "The value of <code>entreeValue</code> should be <code>"hamburger"</code>");'
|
||||
- text: <code>drinkValue</code>应该是一个字符串
|
||||
testString: 'assert(typeof drinkValue === "string" , "<code>drinkValue</code> should be a string");'
|
||||
- text: <code>drinkValue</code>的值应该是<code>"water"</code>
|
||||
testString: 'assert(drinkValue === "water" , "The value of <code>drinkValue</code> should be <code>"water"</code>");'
|
||||
- text: 您应该使用括号表示法两次
|
||||
testString: 'assert(code.match(/testObj\s*?\[("|")[^""]+\1\]/g).length > 1, "You should use bracket notation twice");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var testObj = {
|
||||
"an entree": "hamburger",
|
||||
"my side": "veggies",
|
||||
"the drink": "water"
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var entreeValue = testObj; // Change this line
|
||||
var drinkValue = testObj; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c7
|
||||
title: Accessing Object Properties with Dot Notation
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用点表示法访问对象属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有两种方法可以访问对象的属性:点表示法( <code>.</code> )和括号表示法( <code>[]</code> ),类似于数组。当您知道要提前访问的属性的名称时,使用点符号。以下是使用点表示法( <code>.</code> )读取对象属性的示例: <blockquote> var myObj = { <br> prop1:“val1”, <br> prop2:“val2” <br> }; <br> var prop1val = myObj.prop1; // val1 <br> var prop2val = myObj.prop2; // val2 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用点表示法读入<code>testObj</code>的属性值。将变量<code>hatValue</code>设置为等于对象的属性<code>hat</code> ,并将变量<code>shirtValue</code>设置为等于对象的属性<code>shirt</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>hatValue</code>应该是一个字符串
|
||||
testString: 'assert(typeof hatValue === "string" , "<code>hatValue</code> should be a string");'
|
||||
- text: <code>hatValue</code>的值应该是<code>"ballcap"</code>
|
||||
testString: 'assert(hatValue === "ballcap" , "The value of <code>hatValue</code> should be <code>"ballcap"</code>");'
|
||||
- text: <code>shirtValue</code>应该是一个字符串
|
||||
testString: 'assert(typeof shirtValue === "string" , "<code>shirtValue</code> should be a string");'
|
||||
- text: <code>shirtValue</code>的值应该是<code>"jersey"</code>
|
||||
testString: 'assert(shirtValue === "jersey" , "The value of <code>shirtValue</code> should be <code>"jersey"</code>");'
|
||||
- text: 你应该使用点符号两次
|
||||
testString: 'assert(code.match(/testObj\.\w+/g).length > 1, "You should use dot notation twice");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var testObj = {
|
||||
"hat": "ballcap",
|
||||
"shirt": "jersey",
|
||||
"shoes": "cleats"
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var hatValue = testObj; // Change this line
|
||||
var shirtValue = testObj; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c9
|
||||
title: Accessing Object Properties with Variables
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/accessing-objects-properties-with-variables'
|
||||
videoUrl: ''
|
||||
localeTitle: 使用变量访问对象属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">对象的括号表示法的另一个用途是访问存储为变量值的属性。这对于迭代对象的属性或访问查找表非常有用。以下是使用变量访问属性的示例: <blockquote> var dogs = { <br> Fido:“Mutt”,Hunter:“Doberman”,Snoopie:“Beagle” <br> }; <br> var myDog =“猎人”; <br> var myBreed = dogs [myDog]; <br>的console.log(myBreed); //“杜宾犬” </blockquote>另一种可以使用此概念的方法是在程序执行期间动态收集属性的名称,如下所示: <blockquote> var someObj = { <br> propName:“约翰” <br> }; <br> function propPrefix(str){ <br> var s =“prop”; <br> return s + str; <br> } <br> var someProp = propPrefix(“Name”); // someProp现在保存值'propName' <br>的console.log(someObj中[someProp]); // “约翰” </blockquote>请注意,在使用变量名来访问属性时,我们<em>不会</em>使用引号,因为我们使用的是变量的<em>值</em> ,而不是<em>名称</em> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>playerNumber</code>变量使用括号表示法在<code>testObj</code>查找玩家<code>16</code> 。然后将该名称分配给<code>player</code>变量。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>playerNumber</code>应该是一个数字
|
||||
testString: 'assert(typeof playerNumber === "number", "<code>playerNumber</code> should be a number");'
|
||||
- text: 变量<code>player</code>应该是一个字符串
|
||||
testString: 'assert(typeof player === "string", "The variable <code>player</code> should be a string");'
|
||||
- text: <code>player</code>的价值应该是“蒙大拿”
|
||||
testString: 'assert(player === "Montana", "The value of <code>player</code> should be "Montana"");'
|
||||
- text: 您应该使用括号表示法来访问<code>testObj</code>
|
||||
testString: 'assert(/testObj\s*?\[.*?\]/.test(code),"You should use bracket notation to access <code>testObj</code>");'
|
||||
- text: 您不应该直接将值<code>Montana</code>分配给变量<code>player</code> 。
|
||||
testString: 'assert(!code.match(/player\s*=\s*"|\"\s*Montana\s*"|\"\s*;/gi),"You should not assign the value <code>Montana</code> to the variable <code>player</code> directly.");'
|
||||
- text: 您应该在括号表示法中使用变量<code>playerNumber</code>
|
||||
testString: 'assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code),"You should be using the variable <code>playerNumber</code> in your bracket notation");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var testObj = {
|
||||
12: "Namath",
|
||||
16: "Montana",
|
||||
19: "Unitas"
|
||||
};
|
||||
|
||||
// Only change code below this line;
|
||||
|
||||
var playerNumber; // Change this Line
|
||||
var player = testObj; // Change this Line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d2
|
||||
title: Add New Properties to a JavaScript Object
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 将新属性添加到JavaScript对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以像修改现有JavaScript对象一样向现有JavaScript对象添加新属性。以下是我们如何为<code>ourDog</code>添加<code>"bark"</code>属性: <code>ourDog.bark = "bow-wow";</code>或者我们的<code>ourDog["bark"] = "bow-wow";</code>现在当我们评估我们的<code>ourDog.bark</code> ,我们会得到他的吠声,“低头哇”。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">向<code>myDog</code>添加<code>"bark"</code>属性并将其设置为狗声,例如“woof”。您可以使用点或括号表示法。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 将属性<code>"bark"</code>添加到<code>myDog</code> 。
|
||||
testString: 'assert(myDog.bark !== undefined, "Add the property <code>"bark"</code> to <code>myDog</code>.");'
|
||||
- text: 不要在设置部分添加<code>"bark"</code>
|
||||
testString: 'assert(!/bark[^\n]:/.test(code), "Do not add <code>"bark"</code> to the setup section");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"]
|
||||
};
|
||||
|
||||
ourDog.bark = "bow-wow";
|
||||
|
||||
// Setup
|
||||
var myDog = {
|
||||
"name": "Happy Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"]
|
||||
};
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,59 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb3bdef
|
||||
title: Add Two Numbers with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript添加两个数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>Number</code>是JavaScript中的数据类型,表示数字数据。现在让我们尝试使用JavaScript添加两个数字。当放置在两个数字之间时,JavaScript使用<code>+</code>符号作为加法运算。 <strong>例</strong> <blockquote> myVar = 5 + 10; //分配15 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改<code>0</code>使总和等于<code>20</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>sum</code>应该等于<code>20</code>
|
||||
testString: 'assert(sum === 20, "<code>sum</code> should equal <code>20</code>");'
|
||||
- text: 使用<code>+</code>运算符
|
||||
testString: 'assert(/\+/.test(code), "Use the <code>+</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var sum = 10 + 0;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244de
|
||||
title: Adding a Default Option in Switch Statements
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/adding-a-default-option-in-switch-statements'
|
||||
videoUrl: ''
|
||||
localeTitle: 在交换机语句中添加默认选项
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在<code>switch</code>语句中,您可能无法将所有可能的值指定为<code>case</code>语句。相反,您可以添加<code>default</code>语句,如果找不到匹配的<code>case</code>语句,将执行该语句。可以把它想象成<code>if/else</code>链中的最后一个<code>else</code>语句。 <code>default</code>语句应该是最后一种情况。 <blockquote> switch(num){ <br>案例值1: <br>语句1; <br>打破; <br>案例值2: <br>语句2; <br>打破; <br> ... <br>默认: <br> defaultStatement; <br>打破; <br> } </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">写一个switch语句来设置以下条件的<code>answer</code> : <br> <code>"a"</code> - “苹果” <br> <code>"b"</code> - “鸟” <br> <code>"c"</code> - “猫” <br> <code>default</code> - “东西” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>switchOfStuff("a")</code>的值应为“apple”
|
||||
testString: 'assert(switchOfStuff("a") === "apple", "<code>switchOfStuff("a")</code> should have a value of "apple"");'
|
||||
- text: <code>switchOfStuff("b")</code>的值应为“bird”
|
||||
testString: 'assert(switchOfStuff("b") === "bird", "<code>switchOfStuff("b")</code> should have a value of "bird"");'
|
||||
- text: <code>switchOfStuff("c")</code>的值应为“cat”
|
||||
testString: 'assert(switchOfStuff("c") === "cat", "<code>switchOfStuff("c")</code> should have a value of "cat"");'
|
||||
- text: <code>switchOfStuff("d")</code>的值应为“stuff”
|
||||
testString: 'assert(switchOfStuff("d") === "stuff", "<code>switchOfStuff("d")</code> should have a value of "stuff"");'
|
||||
- text: <code>switchOfStuff(4)</code>的值应为“stuff”
|
||||
testString: 'assert(switchOfStuff(4) === "stuff", "<code>switchOfStuff(4)</code> should have a value of "stuff"");'
|
||||
- text: 您不应该使用任何<code>if</code>或<code>else</code>语句
|
||||
testString: 'assert(!/else/g.test(code) || !/if/g.test(code), "You should not use any <code>if</code> or <code>else</code> statements");'
|
||||
- text: 您应该使用<code>default</code>语句
|
||||
testString: 'assert(switchOfStuff("string-to-trigger-default-case") === "stuff", "You should use a <code>default</code> statement");'
|
||||
- text: 你应该至少有3个<code>break</code>语句
|
||||
testString: 'assert(code.match(/break/g).length > 2, "You should have at least 3 <code>break</code> statements");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function switchOfStuff(val) {
|
||||
var answer = "";
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
return answer;
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
switchOfStuff(1);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ed
|
||||
title: Appending Variables to Strings
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/appending-variables-to-strings'
|
||||
videoUrl: ''
|
||||
localeTitle: 将变量附加到字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">正如我们可以在字符串<dfn>文字中</dfn>构建多行的字符串一样,我们也可以使用加号等于( <code>+=</code> )运算符将变量附加到字符串。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">设置<code>someAdjective</code>并使用<code>+=</code>运算符将其附加到<code>myStr</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>someAdjective</code>应设置为至少3个字符长的字符串
|
||||
testString: 'assert(typeof someAdjective !== "undefined" && someAdjective.length > 2, "<code>someAdjective</code> should be set to a string at least 3 characters long");'
|
||||
- text: 使用<code>+=</code>运算符将<code>someAdjective</code>附加到<code>myStr</code>
|
||||
testString: 'assert(code.match(/myStr\s*\+=\s*someAdjective\s*/).length > 0, "Append <code>someAdjective</code> to <code>myStr</code> using the <code>+=</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var anAdjective = "awesome!";
|
||||
var ourStr = "freeCodeCamp is ";
|
||||
ourStr += anAdjective;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var someAdjective;
|
||||
var myStr = "Learning to code is ";
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c3
|
||||
title: Assignment with a Returned Value
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/assignment-with-a-returned-value'
|
||||
videoUrl: ''
|
||||
localeTitle: 具有返回值的分配
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果您从我们对<a href="javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator" target="_blank">使用赋值运算符存储值</a>的讨论中回忆起来<a href="javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator" target="_blank">,则在分配</a>值之前,将解决等号右侧的所有内容。这意味着我们可以获取函数的返回值并将其赋值给变量。假设我们预先定义了一个函数<code>sum</code> ,它将两个数字相加,然后: <code>ourSum = sum(5, 12);</code>将调用<code>sum</code>函数,它返回值<code>17</code>并将其分配给<code>ourSum</code>变量。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用参数<code>7</code>调用<code>processArg</code>函数,并将其返回值分配给已<code>processed</code>的变量。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>processed</code>的值应为<code>2</code>
|
||||
testString: 'assert(processed === 2, "<code>processed</code> should have a value of <code>2</code>");'
|
||||
- text: 您应该将<code>processArg</code>分配给已<code>processed</code>
|
||||
testString: 'assert(/processed\s*=\s*processArg\(\s*7\s*\)\s*;/.test(code), "You should assign <code>processArg</code> to <code>processed</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var changed = 0;
|
||||
|
||||
function change(num) {
|
||||
return (num + 5) / 3;
|
||||
}
|
||||
|
||||
changed = change(10);
|
||||
|
||||
// Setup
|
||||
var processed = 0;
|
||||
|
||||
function processArg(num) {
|
||||
return (num + 3) / 5;
|
||||
}
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d0
|
||||
title: Build JavaScript Objects
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 构建JavaScript对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您之前可能听说过<code>object</code>这个术语。对象类似于<code>arrays</code> ,除了不使用索引访问和修改数据,您可以通过所谓的<code>properties</code>访问对象中的数据。对象对于以结构化方式存储数据很有用,并且可以表示真实世界对象,如猫。这是一个示例cat对象: <blockquote> var cat = { <br> “名字”:“胡须”, <br> “腿”:4, <br> “尾巴”:1, <br> “敌人”:[“水”,“狗”] <br> }; </blockquote>在此示例中,所有属性都存储为字符串,例如 - <code>"name"</code> , <code>"legs"</code>和<code>"tails"</code> 。但是,您也可以使用数字作为属性。您甚至可以省略单字符串属性的引号,如下所示: <blockquote> var anotherObject = { <br>制作:“福特”, <br> 5:“五”, <br> “模特”:“焦点” <br> }; </blockquote>但是,如果您的对象具有任何非字符串属性,JavaScript将自动将它们作为字符串进行类型转换。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个代表名为<code>myDog</code>的狗的对象,其中包含属性<code>"name"</code> (字符串), <code>"legs"</code> , <code>"tails"</code>和<code>"friends"</code> 。您可以将这些对象属性设置为您想要的任何值,因为<code>"name"</code>是一个字符串, <code>"legs"</code>和<code>"tails"</code>是数字, <code>"friends"</code>是一个数组。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myDog</code>应该包含属性<code>name</code> ,它应该是一个<code>string</code> 。
|
||||
testString: 'assert((function(z){if(z.hasOwnProperty("name") && z.name !== undefined && typeof z.name === "string"){return true;}else{return false;}})(myDog), "<code>myDog</code> should contain the property <code>name</code> and it should be a <code>string</code>.");'
|
||||
- text: <code>myDog</code>应该包含属性<code>legs</code> ,它应该是一个<code>number</code> 。
|
||||
testString: 'assert((function(z){if(z.hasOwnProperty("legs") && z.legs !== undefined && typeof z.legs === "number"){return true;}else{return false;}})(myDog), "<code>myDog</code> should contain the property <code>legs</code> and it should be a <code>number</code>.");'
|
||||
- text: <code>myDog</code>应该包含属性<code>tails</code> ,它应该是一个<code>number</code> 。
|
||||
testString: 'assert((function(z){if(z.hasOwnProperty("tails") && z.tails !== undefined && typeof z.tails === "number"){return true;}else{return false;}})(myDog), "<code>myDog</code> should contain the property <code>tails</code> and it should be a <code>number</code>.");'
|
||||
- text: <code>myDog</code>应该包含属性<code>friends</code> ,它应该是一个<code>array</code> 。
|
||||
testString: 'assert((function(z){if(z.hasOwnProperty("friends") && z.friends !== undefined && Array.isArray(z.friends)){return true;}else{return false;}})(myDog), "<code>myDog</code> should contain the property <code>friends</code> and it should be an <code>array</code>.");'
|
||||
- text: <code>myDog</code>应该只包含所有给定的属性。
|
||||
testString: 'assert((function(z){return Object.keys(z).length === 4;})(myDog), "<code>myDog</code> should only contain all the given properties.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"]
|
||||
};
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
var myDog = {
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244dc
|
||||
title: Chaining If Else Statements
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 链接如果其他声明
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>if/else</code>语句可以链接在一起以用于复杂的逻辑。这是多个链式<code>if</code> / <code>else if</code>语句的<dfn>伪代码</dfn> : <blockquote> if( <em>condition1</em> ){ <br> <em>语句1</em> <br> } else if( <em>condition2</em> ){ <br> <em>语句2</em> <br> } else if( <em>condition3</em> ){ <br> <em>声明3</em> <br> 。 。 。 <br> } else { <br> <em>statementN</em> <br> } </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">写入链接<code>if</code> / <code>else if</code>语句以满足以下条件: <code>num < 5</code> - return“Tiny” <br> <code>num < 10</code> - 返回“Small” <br> <code>num < 15</code> - 返回“中” <br> <code>num < 20</code> - 返回“Large” <br> <code>num >= 20</code> - 返回“巨大” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该至少有四个<code>else</code>语句
|
||||
testString: 'assert(code.match(/else/g).length > 3, "You should have at least four <code>else</code> statements");'
|
||||
- text: 你应该至少有四个<code>if</code>语句
|
||||
testString: 'assert(code.match(/if/g).length > 3, "You should have at least four <code>if</code> statements");'
|
||||
- text: 你应该至少有一个<code>return</code>语句
|
||||
testString: 'assert(code.match(/return/g).length >= 1, "You should have at least one <code>return</code> statement");'
|
||||
- text: <code>testSize(0)</code>应该返回“Tiny”
|
||||
testString: 'assert(testSize(0) === "Tiny", "<code>testSize(0)</code> should return "Tiny"");'
|
||||
- text: <code>testSize(4)</code>应该返回“Tiny”
|
||||
testString: 'assert(testSize(4) === "Tiny", "<code>testSize(4)</code> should return "Tiny"");'
|
||||
- text: <code>testSize(5)</code>应返回“Small”
|
||||
testString: 'assert(testSize(5) === "Small", "<code>testSize(5)</code> should return "Small"");'
|
||||
- text: <code>testSize(8)</code>应该返回“Small”
|
||||
testString: 'assert(testSize(8) === "Small", "<code>testSize(8)</code> should return "Small"");'
|
||||
- text: <code>testSize(10)</code>应该返回“Medium”
|
||||
testString: 'assert(testSize(10) === "Medium", "<code>testSize(10)</code> should return "Medium"");'
|
||||
- text: <code>testSize(14)</code>应返回“Medium”
|
||||
testString: 'assert(testSize(14) === "Medium", "<code>testSize(14)</code> should return "Medium"");'
|
||||
- text: <code>testSize(15)</code>应该返回“Large”
|
||||
testString: 'assert(testSize(15) === "Large", "<code>testSize(15)</code> should return "Large"");'
|
||||
- text: <code>testSize(17)</code>应该返回“Large”
|
||||
testString: 'assert(testSize(17) === "Large", "<code>testSize(17)</code> should return "Large"");'
|
||||
- text: <code>testSize(20)</code>应该返回“巨大”
|
||||
testString: 'assert(testSize(20) === "Huge", "<code>testSize(20)</code> should return "Huge"");'
|
||||
- text: <code>testSize(25)</code>应该返回“巨大”
|
||||
testString: 'assert(testSize(25) === "Huge", "<code>testSize(25)</code> should return "Huge"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testSize(num) {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
return "Change Me";
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testSize(7);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,50 @@
|
||||
---
|
||||
id: bd7123c9c441eddfaeb4bdef
|
||||
title: Comment Your JavaScript Code
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 评论您的JavaScript代码
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">注释是JavaScript有意忽略的代码行。注释是一种很好的方式,可以将注释留给自己和其他人,这些人稍后需要弄清楚代码的作用。在JavaScript中编写注释有两种方法:使用<code>//</code>将告诉JavaScript忽略当前行上的其余文本: <blockquote> //这是一个内嵌评论。 </blockquote>您可以使用<code>/*</code>开头并以<code>*/</code>结尾的多行注释: <blockquote> /* 这是一个<br>多行评论* / </blockquote> <strong>最佳实践</strong> <br>在编写代码时,应定期添加注释以阐明代码部分的功能。良好的评论可以帮助传达您的代码的意图 - 包括他人<em>和</em>未来的自我。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">尝试创建每种评论类型之一。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 创建一个包含至少五个字母的<code>//</code>样式注释。
|
||||
testString: 'assert(code.match(/(\/\/)...../g), "Create a <code>//</code> style comment that contains at least five letters.");'
|
||||
- text: 创建包含至少五个字母的<code>/* */</code>样式注释。
|
||||
testString: 'assert(code.match(/(\/\*)([^\/]{5,})(?=\*\/)/gm), "Create a <code>/* */</code> style comment that contains at least five letters.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d0
|
||||
title: Comparison with the Equality Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与平等算子的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有很多<dfn>比较运算符</dfn>在JavaScript中。所有这些运算符都返回布尔值<code>true</code>或<code>false</code>值。最基本的运算符是等于运算符<code>==</code> 。等于运算符比较两个值,如果它们是等价的则返回<code>true</code>否则返回<code>false</code> 。请注意,相等性与赋值( <code>=</code> )不同,后者将运算符右侧的值分配给左侧的变量。 <blockquote> function equalityTest(myVal){ <br> if(myVal == 10){ <br>返回“平等”; <br> } <br>返回“不等于”; <br> } </blockquote>如果<code>myVal</code>等于<code>10</code> ,则等于运算符返回<code>true</code> ,因此大括号中的代码将执行,函数将返回<code>"Equal"</code> 。否则,该函数将返回<code>"Not Equal"</code> 。为了使JavaScript能够比较两种不同的<code>data types</code> (例如, <code>numbers</code>和<code>strings</code> ),它必须将一种类型转换为另一种类型。这被称为“类型强制”。但是,一旦它完成,它可以比较如下术语: <blockquote> 1 == 1 //是的<br> 1 == 2 //假<br> 1 =='1'//是的<br> “3”== 3 //是的</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>equality operator</code>添加到指示的行,以便当<code>val</code>等于<code>12</code>时,函数将返回“Equal” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testEqual(10)</code>应该返回“Not Equal”
|
||||
testString: 'assert(testEqual(10) === "Not Equal", "<code>testEqual(10)</code> should return "Not Equal"");'
|
||||
- text: <code>testEqual(12)</code>应返回“Equal”
|
||||
testString: 'assert(testEqual(12) === "Equal", "<code>testEqual(12)</code> should return "Equal"");'
|
||||
- text: <code>testEqual("12")</code>应返回“Equal”
|
||||
testString: 'assert(testEqual("12") === "Equal", "<code>testEqual("12")</code> should return "Equal"");'
|
||||
- text: 您应该使用<code>==</code>运算符
|
||||
testString: 'assert(code.match(/==/g) && !code.match(/===/g), "You should use the <code>==</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testEqual(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d4
|
||||
title: Comparison with the Greater Than Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与大于运营商的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">大于运算符( <code>></code> )比较两个数字的值。如果左边的数字大于右边的数字,则返回<code>true</code> 。否则,它返回<code>false</code> 。与等于运算符一样,大于运算符将在比较时转换数据类型的值。 <strong>例子</strong> <blockquote> 5> 3 //是的<br> 7>'3'//是的<br> 2> 3 //假<br> '1'> 9 //假</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>greater than</code>运算符添加到指示的行,以便返回语句有意义。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testGreaterThan(0)</code>应返回“10或Under”
|
||||
testString: 'assert(testGreaterThan(0) === "10 or Under", "<code>testGreaterThan(0)</code> should return "10 or Under"");'
|
||||
- text: <code>testGreaterThan(10)</code>应返回“10或Under”
|
||||
testString: 'assert(testGreaterThan(10) === "10 or Under", "<code>testGreaterThan(10)</code> should return "10 or Under"");'
|
||||
- text: <code>testGreaterThan(11)</code>应该返回“Over 10”
|
||||
testString: 'assert(testGreaterThan(11) === "Over 10", "<code>testGreaterThan(11)</code> should return "Over 10"");'
|
||||
- text: <code>testGreaterThan(99)</code>应该返回“Over 10”
|
||||
testString: 'assert(testGreaterThan(99) === "Over 10", "<code>testGreaterThan(99)</code> should return "Over 10"");'
|
||||
- text: <code>testGreaterThan(100)</code>应该返回“Over 10”
|
||||
testString: 'assert(testGreaterThan(100) === "Over 10", "<code>testGreaterThan(100)</code> should return "Over 10"");'
|
||||
- text: <code>testGreaterThan(101)</code>应返回“超过100”
|
||||
testString: 'assert(testGreaterThan(101) === "Over 100", "<code>testGreaterThan(101)</code> should return "Over 100"");'
|
||||
- text: <code>testGreaterThan(150)</code>应该返回“超过100”
|
||||
testString: 'assert(testGreaterThan(150) === "Over 100", "<code>testGreaterThan(150)</code> should return "Over 100"");'
|
||||
- text: 您应该至少使用<code>></code>运算符两次
|
||||
testString: 'assert(code.match(/val\s*>\s*("|")*\d+("|")*/g).length > 1, "You should use the <code>></code> operator at least twice");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testGreaterThan(val) {
|
||||
if (val) { // Change this line
|
||||
return "Over 100";
|
||||
}
|
||||
|
||||
if (val) { // Change this line
|
||||
return "Over 10";
|
||||
}
|
||||
|
||||
return "10 or Under";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testGreaterThan(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d5
|
||||
title: Comparison with the Greater Than Or Equal To Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与大于或等于运算符的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>greater than or equal to</code>运算符( <code>>=</code> )比较两个数字的值。如果左边的数字大于或等于右边的数字,则返回<code>true</code> 。否则,它返回<code>false</code> 。与等于运算符一样, <code>greater than or equal to</code>运算符将在比较时转换数据类型。 <strong>例子</strong> <blockquote> 6> = 6 //是的<br> 7> ='3'//是的<br> 2> = 3 //假<br> '7'> = 9 //假</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>greater than or equal to</code>运算符添加到指示的行,以便返回语句有意义。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testGreaterOrEqual(0)</code>应返回“小于10”
|
||||
testString: 'assert(testGreaterOrEqual(0) === "Less than 10", "<code>testGreaterOrEqual(0)</code> should return "Less than 10"");'
|
||||
- text: <code>testGreaterOrEqual(9)</code>应返回“小于10”
|
||||
testString: 'assert(testGreaterOrEqual(9) === "Less than 10", "<code>testGreaterOrEqual(9)</code> should return "Less than 10"");'
|
||||
- text: <code>testGreaterOrEqual(10)</code>应返回“10或Over”
|
||||
testString: 'assert(testGreaterOrEqual(10) === "10 or Over", "<code>testGreaterOrEqual(10)</code> should return "10 or Over"");'
|
||||
- text: <code>testGreaterOrEqual(11)</code>应返回“10或Over”
|
||||
testString: 'assert(testGreaterOrEqual(11) === "10 or Over", "<code>testGreaterOrEqual(11)</code> should return "10 or Over"");'
|
||||
- text: <code>testGreaterOrEqual(19)</code>应返回“10或Over”
|
||||
testString: 'assert(testGreaterOrEqual(19) === "10 or Over", "<code>testGreaterOrEqual(19)</code> should return "10 or Over"");'
|
||||
- text: <code>testGreaterOrEqual(100)</code>应该返回“20或Over”
|
||||
testString: 'assert(testGreaterOrEqual(100) === "20 or Over", "<code>testGreaterOrEqual(100)</code> should return "20 or Over"");'
|
||||
- text: <code>testGreaterOrEqual(21)</code>应返回“20或Over”
|
||||
testString: 'assert(testGreaterOrEqual(21) === "20 or Over", "<code>testGreaterOrEqual(21)</code> should return "20 or Over"");'
|
||||
- text: 您应该使用<code>>=</code>运算符至少两次
|
||||
testString: 'assert(code.match(/val\s*>=\s*("|")*\d+("|")*/g).length > 1, "You should use the <code>>=</code> operator at least twice");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testGreaterOrEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "20 or Over";
|
||||
}
|
||||
|
||||
if (val) { // Change this line
|
||||
return "10 or Over";
|
||||
}
|
||||
|
||||
return "Less than 10";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testGreaterOrEqual(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d2
|
||||
title: Comparison with the Inequality Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与不等式算子的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">不等运算符( <code>!=</code> )与等于运算符相反。它意味着“不等于”并返回<code>false</code> ,其中相等性将返回<code>true</code> , <em>反之亦然</em> 。与等式运算符一样,不等式运算符将在比较时转换数据类型的值。 <strong>例子</strong> <blockquote> 1!= 2 //是的<br> 1!=“1”//假<br> 1!='1'//假<br> 1!= true // false <br> 0!= false // false </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在<code>if</code>语句中添加不等式运算符<code>!=</code> ,以便当<code>val</code>不等于<code>99</code>时函数将返回“Not Equal” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testNotEqual(99)</code>应返回“Equal”
|
||||
testString: 'assert(testNotEqual(99) === "Equal", "<code>testNotEqual(99)</code> should return "Equal"");'
|
||||
- text: <code>testNotEqual("99")</code>应该返回“Equal”
|
||||
testString: 'assert(testNotEqual("99") === "Equal", "<code>testNotEqual("99")</code> should return "Equal"");'
|
||||
- text: <code>testNotEqual(12)</code>应该返回“Not Equal”
|
||||
testString: 'assert(testNotEqual(12) === "Not Equal", "<code>testNotEqual(12)</code> should return "Not Equal"");'
|
||||
- text: <code>testNotEqual("12")</code>应该返回“Not Equal”
|
||||
testString: 'assert(testNotEqual("12") === "Not Equal", "<code>testNotEqual("12")</code> should return "Not Equal"");'
|
||||
- text: <code>testNotEqual("bob")</code>应返回“Not Equal”
|
||||
testString: 'assert(testNotEqual("bob") === "Not Equal", "<code>testNotEqual("bob")</code> should return "Not Equal"");'
|
||||
- text: 你应该使用<code>!=</code>运算符
|
||||
testString: 'assert(code.match(/(?!!==)!=/), "You should use the <code>!=</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testNotEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Not Equal";
|
||||
}
|
||||
return "Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testNotEqual(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d6
|
||||
title: Comparison with the Less Than Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与小于算子的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <dfn>小于</dfn>运算符( <code><</code> )比较两个数字的值。如果左边的数字小于右边的数字,则返回<code>true</code> 。否则,它返回<code>false</code> 。与等于运算符一样, <dfn>少于</dfn>运算符在比较时转换数据类型。 <strong>例子</strong> <blockquote> 2 <5 //是的<br> '3'<7 //是的<br> 5 <5 //假<br> 3 <2 //假<br> '8'<4 //假</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>less than</code>运算符添加到指示的行,以便返回语句有意义。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testLessThan(0)</code>应该返回“25岁以下”
|
||||
testString: 'assert(testLessThan(0) === "Under 25", "<code>testLessThan(0)</code> should return "Under 25"");'
|
||||
- text: <code>testLessThan(24)</code>应该返回“25岁以下”
|
||||
testString: 'assert(testLessThan(24) === "Under 25", "<code>testLessThan(24)</code> should return "Under 25"");'
|
||||
- text: <code>testLessThan(25)</code>应该返回“55岁以下”
|
||||
testString: 'assert(testLessThan(25) === "Under 55", "<code>testLessThan(25)</code> should return "Under 55"");'
|
||||
- text: <code>testLessThan(54)</code>应该返回“55岁以下”
|
||||
testString: 'assert(testLessThan(54) === "Under 55", "<code>testLessThan(54)</code> should return "Under 55"");'
|
||||
- text: <code>testLessThan(55)</code>应返回“55或以上”
|
||||
testString: 'assert(testLessThan(55) === "55 or Over", "<code>testLessThan(55)</code> should return "55 or Over"");'
|
||||
- text: <code>testLessThan(99)</code>应返回“55或以上”
|
||||
testString: 'assert(testLessThan(99) === "55 or Over", "<code>testLessThan(99)</code> should return "55 or Over"");'
|
||||
- text: 您应该至少使用<code><</code>运算符两次
|
||||
testString: 'assert(code.match(/val\s*<\s*("|")*\d+("|")*/g).length > 1, "You should use the <code><</code> operator at least twice");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testLessThan(val) {
|
||||
if (val) { // Change this line
|
||||
return "Under 25";
|
||||
}
|
||||
|
||||
if (val) { // Change this line
|
||||
return "Under 55";
|
||||
}
|
||||
|
||||
return "55 or Over";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testLessThan(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d7
|
||||
title: Comparison with the Less Than Or Equal To Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与小于或等于运算符的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>less than or equal to</code>运算符( <code><=</code> )比较两个数字的值。如果左边的数字小于或等于右边的数字,则返回<code>true</code> 。如果左侧的数字大于右侧的数字,则返回<code>false</code> 。与等于运算符一样, <code>less than or equal to</code>转换数据类型。 <strong>例子</strong> <blockquote> 4 <= 5 //是的<br> '7'<= 7 //是的<br> 5 <= 5 //是的<br> 3 <= 2 //假<br> '8'<= 4 //假</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>less than or equal to</code>运算符添加到指示的行,以便返回语句有意义。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testLessOrEqual(0)</code>应该返回“小于或等于12”
|
||||
testString: 'assert(testLessOrEqual(0) === "Smaller Than or Equal to 12", "<code>testLessOrEqual(0)</code> should return "Smaller Than or Equal to 12"");'
|
||||
- text: <code>testLessOrEqual(11)</code>应返回“小于或等于12”
|
||||
testString: 'assert(testLessOrEqual(11) === "Smaller Than or Equal to 12", "<code>testLessOrEqual(11)</code> should return "Smaller Than or Equal to 12"");'
|
||||
- text: <code>testLessOrEqual(12)</code>应返回“小于或等于12”
|
||||
testString: 'assert(testLessOrEqual(12) === "Smaller Than or Equal to 12", "<code>testLessOrEqual(12)</code> should return "Smaller Than or Equal to 12"");'
|
||||
- text: <code>testLessOrEqual(23)</code>应返回“小于或等于24”
|
||||
testString: 'assert(testLessOrEqual(23) === "Smaller Than or Equal to 24", "<code>testLessOrEqual(23)</code> should return "Smaller Than or Equal to 24"");'
|
||||
- text: <code>testLessOrEqual(24)</code>应返回“小于或等于24”
|
||||
testString: 'assert(testLessOrEqual(24) === "Smaller Than or Equal to 24", "<code>testLessOrEqual(24)</code> should return "Smaller Than or Equal to 24"");'
|
||||
- text: <code>testLessOrEqual(25)</code>应该返回“超过24”
|
||||
testString: 'assert(testLessOrEqual(25) === "More Than 24", "<code>testLessOrEqual(25)</code> should return "More Than 24"");'
|
||||
- text: <code>testLessOrEqual(55)</code>应该返回“超过24”
|
||||
testString: 'assert(testLessOrEqual(55) === "More Than 24", "<code>testLessOrEqual(55)</code> should return "More Than 24"");'
|
||||
- text: 你应该至少使用<code><=</code>运算符两次
|
||||
testString: 'assert(code.match(/val\s*<=\s*("|")*\d+("|")*/g).length > 1, "You should use the <code><=</code> operator at least twice");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testLessOrEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Smaller Than or Equal to 12";
|
||||
}
|
||||
|
||||
if (val) { // Change this line
|
||||
return "Smaller Than or Equal to 24";
|
||||
}
|
||||
|
||||
return "More Than 24";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testLessOrEqual(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d1
|
||||
title: Comparison with the Strict Equality Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与严格平等算子的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">严格相等( <code>===</code> )是相等运算符( <code>==</code> )的对应物。但是,与尝试将两个值转换为常见类型的等式运算符不同,严格相等运算符不执行类型转换。如果要比较的值具有不同的类型,则认为它们不相等,并且严格相等运算符将返回false。 <strong>例子</strong> <blockquote> 3 === 3 //是的<br> 3 ==='3'//假</blockquote>在第二个示例中, <code>3</code>是<code>Number</code>类型, <code>'3'</code>是<code>String</code>类型。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在<code>if</code>语句中使用strict equality运算符,因此当<code>val</code>严格等于<code>7</code>时,函数将返回“Equal” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testStrict(10)</code>应返回“Not Equal”
|
||||
testString: 'assert(testStrict(10) === "Not Equal", "<code>testStrict(10)</code> should return "Not Equal"");'
|
||||
- text: <code>testStrict(7)</code>应返回“Equal”
|
||||
testString: 'assert(testStrict(7) === "Equal", "<code>testStrict(7)</code> should return "Equal"");'
|
||||
- text: <code>testStrict("7")</code>应返回“Not Equal”
|
||||
testString: 'assert(testStrict("7") === "Not Equal", "<code>testStrict("7")</code> should return "Not Equal"");'
|
||||
- text: 您应该使用<code>===</code>运算符
|
||||
testString: 'assert(code.match(/(val\s*===\s*\d+)|(\d+\s*===\s*val)/g).length > 0, "You should use the <code>===</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testStrict(val) {
|
||||
if (val) { // Change this line
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testStrict(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,71 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d3
|
||||
title: Comparison with the Strict Inequality Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与严格不等式算子的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">严格不等式运算符( <code>!==</code> )与严格相等运算符的逻辑相反。它意味着“严格不等于”并返回<code>false</code> ,其中严格相等将返回<code>true</code> , <em>反之亦然</em> 。严格的不等式不会转换数据类型。 <strong>例子</strong> <blockquote> 3!== 3 //假<br> 3!=='3'//是的<br> 4!== 3 //是的</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>strict inequality operator</code>添加到<code>if</code>语句,以便当<code>val</code>不严格等于<code>17</code>时,函数将返回“Not Equal” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testStrictNotEqual(17)</code>应返回“Equal”
|
||||
testString: 'assert(testStrictNotEqual(17) === "Equal", "<code>testStrictNotEqual(17)</code> should return "Equal"");'
|
||||
- text: <code>testStrictNotEqual("17")</code>应返回“Not Equal”
|
||||
testString: 'assert(testStrictNotEqual("17") === "Not Equal", "<code>testStrictNotEqual("17")</code> should return "Not Equal"");'
|
||||
- text: <code>testStrictNotEqual(12)</code>应该返回“Not Equal”
|
||||
testString: 'assert(testStrictNotEqual(12) === "Not Equal", "<code>testStrictNotEqual(12)</code> should return "Not Equal"");'
|
||||
- text: <code>testStrictNotEqual("bob")</code>应返回“Not Equal”
|
||||
testString: 'assert(testStrictNotEqual("bob") === "Not Equal", "<code>testStrictNotEqual("bob")</code> should return "Not Equal"");'
|
||||
- text: 你应该使用<code>!==</code>运算符
|
||||
testString: 'assert(code.match(/(val\s*!==\s*\d+)|(\d+\s*!==\s*val)/g).length > 0, "You should use the <code>!==</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testStrictNotEqual(val) {
|
||||
// Only Change Code Below this Line
|
||||
|
||||
if (val) {
|
||||
|
||||
// Only Change Code Above this Line
|
||||
|
||||
return "Not Equal";
|
||||
}
|
||||
return "Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testStrictNotEqual(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d8
|
||||
title: Comparisons with the Logical And Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与逻辑和运算符的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有时你需要一次测试多个东西。当且仅当其左侧和右侧的<dfn>操作数</dfn>为<code>true</code>时, <dfn>逻辑和</dfn>运算符( <code>&&</code> )才返回true。如果将if语句嵌套在另一个语句中,则可以实现相同的效果: <blockquote> if(num> 5){ <br> if(num <10){ <br>返回“是”; <br> } <br> } <br>返回“否”; </blockquote>如果<code>num</code>大于<code>5</code>且小于<code>10</code>则仅返回“Yes”。相同的逻辑可以写成: <blockquote> if(num> 5 && num <10){ <br>返回“是”; <br> } <br>返回“否”; </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将两个if语句合并为一个语句,如果<code>val</code>小于或等于<code>50</code>且大于或等于<code>25</code> ,则返回<code>"Yes"</code> 。否则,将返回<code>"No"</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用一次<code>&&</code>运算符
|
||||
testString: 'assert(code.match(/&&/g).length === 1, "You should use the <code>&&</code> operator once");'
|
||||
- text: 你应该只有一个<code>if</code>语句
|
||||
testString: 'assert(code.match(/if/g).length === 1, "You should only have one <code>if</code> statement");'
|
||||
- text: <code>testLogicalAnd(0)</code>应返回“否”
|
||||
testString: 'assert(testLogicalAnd(0) === "No", "<code>testLogicalAnd(0)</code> should return "No"");'
|
||||
- text: <code>testLogicalAnd(24)</code>应返回“否”
|
||||
testString: 'assert(testLogicalAnd(24) === "No", "<code>testLogicalAnd(24)</code> should return "No"");'
|
||||
- text: <code>testLogicalAnd(25)</code>应返回“是”
|
||||
testString: 'assert(testLogicalAnd(25) === "Yes", "<code>testLogicalAnd(25)</code> should return "Yes"");'
|
||||
- text: <code>testLogicalAnd(30)</code>应该返回“是”
|
||||
testString: 'assert(testLogicalAnd(30) === "Yes", "<code>testLogicalAnd(30)</code> should return "Yes"");'
|
||||
- text: <code>testLogicalAnd(50)</code>应该返回“是”
|
||||
testString: 'assert(testLogicalAnd(50) === "Yes", "<code>testLogicalAnd(50)</code> should return "Yes"");'
|
||||
- text: <code>testLogicalAnd(51)</code>应返回“否”
|
||||
testString: 'assert(testLogicalAnd(51) === "No", "<code>testLogicalAnd(51)</code> should return "No"");'
|
||||
- text: <code>testLogicalAnd(75)</code>应返回“否”
|
||||
testString: 'assert(testLogicalAnd(75) === "No", "<code>testLogicalAnd(75)</code> should return "No"");'
|
||||
- text: <code>testLogicalAnd(80)</code>应返回“否”
|
||||
testString: 'assert(testLogicalAnd(80) === "No", "<code>testLogicalAnd(80)</code> should return "No"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testLogicalAnd(val) {
|
||||
// Only change code below this line
|
||||
|
||||
if (val) {
|
||||
if (val) {
|
||||
return "Yes";
|
||||
}
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
return "No";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testLogicalAnd(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,83 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d9
|
||||
title: Comparisons with the Logical Or Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与逻辑或运算符的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <dfn>逻辑OR</dfn>运算符( <code>||</code> )返回<code>true</code> ,如果任一<dfn>操作数</dfn>为<code>true</code> 。否则,它返回<code>false</code> 。 <dfn>逻辑或</dfn>运算符由两个管道符号( <code>|</code> )组成。这通常可以在Backspace和Enter键之间找到。以下模式应该从以前的方法点看起来很熟悉: <blockquote> if(num> 10){ <br>返回“否”; <br> } <br> if(num <5){ <br>返回“否”; <br> } <br>返回“是”; </blockquote>仅当<code>num</code>介于<code>5</code>和<code>10</code>之间(包括5和10)时,才会返回“Yes”。相同的逻辑可以写成: <blockquote> if(num> 10 || num <5){ <br>返回“否”; <br> } <br>返回“是”; </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将两个<code>if</code>语句组合成一个语句,如果<code>val</code>不在<code>10</code>和<code>20</code>之间(包括<code>10</code>和<code>20</code> ,则返回<code>"Outside"</code> 。否则,返回<code>"Inside"</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>||</code>操作员一次
|
||||
testString: 'assert(code.match(/\|\|/g).length === 1, "You should use the <code>||</code> operator once");'
|
||||
- text: 你应该只有一个<code>if</code>语句
|
||||
testString: 'assert(code.match(/if/g).length === 1, "You should only have one <code>if</code> statement");'
|
||||
- text: <code>testLogicalOr(0)</code>应返回“Outside”
|
||||
testString: 'assert(testLogicalOr(0) === "Outside", "<code>testLogicalOr(0)</code> should return "Outside"");'
|
||||
- text: <code>testLogicalOr(9)</code>应返回“Outside”
|
||||
testString: 'assert(testLogicalOr(9) === "Outside", "<code>testLogicalOr(9)</code> should return "Outside"");'
|
||||
- text: <code>testLogicalOr(10)</code>应返回“Inside”
|
||||
testString: 'assert(testLogicalOr(10) === "Inside", "<code>testLogicalOr(10)</code> should return "Inside"");'
|
||||
- text: <code>testLogicalOr(15)</code>应返回“Inside”
|
||||
testString: 'assert(testLogicalOr(15) === "Inside", "<code>testLogicalOr(15)</code> should return "Inside"");'
|
||||
- text: <code>testLogicalOr(19)</code>应该返回“Inside”
|
||||
testString: 'assert(testLogicalOr(19) === "Inside", "<code>testLogicalOr(19)</code> should return "Inside"");'
|
||||
- text: <code>testLogicalOr(20)</code>应该返回“Inside”
|
||||
testString: 'assert(testLogicalOr(20) === "Inside", "<code>testLogicalOr(20)</code> should return "Inside"");'
|
||||
- text: <code>testLogicalOr(21)</code>应该返回“Outside”
|
||||
testString: 'assert(testLogicalOr(21) === "Outside", "<code>testLogicalOr(21)</code> should return "Outside"");'
|
||||
- text: <code>testLogicalOr(25)</code>应返回“Outside”
|
||||
testString: 'assert(testLogicalOr(25) === "Outside", "<code>testLogicalOr(25)</code> should return "Outside"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testLogicalOr(val) {
|
||||
// Only change code below this line
|
||||
|
||||
if (val) {
|
||||
return "Outside";
|
||||
}
|
||||
|
||||
if (val) {
|
||||
return "Outside";
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
return "Inside";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testLogicalOr(15);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244af
|
||||
title: Compound Assignment With Augmented Addition
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 具有增强加法的复合赋值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在编程中,通常使用赋值来修改变量的内容。请记住,首先评估等号右侧的所有内容,因此我们可以说: <code>myVar = myVar + 5;</code>添加<code>5</code>到<code>myVar</code> 。由于这是一种常见的模式,因此存在一步完成数学运算和赋值的运算符。一个这样的运算符是<code>+=</code>运算符。 <blockquote> var myVar = 1; <br> myVar + = 5; <br>的console.log(myVar的); //返回6 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">转换<code>a</code> , <code>b</code>和<code>c</code>的赋值以使用<code>+=</code>运算符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应该等于<code>15</code>
|
||||
testString: 'assert(a === 15, "<code>a</code> should equal <code>15</code>");'
|
||||
- text: <code>b</code>应该等于<code>26</code>
|
||||
testString: 'assert(b === 26, "<code>b</code> should equal <code>26</code>");'
|
||||
- text: <code>c</code>应该等于<code>19</code>
|
||||
testString: 'assert(c === 19, "<code>c</code> should equal <code>19</code>");'
|
||||
- text: 您应该为每个变量使用<code>+=</code>运算符
|
||||
testString: 'assert(code.match(/\+=/g).length === 3, "You should use the <code>+=</code> operator for each variable");'
|
||||
- text: 不要修改行上方的代码
|
||||
testString: 'assert(/var a = 3;/.test(code) && /var b = 17;/.test(code) && /var c = 12;/.test(code), "Do not modify the code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var a = 3;
|
||||
var b = 17;
|
||||
var c = 12;
|
||||
|
||||
// Only modify code below this line
|
||||
|
||||
a = a + 12;
|
||||
b = 9 + b;
|
||||
c = c + 7;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b2
|
||||
title: Compound Assignment With Augmented Division
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 具有增广划分的复合赋值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>/=</code>运算符将变量除以另一个数字。 <code>myVar = myVar / 5;</code>将<code>myVar</code>除以<code>5</code> 。这可以改写为: <code>myVar /= 5;</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">转换<code>a</code> , <code>b</code>和<code>c</code>的赋值以使用<code>/=</code>运算符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应该等于<code>4</code>
|
||||
testString: 'assert(a === 4, "<code>a</code> should equal <code>4</code>");'
|
||||
- text: <code>b</code>应该等于<code>27</code>
|
||||
testString: 'assert(b === 27, "<code>b</code> should equal <code>27</code>");'
|
||||
- text: <code>c</code>应该等于<code>3</code>
|
||||
testString: 'assert(c === 3, "<code>c</code> should equal <code>3</code>");'
|
||||
- text: 您应该为每个变量使用<code>/=</code>运算符
|
||||
testString: 'assert(code.match(/\/=/g).length === 3, "You should use the <code>/=</code> operator for each variable");'
|
||||
- text: 不要修改行上方的代码
|
||||
testString: 'assert(/var a = 48;/.test(code) && /var b = 108;/.test(code) && /var c = 33;/.test(code), "Do not modify the code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var a = 48;
|
||||
var b = 108;
|
||||
var c = 33;
|
||||
|
||||
// Only modify code below this line
|
||||
|
||||
a = a / 12;
|
||||
b = b / 4;
|
||||
c = c / 11;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b1
|
||||
title: Compound Assignment With Augmented Multiplication
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 具有增广乘法的复合赋值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>*=</code>运算符将变量乘以数字。 <code>myVar = myVar * 5;</code>将<code>myVar</code>乘以<code>5</code> 。这可以改写为: <code>myVar *= 5;</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">转换<code>a</code> , <code>b</code>和<code>c</code>的赋值以使用<code>*=</code>运算符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应该等于<code>25</code>
|
||||
testString: 'assert(a === 25, "<code>a</code> should equal <code>25</code>");'
|
||||
- text: <code>b</code>应该等于<code>36</code>
|
||||
testString: 'assert(b === 36, "<code>b</code> should equal <code>36</code>");'
|
||||
- text: <code>c</code>应该等于<code>46</code>
|
||||
testString: 'assert(c === 46, "<code>c</code> should equal <code>46</code>");'
|
||||
- text: 您应该为每个变量使用<code>*=</code>运算符
|
||||
testString: 'assert(code.match(/\*=/g).length === 3, "You should use the <code>*=</code> operator for each variable");'
|
||||
- text: 不要修改行上方的代码
|
||||
testString: 'assert(/var a = 5;/.test(code) && /var b = 12;/.test(code) && /var c = 4\.6;/.test(code), "Do not modify the code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var a = 5;
|
||||
var b = 12;
|
||||
var c = 4.6;
|
||||
|
||||
// Only modify code below this line
|
||||
|
||||
a = a * 5;
|
||||
b = 3 * b;
|
||||
c = c * 10;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b0
|
||||
title: Compound Assignment With Augmented Subtraction
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 具有增广减法的复合赋值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">与<code>+=</code>运算符一样, <code>-=</code>从变量中减去一个数字。 <code>myVar = myVar - 5;</code>将从<code>myVar</code>减去<code>5</code> 。这可以改写为: <code>myVar -= 5;</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">转换<code>a</code> , <code>b</code>和<code>c</code>的赋值以使用<code>-=</code>运算符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应该等于<code>5</code>
|
||||
testString: 'assert(a === 5, "<code>a</code> should equal <code>5</code>");'
|
||||
- text: <code>b</code>应该等于<code>-6</code>
|
||||
testString: 'assert(b === -6, "<code>b</code> should equal <code>-6</code>");'
|
||||
- text: <code>c</code>应该等于<code>2</code>
|
||||
testString: 'assert(c === 2, "<code>c</code> should equal <code>2</code>");'
|
||||
- text: 您应该为每个变量使用<code>-=</code>运算符
|
||||
testString: 'assert(code.match(/-=/g).length === 3, "You should use the <code>-=</code> operator for each variable");'
|
||||
- text: 不要修改行上方的代码
|
||||
testString: 'assert(/var a = 11;/.test(code) && /var b = 9;/.test(code) && /var c = 3;/.test(code), "Do not modify the code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var a = 11;
|
||||
var b = 9;
|
||||
var c = 3;
|
||||
|
||||
// Only modify code below this line
|
||||
|
||||
a = a - 6;
|
||||
b = b - 15;
|
||||
c = c - 1;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b7
|
||||
title: Concatenating Strings with Plus Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 用Plus运算符连接字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在JavaScript中,当<code>+</code>运算符与<code>String</code>值一起使用时,它被称为<dfn>连接</dfn>运算符。您可以通过<dfn>将</dfn>它们<dfn>连接</dfn>在一起来构建其他字符串中的新字符串。 <strong>例</strong> <blockquote> “我叫艾伦,'+'我连接起来。” </blockquote> <strong>注意</strong> <br>留意空间。连接不会在连接字符串之间添加空格,因此您需要自己添加它们。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">从字符串构建<code>myStr</code> <code>"This is the start. "</code>和<code>"This is the end."</code>使用<code>+</code>运算符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myStr</code>应该有一个值<code>This is the start. This is the end.</code>
|
||||
testString: 'assert(myStr === "This is the start. This is the end.", "<code>myStr</code> should have a value of <code>This is the start. This is the end.</code>");'
|
||||
- text: 使用<code>+</code>运算符构建<code>myStr</code>
|
||||
testString: 'assert(code.match(/([""]).*([""])\s*\+\s*([""]).*([""])/g).length > 1, "Use the <code>+</code> operator to build <code>myStr</code>");'
|
||||
- text: 应使用<code>var</code>关键字创建<code>myStr</code> 。
|
||||
testString: 'assert(/var\s+myStr/.test(code), "<code>myStr</code> should be created using the <code>var</code> keyword.");'
|
||||
- text: 确保将结果分配给<code>myStr</code>变量。
|
||||
testString: 'assert(/myStr\s*=/.test(code), "Make sure to assign the result to the <code>myStr</code> variable.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourStr = "I come first. " + "I come second.";
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var myStr;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b8
|
||||
title: Concatenating Strings with the Plus Equals Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用Plus Equals运算符连接字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们还可以使用<code>+=</code>运算符将字符串<dfn>连接</dfn>到现有字符串变量的末尾。这对于在多行上打破长字符串非常有帮助。 <strong>注意</strong> <br>留意空间。连接不会在连接字符串之间添加空格,因此您需要自己添加它们。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">通过连接这两个字符串来构建<code>myStr</code>几行: <code>"This is the first sentence. "</code>和<code>"This is the second sentence."</code>使用<code>+=</code>运算符。使用<code>+=</code>运算符,类似于它在编辑器中的显示方式。首先将第一个字符串分配给<code>myStr</code> ,然后添加第二个字符串。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myStr</code>应该有一个值<code>This is the first sentence. This is the second sentence.</code>
|
||||
testString: 'assert(myStr === "This is the first sentence. This is the second sentence.", "<code>myStr</code> should have a value of <code>This is the first sentence. This is the second sentence.</code>");'
|
||||
- text: 使用<code>+=</code>运算符构建<code>myStr</code>
|
||||
testString: 'assert(code.match(/\w\s*\+=\s*[""]/g).length > 1 && code.match(/\w\s*\=\s*[""]/g).length > 1, "Use the <code>+=</code> operator to build <code>myStr</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourStr = "I come first. ";
|
||||
ourStr += "I come second.";
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var myStr;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b9
|
||||
title: Constructing Strings with Variables
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 用变量构造字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有时您需要构建一个字符串, <a href="https://en.wikipedia.org/wiki/Mad_Libs" target="_blank">Mad Libs</a>样式。通过使用连接运算符( <code>+</code> ),您可以将一个或多个变量插入到正在构建的字符串中。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>myName</code>设置为等于您的名字的字符串,并在字符串<code>"My name is "</code>和<code>" and I am well!"</code>之间用<code>myName</code>构建<code>myStr</code> <code>" and I am well!"</code> </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myName</code>应设置为至少3个字符长的字符串
|
||||
testString: 'assert(typeof myName !== "undefined" && myName.length > 2, "<code>myName</code> should be set to a string at least 3 characters long");'
|
||||
- text: 使用两个<code>+</code>运算符在其中构建<code>myStr</code> with <code>myName</code>
|
||||
testString: 'assert(code.match(/[""]\s*\+\s*myName\s*\+\s*[""]/g).length > 0, "Use two <code>+</code> operators to build <code>myStr</code> with <code>myName</code> inside it");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourName = "freeCodeCamp";
|
||||
var ourStr = "Hello, our name is " + ourName + ", how are you?";
|
||||
|
||||
// Only change code below this line
|
||||
var myName;
|
||||
var myStr;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,71 @@
|
||||
---
|
||||
id: 56105e7b514f539506016a5e
|
||||
title: Count Backwards With a For Loop
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 用For循环向后计数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">只要我们可以定义正确的条件,for循环也可以向后计数。为了向后计数两次,我们需要更改<code>initialization</code> , <code>condition</code>和<code>final-expression</code> 。我们将从<code>i = 10</code>开始并在<code>i > 0</code>循环。我们将递减<code>i</code> 2每个回路与<code>i -= 2</code> 。 <blockquote> var ourArray = []; <br> for(var i = 10; i> 0; i- = 2){ <br> ourArray.push(ⅰ); <br> } </blockquote> <code>ourArray</code>现在包含<code>[10,8,6,4,2]</code> 。让我们改变<code>initialization</code>和<code>final-expression</code>这样我们就可以向后计数两位奇数。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>for</code>循环将奇数从9到1推送到<code>myArray</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该为此使用<code>for</code>循环。
|
||||
testString: 'assert(code.match(/for\s*\(/g).length > 1, "You should be using a <code>for</code> loop for this.");'
|
||||
- text: 你应该使用数组方法<code>push</code> 。
|
||||
testString: 'assert(code.match(/myArray.push/), "You should be using the array method <code>push</code>.");'
|
||||
- text: '<code>myArray</code>应该等于<code>[9,7,5,3,1]</code> 。'
|
||||
testString: 'assert.deepEqual(myArray, [9,7,5,3,1], "<code>myArray</code> should equal <code>[9,7,5,3,1]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [];
|
||||
|
||||
for (var i = 10; i > 0; i -= 2) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 565bbe00e9cc8ac0725390f4
|
||||
title: Counting Cards
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 计数卡
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在赌场游戏Blackjack中,玩家可以通过跟踪牌组中剩余的高牌和低牌的相对数量来获得优势。这称为<a href="https://en.wikipedia.org/wiki/Card_counting" target="_blank">卡计数</a> 。在牌组中剩下更多高牌有利于玩家。根据下表为每张卡分配一个值。当计数为正数时,玩家应该下注。当计数为零或负数时,玩家应该下注低。 <table class="table table-striped"><thead><tr><th>计数变化</th><th>牌</th></tr></thead><tbody><tr><td> +1 </td><td> 2,3,4,5,6 </td></tr><tr><td> 0 </td><td> 7,8,9 </td></tr><tr><td> -1 </td><td> 10,'J','Q','K','A' </td></tr></tbody></table>你会写一个卡计数功能。它将接收一个<code>card</code>参数,可以是数字或字符串,并根据卡的值递增或递减全局<code>count</code>变量(参见表格)。然后,该函数将返回一个包含当前计数的字符串,如果计数为正则返回字符串<code>Bet</code> ,如果计数为零或为负,则返回<code>Hold</code> 。当前计数和玩家的决定( <code>Bet</code>或<code>Hold</code> )应该由一个空格分隔。 <strong>示例输出</strong> <br> <code>-3 Hold</code> <br> <code>5 Bet</code> <strong>提示</strong> <br>当值为7,8或9时,请勿将<code>count</code>重置为0。 <br>不要返回数组。 <br>不要在输出中包含引号(单引号或双引号)。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 牌序列<code>5 Bet</code>应该返回<code>5 Bet</code>
|
||||
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; })(), "Cards Sequence 2, 3, 4, 5, 6 should return <code>5 Bet</code>");'
|
||||
- text: '卡片序列7,8,9应返回<code>0 Hold</code>'
|
||||
testString: 'assert((function(){ count = 0; cc(7);cc(8);var out = cc(9); if(out === "0 Hold") {return true;} return false; })(), "Cards Sequence 7, 8, 9 should return <code>0 Hold</code>");'
|
||||
- text: 卡序列10,J,Q,K,A应返回<code>-5 Hold</code>
|
||||
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; })(), "Cards Sequence 10, J, Q, K, A should return <code>-5 Hold</code>");'
|
||||
- text: '卡序列3,7,Q,8,A应返回<code>-1 Hold</code>'
|
||||
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; })(), "Cards Sequence 3, 7, Q, 8, A should return <code>-1 Hold</code>");'
|
||||
- text: 牌序列2,J, <code>1 Bet</code>应该返回<code>1 Bet</code>
|
||||
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; })(), "Cards Sequence 2, J, 9, 2, 7 should return <code>1 Bet</code>");'
|
||||
- text: 牌序列<code>1 Bet</code>应该返回<code>1 Bet</code>
|
||||
testString: 'assert((function(){ count = 0; cc(2);cc(2);var out = cc(10); if(out === "1 Bet") {return true;} return false; })(), "Cards Sequence 2, 2, 10 should return <code>1 Bet</code>");'
|
||||
- text: '卡序列3,2,A,10,K应返回<code>-1 Hold</code>'
|
||||
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; })(), "Cards Sequence 3, 2, A, 10, K should return <code>-1 Hold</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var count = 0;
|
||||
|
||||
function cc(card) {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
return "Change Me";
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
// Add/remove calls to test your function.
|
||||
// Note: Only the last will display
|
||||
cc(2); cc(3); cc(7); cc('K'); cc('A');
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,61 @@
|
||||
---
|
||||
id: cf1391c1c11feddfaeb4bdef
|
||||
title: Create Decimal Numbers with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript创建十进制数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们也可以在变量中存储十进制数。十进制数有时称为<dfn>浮点数</dfn>或<dfn>浮点数</dfn> 。 <strong>注意</strong> <br>并非所有实数都可以准确地以<dfn>浮点</dfn>表示。这可能导致舍入错误。 <a href="https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems" target="_blank">细节在这里</a> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个变量<code>myDecimal</code>并给它一个带小数部分的十进制值(例如<code>5.7</code> )。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myDecimal</code>应该是一个数字。
|
||||
testString: 'assert(typeof myDecimal === "number", "<code>myDecimal</code> should be a number.");'
|
||||
- text: <code>myDecimal</code>应该有一个小数点
|
||||
testString: 'assert(myDecimal % 1 != 0, "<code>myDecimal</code> should have a decimal point"); '
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var ourDecimal = 5.7;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,60 @@
|
||||
---
|
||||
id: bd7123c9c443eddfaeb5bdef
|
||||
title: Declare JavaScript Variables
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 声明JavaScript变量
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在计算机科学中, <dfn>数据</dfn>是对计算机有意义的任何东西。 JavaScript提供了七种不同的<dfn>数据类型</dfn> ,它们是<code>undefined</code> , <code>null</code> , <code>boolean</code> , <code>string</code> , <code>symbol</code> , <code>number</code>和<code>object</code> 。例如,计算机区分数字(例如数字<code>12</code> )和<code>strings</code> (例如<code>"12"</code> , <code>"dog"</code>或<code>"123 cats"</code> ,它们是字符集合。计算机可以对数字执行数学运算,但不能对字符串执行数学运算。 <dfn>变量</dfn>允许计算机以动态方式存储和操作数据。他们通过使用“标签”指向数据而不是使用数据本身来做到这一点。七种数据类型中的任何一种都可以存储在变量中。 <code>Variables</code>类似于您在数学中使用的x和y变量,这意味着它们是表示我们想要引用的数据的简单名称。计算机<code>variables</code>与数学<code>variables</code>不同之处在于它们可以在不同时间存储不同的值。我们告诉JavaScript通过将关键字<code>var</code>放在它前面来创建或<dfn>声明</dfn>变量,如下所示: <blockquote> var ourName; </blockquote>创建一个名为<code>ourName</code>的<code>variable</code> 。在JavaScript中,我们以分号结束语句。 <code>Variable</code>名可以由数字,字母和<code>$</code>或<code>_</code> ,但不能包含空格或以数字开头。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>var</code>关键字创建名为<code>myName</code>的变量。 <strong>暗示</strong> <br>如果你遇到<code>ourName</code>查看我们的<code>ourName</code>示例。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您应该使用<code>var</code>关键字声明<code>myName</code> ,以分号结尾
|
||||
testString: 'assert(/var\s+myName\s*;/.test(code), "You should declare <code>myName</code> with the <code>var</code> keyword, ending with a semicolon");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourName;
|
||||
|
||||
// Declare myName below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,63 @@
|
||||
---
|
||||
id: bd7123c9c444eddfaeb5bdef
|
||||
title: Declare String Variables
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 声明字符串变量
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">以前我们使用过代码<code>var myName = "your name";</code> <code>"your name"</code>被称为<dfn>字符串</dfn> <dfn>文字</dfn> 。它是一个字符串,因为它是用单引号或双引号括起来的一系列零个或多个字符。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建两个新的<code>string</code>变量: <code>myFirstName</code>和<code>myLastName</code>并分别为它们分配<code>myFirstName</code>和<code>myLastName</code>的值。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myFirstName</code>应该是一个至少包含一个字符的字符串。
|
||||
testString: 'assert((function(){if(typeof myFirstName !== "undefined" && typeof myFirstName === "string" && myFirstName.length > 0){return true;}else{return false;}})(), "<code>myFirstName</code> should be a string with at least one character in it.");'
|
||||
- text: <code>myLastName</code>应该是一个至少包含一个字符的字符串。
|
||||
testString: 'assert((function(){if(typeof myLastName !== "undefined" && typeof myLastName === "string" && myLastName.length > 0){return true;}else{return false;}})(), "<code>myLastName</code> should be a string with at least one character in it.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var firstName = "Alan";
|
||||
var lastName = "Turing";
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ad
|
||||
title: Decrement a Number with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript减少数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以使用<code>--</code>运算符轻松地将变量<dfn>减1</dfn>或减1。 <code>i--;</code>相当于<code>i = i - 1;</code> <strong>注意</strong> <br>整条线变成了<code>i--;</code> ,消除了对等号的需要。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改代码以在<code>myVar</code>上使用<code>--</code>运算符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myVar</code>应该等于<code>10</code>
|
||||
testString: 'assert(myVar === 10, "<code>myVar</code> should equal <code>10</code>");'
|
||||
- text: <code>myVar = myVar - 1;</code>应该改变
|
||||
testString: 'assert(/var\s*myVar\s*=\s*11;\s*\/*.*\s*([-]{2}\s*myVar|myVar\s*[-]{2});/.test(code), "<code>myVar = myVar - 1;</code> should be changed");'
|
||||
- text: 在<code>myVar</code>上使用<code>--</code>运算符
|
||||
testString: 'assert(/[-]{2}\s*myVar|myVar\s*[-]{2}/.test(code), "Use the <code>--</code> operator on <code>myVar</code>");'
|
||||
- text: 不要更改行上方的代码
|
||||
testString: 'assert(/var myVar = 11;/.test(code), "Do not change code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var myVar = 11;
|
||||
|
||||
// Only change code below this line
|
||||
myVar = myVar - 1;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,79 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d3
|
||||
title: Delete Properties from a JavaScript Object
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 从JavaScript对象中删除属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们还可以删除对象中的属性,如下所示: <code>delete ourDog.bark;</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">从<code>myDog</code>删除<code>"tails"</code>属性。您可以使用点或括号表示法。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 从<code>myDog</code>删除属性<code>"tails"</code> 。
|
||||
testString: 'assert(typeof myDog === "object" && myDog.tails === undefined, "Delete the property <code>"tails"</code> from <code>myDog</code>.");'
|
||||
- text: 不要修改<code>myDog</code>设置
|
||||
testString: 'assert(code.match(/"tails": 1/g).length > 1, "Do not modify the <code>myDog</code> setup");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"],
|
||||
"bark": "bow-wow"
|
||||
};
|
||||
|
||||
delete ourDog.bark;
|
||||
|
||||
// Setup
|
||||
var myDog = {
|
||||
"name": "Happy Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"],
|
||||
"bark": "woof"
|
||||
};
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,61 @@
|
||||
---
|
||||
id: bd7993c9ca9feddfaeb7bdef
|
||||
title: Divide One Decimal by Another with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript将另一个十进制除以另一个
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在让我们将一位小数除以另一位小数。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改<code>0.0</code>使<code>quotient</code>等于<code>2.2</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 变<code>quotient</code>应该等于<code>2.2</code>
|
||||
testString: 'assert(quotient === 2.2, "The variable <code>quotient</code> should equal <code>2.2</code>");'
|
||||
- text: 您应该使用<code>/</code>运算符将4.4除以2
|
||||
testString: 'assert(/4\.40*\s*\/\s*2\.*0*/.test(code), "You should use the <code>/</code> operator to divide 4.4 by 2");'
|
||||
- text: 商数变量只应分配一次
|
||||
testString: 'assert(code.match(/quotient/g).length === 1, "The quotient variable should only be assigned once");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var quotient = 0.0 / 2.0; // Fix this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,59 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb6bdef
|
||||
title: Divide One Number by Another with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 用JavaScript划分一个号码
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们也可以将一个数字除以另一个数字。 JavaScript使用<code>/</code>符号进行除法。 <p> <strong>例</strong> </p><blockquote> myVar = 16/2; //分配8 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改<code>0</code> ,使<code>quotient</code>等于<code>2</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 使变量<code>quotient</code>等于2。
|
||||
testString: 'assert(quotient === 2, "Make the variable <code>quotient</code> equal to 2.");'
|
||||
- text: 使用<code>/</code>运算符
|
||||
testString: 'assert(/\d+\s*\/\s*\d+/.test(code), "Use the <code>/</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var quotient = 66 / 0;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,67 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b6
|
||||
title: Escape Sequences in Strings
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 字符串中的转义序列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">引号不是可以在字符串中<dfn>转义</dfn>的唯一字符。使用转义字符有两个原因:首先是允许您使用您可能无法输入的字符,例如退格。其次是允许你在一个字符串中表示多个引号,而不会误解你的意思。我们在之前的挑战中学到了这一点。 <table class="table table-striped"><thead><tr><th>码</th><th>产量</th></tr></thead><tbody><tr><td> <code>\'</code> </td> <td>单引号</td></tr><tr><td> <code>\"</code> </td> <td>双引号</td></tr><tr><td> <code>\\</code> </td> <td>反斜线</td></tr><tr><td> <code>\n</code> </td> <td>新队</td></tr><tr><td> <code>\r</code> </td> <td>回车</td></tr><tr><td> <code>\t</code> </td> <td>标签</td></tr><tr><td> <code>\b</code> </td> <td>退格</td></tr><tr><td> <code>\f</code> </td> <td>形式饲料</td></tr></tbody></table> <em>请注意,必须对反斜杠本身进行转义才能显示为反斜杠。</em> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用转义序列将以下三行文本分配到单个变量<code>myStr</code> 。 <blockquote>第一行<br> \第二行<br> ThirdLine </blockquote>您将需要使用转义序列正确插入特殊字符。您还需要按照上面的间距来跟踪,在转义序列或单词之间没有空格。这是写出转义序列的文本。 <q>FirstLine <code>newline</code> <code>tab</code> <code>backslash</code> SecondLine <code>newline</code> ThirdLine</q> </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myStr</code>不应包含任何空格
|
||||
testString: 'assert(!/ /.test(myStr), "<code>myStr</code> should not contain any spaces");'
|
||||
- text: <code>myStr</code>应包含的字符串<code>FirstLine</code> , <code>SecondLine</code>和<code>ThirdLine</code> (记住区分大小写)
|
||||
testString: 'assert(/FirstLine/.test(myStr) && /SecondLine/.test(myStr) && /ThirdLine/.test(myStr), "<code>myStr</code> should contain the strings <code>FirstLine</code>, <code>SecondLine</code> and <code>ThirdLine</code> (remember case sensitivity)");'
|
||||
- text: <code>FirstLine</code>后面应跟换行符<code>\n</code>
|
||||
testString: 'assert(/FirstLine\n/.test(myStr), "<code>FirstLine</code> should be followed by the newline character <code>\n</code>");'
|
||||
- text: <code>myStr</code>应该包含一个制表字符<code>\t</code> ,它跟在换行符后面
|
||||
testString: 'assert(/\n\t/.test(myStr), "<code>myStr</code> should contain a tab character <code>\t</code> which follows a newline character");'
|
||||
- text: <code>SecondLine</code>应该以反斜杠字符<code>\\</code>开头
|
||||
testString: 'assert(/\SecondLine/.test(myStr), "<code>SecondLine</code> should be preceded by the backslash character <code>\\</code>");'
|
||||
- text: <code>SecondLine</code>和<code>ThirdLine</code>之间应该有换行符
|
||||
testString: 'assert(/SecondLine\nThirdLine/.test(myStr), "There should be a newline character between <code>SecondLine</code> and <code>ThirdLine</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var myStr; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,59 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b5
|
||||
title: Escaping Literal Quotes in Strings
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 逃避字符串中的字面引用
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在定义字符串时,必须以单引号或双引号开头和结尾。当你需要一个文字报价会发生什么: <code>"</code>还是<code>'</code> ?你的字符串里面在JavaScript中,你可以放置一个<dfn>反斜杠</dfn> (从考虑到它作为字符串报价的最终<dfn>逃脱</dfn>报价<code>\</code>在引号前)。 <code>var sampleStr = "Alan said, \"Peter is learning JavaScript\".";</code>这告诉JavaScript,以下引用不是字符串的结尾,而是应该出现在字符串中。所以如果要将它打印到控制台,你会得到: <code>Alan said, "Peter is learning JavaScript".</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<dfn>反斜杠</dfn>将字符串分配给<code>myStr</code>变量,这样如果要将其打印到控制台,您会看到: <code>I am a "double quoted" string inside "double quotes".</code> </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您应该使用两个双引号( <code>"</code> )和四个转义双引号( <code>\"</code> )。
|
||||
testString: 'assert(code.match(/\\"/g).length === 4 && code.match(/[^\\]"/g).length === 2, "You should use two double quotes (<code>"</code>) and four escaped double quotes (<code>\"</code>).");'
|
||||
- text: 变量myStr应该包含字符串: <code>I am a "double quoted" string inside "double quotes".</code>
|
||||
testString: 'assert(myStr === "I am a \"double quoted\" string inside \"double quotes\".", "Variable myStr should contain the string: <code>I am a "double quoted" string inside "double quotes".</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var myStr = ""; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,71 @@
|
||||
---
|
||||
id: bd7123c9c448eddfaeb5bdef
|
||||
title: Find the Length of a String
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 找到字符串的长度
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以通过在字符串变量或字符串文字后面写<code>.length</code>来查找<code>String</code>值的长度。 <code>"Alan Peter".length; // 10</code>例如,如果我们创建了一个变量<code>var firstName = "Charles"</code> ,我们可以通过使用<code>firstName.length</code>属性找出字符串<code>"Charles"</code>长度。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>.length</code>属性计算<code>lastName</code>变量中的字符数,并将其分配给<code>lastNameLength</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>lastNameLength</code>应该等于8。
|
||||
testString: 'assert((function(){if(typeof lastNameLength !== "undefined" && typeof lastNameLength === "number" && lastNameLength === 8){return true;}else{return false;}})(), "<code>lastNameLength</code> should be equal to eight.");'
|
||||
- text: 您应该使用<code>.length</code>来获取<code>lastName</code>的长度,如下所示: <code>lastName.length</code> 。
|
||||
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 <code>lastName</code> by using <code>.length</code> like this: <code>lastName.length</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var firstNameLength = 0;
|
||||
var firstName = "Ada";
|
||||
|
||||
firstNameLength = firstName.length;
|
||||
|
||||
// Setup
|
||||
var lastNameLength = 0;
|
||||
var lastName = "Lovelace";
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
lastNameLength = lastName;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,63 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ae
|
||||
title: Finding a Remainder in JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在JavaScript中查找剩余内容
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <dfn>余数</dfn>运算符<code>%</code>给出了两个数的除法的余数。 <strong>例</strong> <blockquote> 5%2 = 1因为<br> Math.floor(5/2)= 2(商数) <br> 2 * 2 = 4 <br> 5 - 4 = 1(剩余) </blockquote> <strong>用法</strong> <br>在数学中,通过检查数字除以<code>2</code>的余数,可以检查数字是偶数还是奇数。 <blockquote> 17%2 = 1(17为奇数) <br> 48%2 = 0(48为偶数) </blockquote> <strong>注意</strong> <br> <dfn>余数</dfn>运算符有时被错误地称为“模数”运算符。它与模数非常相似,但在负数下不能正常工作。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<dfn>余数</dfn> ( <code>%</code> )运算符将<code>remainder</code>设置为等于<code>11</code>的余数除以<code>3</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 应该初始化变量<code>remainder</code>
|
||||
testString: 'assert(/var\s+?remainder/.test(code), "The variable <code>remainder</code> should be initialized");'
|
||||
- text: <code>remainder</code>的值应为<code>2</code>
|
||||
testString: 'assert(remainder === 2, "The value of <code>remainder</code> should be <code>2</code>");'
|
||||
- text: 您应该使用<code>%</code>运算符
|
||||
testString: 'assert(/\s+?remainder\s*?=\s*?.*%.*;/.test(code), "You should use the <code>%</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Only change code below this line
|
||||
|
||||
var remainder;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,68 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb9bdef
|
||||
title: Generate Random Fractions with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript生成随机分数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">随机数对于创建随机行为很有用。 JavaScript有一个<code>Math.random()</code>函数,它生成一个介于<code>0</code> (含)和不高达<code>1</code> (独占)之间的随机十进制数。因此<code>Math.random()</code>可以返回<code>0</code>但永远不会返回<code>1</code> <strong>Note</strong> <br> <a href="storing-values-with-the-assignment-operator" target="_blank">与使用Equal运算符存储值</a>一样,所有函数调用将在<code>return</code>执行之前解析,因此我们可以<code>return</code> <code>Math.random()</code>函数的值。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改<code>randomFraction</code>以返回随机数而不是返回<code>0</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>randomFraction</code>应该返回一个随机数。
|
||||
testString: 'assert(typeof randomFraction() === "number", "<code>randomFraction</code> should return a random number.");'
|
||||
- text: <code>randomFraction</code>返回的<code>randomFraction</code>应该是小数。
|
||||
testString: 'assert((randomFraction()+""). match(/\./g), "The number returned by <code>randomFraction</code> should be a decimal.");'
|
||||
- text: 您应该使用<code>Math.random</code>来生成随机十进制数。
|
||||
testString: 'assert(code.match(/Math\.random/g).length >= 0, "You should be using <code>Math.random</code> to generate the random decimal number.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function randomFraction() {
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
return 0;
|
||||
|
||||
// Only change code above this line.
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,70 @@
|
||||
---
|
||||
id: cf1111c1c12feddfaeb1bdef
|
||||
title: Generate Random Whole Numbers with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript生成随机整数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们可以生成随机十进制数很好,但如果我们用它来生成随机整数,它会更有用。 <ol><li>使用<code>Math.random()</code>生成随机小数。 </li><li>将随机小数乘以<code>20</code> 。 </li><li>使用另一个函数<code>Math.floor()</code>将数字向下舍入到最接近的整数。 </li></ol>请记住, <code>Math.random()</code>永远不会返回<code>1</code> ,因为我们正在向下舍入,实际上不可能得到<code>20</code> 。这项技术将给我们一个<code>0</code>到<code>19</code>之间的整数。将所有内容放在一起,这就是我们的代码: <code>Math.floor(Math.random() * 20);</code>我们调用<code>Math.random()</code> ,将结果乘以20,然后将值传递给<code>Math.floor()</code>函数,将值向下舍入到最接近的整数。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用此技术生成并返回<code>0</code>到<code>9</code>之间的随机整数。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>randomWholeNum</code>的结果应该是整数。
|
||||
testString: 'assert(typeof randomWholeNum() === "number" && (function(){var r = randomWholeNum();return Math.floor(r) === r;})(), "The result of <code>randomWholeNum</code> should be a whole number.");'
|
||||
- text: 您应该使用<code>Math.random</code>来生成随机数。
|
||||
testString: 'assert(code.match(/Math.random/g).length > 1, "You should be using <code>Math.random</code> to generate a random number.");'
|
||||
- text: 您应该将<code>Math.random</code>的结果乘以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), "You should have multiplied the result of <code>Math.random</code> by 10 to make it a number that is between zero and nine.");'
|
||||
- text: 您应该使用<code>Math.floor</code>删除数字的小数部分。
|
||||
testString: 'assert(code.match(/Math.floor/g).length > 1, "You should use <code>Math.floor</code> to remove the decimal part of the number.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var randomNumberBetween0and19 = Math.floor(Math.random() * 20);
|
||||
|
||||
function randomWholeNum() {
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
return Math.random();
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,80 @@
|
||||
---
|
||||
id: cf1111c1c12feddfaeb2bdef
|
||||
title: Generate Random Whole Numbers within a Range
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 生成范围内的随机整数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们可以生成一个落在两个特定数字范围内的随机数,而不是像我们之前那样在零和给定数字之间生成一个随机数。为此,我们将定义最小数量<code>min</code>和最大数量<code>max</code> 。这是我们将使用的公式。花一点时间阅读它并尝试理解这段代码在做什么: <code>Math.floor(Math.random() * (max - min + 1)) + min</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个名为<code>randomRange</code>的函数,它接受一个范围<code>myMin</code>和<code>myMax</code>并返回一个大于或等于<code>myMin</code>的随机数,并且小于或等于<code>myMax</code> (包括<code>myMax</code> )。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>randomRange</code>可以生成的最低随机数应该等于你的最小数量<code>myMin</code> 。
|
||||
testString: 'assert(calcMin === 5, "The lowest random number that can be generated by <code>randomRange</code> should be equal to your minimum number, <code>myMin</code>.");'
|
||||
- text: <code>randomRange</code>可以生成的最高随机数应该等于最大数量<code>myMax</code> 。
|
||||
testString: 'assert(calcMax === 15, "The highest random number that can be generated by <code>randomRange</code> should be equal to your maximum number, <code>myMax</code>.");'
|
||||
- text: <code>randomRange</code>生成的随机数应该是整数,而不是小数。
|
||||
testString: 'assert(randomRange(0,1) % 1 === 0 , "The random number generated by <code>randomRange</code> should be an integer, not a decimal.");'
|
||||
- text: <code>randomRange</code>应该同时使用<code>myMax</code>和<code>myMin</code> ,并在你的范围内返回一个随机数。
|
||||
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;}})(), "<code>randomRange</code> should use both <code>myMax</code> and <code>myMin</code>, and return a random number in your range.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
function ourRandomRange(ourMin, ourMax) {
|
||||
|
||||
return Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin;
|
||||
}
|
||||
|
||||
ourRandomRange(1, 9);
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
function randomRange(myMin, myMax) {
|
||||
|
||||
return 0; // Change this line
|
||||
|
||||
}
|
||||
|
||||
// Change these values to test your function
|
||||
var myRandom = randomRange(5, 15);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,109 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244be
|
||||
title: Global Scope and Functions
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 全球范围和职能
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在JavaScript中, <dfn>范围</dfn>是指变量的可见性。在功能块之外定义的变量具有<dfn>全局</dfn>范围。这意味着,它们可以在JavaScript代码中随处可见。在没有<code>var</code>关键字的情况下使用的变量将在<code>global</code>范围内自动创建。这可能会在代码中的其他位置或再次运行函数时产生意外后果。您应该始终使用<code>var</code>声明变量。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>var</code> ,在任何函数之外声明一个<code>global</code>变量<code>myGlobal</code> 。使用值<code>10</code>初始化它。在函数<code>fun1</code>内部,在<strong><em>不</em></strong>使用<code>var</code>关键字的<strong><em>情况下</em></strong>为<code>oopsGlobal</code>分配<code>5</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 应该定义<code>myGlobal</code>
|
||||
testString: 'assert(typeof myGlobal != "undefined", "<code>myGlobal</code> should be defined");'
|
||||
- text: <code>myGlobal</code>的值应为<code>10</code>
|
||||
testString: 'assert(myGlobal === 10, "<code>myGlobal</code> should have a value of <code>10</code>");'
|
||||
- text: 应使用<code>var</code>关键字声明<code>myGlobal</code>
|
||||
testString: 'assert(/var\s+myGlobal/.test(code), "<code>myGlobal</code> should be declared using the <code>var</code> keyword");'
|
||||
- text: <code>oopsGlobal</code>应该是一个全局变量,其值为<code>5</code>
|
||||
testString: 'assert(typeof oopsGlobal != "undefined" && oopsGlobal === 5, "<code>oopsGlobal</code> should be a global variable and have a value of <code>5</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Declare your variable here
|
||||
|
||||
|
||||
function fun1() {
|
||||
// Assign 5 to oopsGlobal Here
|
||||
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
function fun2() {
|
||||
var output = "";
|
||||
if (typeof myGlobal != "undefined") {
|
||||
output += "myGlobal: " + myGlobal;
|
||||
}
|
||||
if (typeof oopsGlobal != "undefined") {
|
||||
output += " oopsGlobal: " + oopsGlobal;
|
||||
}
|
||||
console.log(output);
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
|
||||
```js
|
||||
var logOutput = "";
|
||||
var originalConsole = console
|
||||
function capture() {
|
||||
var nativeLog = console.log;
|
||||
console.log = function (message) {
|
||||
logOutput = message;
|
||||
if(nativeLog.apply) {
|
||||
nativeLog.apply(originalConsole, arguments);
|
||||
} else {
|
||||
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
|
||||
nativeLog(nativeMsg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function uncapture() {
|
||||
console.log = originalConsole.log;
|
||||
}
|
||||
var oopsGlobal;
|
||||
capture();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c0
|
||||
title: Global vs. Local Scope in Functions
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 功能中的全局与局部范围
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">可以使<dfn>本地</dfn>变量和<dfn>全局</dfn>变量具有相同的名称。执行此操作时, <code>local</code>变量优先于<code>global</code>变量。在这个例子中: <blockquote> var someVar =“帽子”; <br> function myFun(){ <br> var someVar =“Head”; <br>返回someVar; <br> } </blockquote>函数<code>myFun</code>将返回<code>"Head"</code>因为存在变量的<code>local</code>版本。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将一个局部变量添加到<code>myOutfit</code>函数,以使用<code>"sweater"</code>覆盖<code>outerWear</code>的值。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 不要更改全局<code>outerWear</code>的值
|
||||
testString: 'assert(outerWear === "T-Shirt", "Do not change the value of the global <code>outerWear</code>");'
|
||||
- text: <code>myOutfit</code>应该返回<code>"sweater"</code>
|
||||
testString: 'assert(myOutfit() === "sweater", "<code>myOutfit</code> should return <code>"sweater"</code>");'
|
||||
- text: 不要更改return语句
|
||||
testString: 'assert(/return outerWear/.test(code), "Do not change the return statement");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var outerWear = "T-Shirt";
|
||||
|
||||
function myOutfit() {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
return outerWear;
|
||||
}
|
||||
|
||||
myOutfit();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 5664820f61c48e80c9fa476c
|
||||
title: Golf Code
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 高尔夫码
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在<a href="https://en.wikipedia.org/wiki/Golf" target="_blank">高尔夫</a>比赛中,每个洞都具有<code>par</code>意义,即高尔夫球手为了将球沉入洞中以完成比赛所期望的平均<code>strokes</code>次数。根据你的<code>strokes</code>高出或低于<code>par</code>的距离,有一个不同的昵称。您的函数将通过<code>par</code>和<code>strokes</code>参数。根据此表返回正确的字符串,该表按优先级顺序列出笔划;顶部(最高)到底部(最低): <table class="table table-striped"><thead><tr><th>笔画</th><th>返回</th></tr></thead><tbody><tr><td> 1 </td><td> “一杆进洞!” </td></tr><tr><td> <= par - 2 </td><td> “鹰” </td></tr><tr><td> par - 1 </td><td> “小鸟” </td></tr><tr><td>平价</td><td> “相提并论” </td></tr><tr><td> par + 1 </td><td> “柏忌” </td></tr><tr><td> par + 2 </td><td> “双柏忌” </td></tr><tr><td> > = par + 3 </td><td> “回家!” </td></tr></tbody></table> <code>par</code>和<code>strokes</code>将始终为数字和正数。为方便起见,我们添加了所有名称的数组。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>golfScore(4, 1)</code>应该返回“Hole-in-one!”'
|
||||
testString: 'assert(golfScore(4, 1) === "Hole-in-one!", "<code>golfScore(4, 1)</code> should return "Hole-in-one!"");'
|
||||
- text: '<code>golfScore(4, 2)</code>应该返回“Eagle”'
|
||||
testString: 'assert(golfScore(4, 2) === "Eagle", "<code>golfScore(4, 2)</code> should return "Eagle"");'
|
||||
- text: '<code>golfScore(5, 2)</code>应该返回“Eagle”'
|
||||
testString: 'assert(golfScore(5, 2) === "Eagle", "<code>golfScore(5, 2)</code> should return "Eagle"");'
|
||||
- text: '<code>golfScore(4, 3)</code>应该返回“Birdie”'
|
||||
testString: 'assert(golfScore(4, 3) === "Birdie", "<code>golfScore(4, 3)</code> should return "Birdie"");'
|
||||
- text: '<code>golfScore(4, 4)</code>应该返回“Par”'
|
||||
testString: 'assert(golfScore(4, 4) === "Par", "<code>golfScore(4, 4)</code> should return "Par"");'
|
||||
- text: '<code>golfScore(1, 1)</code>应该返回“Hole-in-one!”'
|
||||
testString: 'assert(golfScore(1, 1) === "Hole-in-one!", "<code>golfScore(1, 1)</code> should return "Hole-in-one!"");'
|
||||
- text: '<code>golfScore(5, 5)</code>应该返回“Par”'
|
||||
testString: 'assert(golfScore(5, 5) === "Par", "<code>golfScore(5, 5)</code> should return "Par"");'
|
||||
- text: '<code>golfScore(4, 5)</code>应该返回“Bogey”'
|
||||
testString: 'assert(golfScore(4, 5) === "Bogey", "<code>golfScore(4, 5)</code> should return "Bogey"");'
|
||||
- text: '<code>golfScore(4, 6)</code>应该返回“Double Bogey”'
|
||||
testString: 'assert(golfScore(4, 6) === "Double Bogey", "<code>golfScore(4, 6)</code> should return "Double Bogey"");'
|
||||
- text: '<code>golfScore(4, 7)</code>应该返回“Go Home!”'
|
||||
testString: 'assert(golfScore(4, 7) === "Go Home!", "<code>golfScore(4, 7)</code> should return "Go Home!"");'
|
||||
- text: '<code>golfScore(5, 9)</code>应该返回“Go Home!”'
|
||||
testString: 'assert(golfScore(5, 9) === "Go Home!", "<code>golfScore(5, 9)</code> should return "Go Home!"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
|
||||
function golfScore(par, strokes) {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
return "Change Me";
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
// Change these values to test
|
||||
golfScore(5, 4);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ac
|
||||
title: Increment a Number with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript增加数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以轻松地<dfn>增加</dfn>或添加一个变量与<code>++</code>运算符。 <code>i++;</code>相当于<code>i = i + 1;</code> <strong>注意</strong> <br>整行成为<code>i++;</code> ,消除了对等号的需要。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改代码以在<code>myVar</code>上使用<code>++</code>运算符。 <strong>暗示</strong> <br>了解有关<a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment_()" target="_blank">算术运算符的</a>更多信息<a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment_()" target="_blank">- 增量(++)</a> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myVar</code>应该等于<code>88</code>
|
||||
testString: 'assert(myVar === 88, "<code>myVar</code> should equal <code>88</code>");'
|
||||
- text: <code>myVar = myVar + 1;</code>应该改变
|
||||
testString: 'assert(/var\s*myVar\s*=\s*87;\s*\/*.*\s*([+]{2}\s*myVar|myVar\s*[+]{2});/.test(code), "<code>myVar = myVar + 1;</code> should be changed");'
|
||||
- text: 使用<code>++</code>运算符
|
||||
testString: 'assert(/[+]{2}\s*myVar|myVar\s*[+]{2}/.test(code), "Use the <code>++</code> operator");'
|
||||
- text: 不要更改行上方的代码
|
||||
testString: 'assert(/var myVar = 87;/.test(code), "Do not change code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var myVar = 87;
|
||||
|
||||
// Only change code below this line
|
||||
myVar = myVar + 1;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,60 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244a9
|
||||
title: Initializing Variables with the Assignment Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用赋值运算符初始化变量
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">通常将变量<dfn>初始化</dfn>为与声明的同一行中的初始值。 <code>var myVar = 0;</code>创建一个名为<code>myVar</code>的新变量,并为其指定初始值<code>0</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>var</code>定义变量<code>a</code>并将其初始化为值<code>9</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 将<code>a</code>初始化为值<code>9</code>
|
||||
testString: 'assert(/var\s+a\s*=\s*9\s*/.test(code), "Initialize <code>a</code> to a value of <code>9</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourVar = 19;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244db
|
||||
title: Introducing Else If Statements
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 如果声明引入Else
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果您有多个需要解决的条件,可以将<code>if</code>语句与<code>else if</code>语句链接在一起。 <blockquote> if(num> 15){ <br>返回“大于15”; <br> } else if(num <5){ <br>返回“小于5”; <br> } else { <br>返回“5到15之间”; <br> } </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">转换逻辑以使用<code>else if</code>语句。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该至少有两个<code>else</code>语句
|
||||
testString: 'assert(code.match(/else/g).length > 1, "You should have at least two <code>else</code> statements");'
|
||||
- text: 你应该至少有两个<code>if</code>语句
|
||||
testString: 'assert(code.match(/if/g).length > 1, "You should have at least two <code>if</code> statements");'
|
||||
- text: 您应该为每个条件关闭并打开花括号
|
||||
testString: 'assert(code.match(/if\s*\((.+)\)\s*\{[\s\S]+\}\s*else if\s*\((.+)\)\s*\{[\s\S]+\}\s*else\s*\{[\s\S]+\s*\}/), "You should have closing and opening curly braces for each condition in your if else statement");'
|
||||
- text: <code>testElseIf(0)</code>应返回“小于5”
|
||||
testString: 'assert(testElseIf(0) === "Smaller than 5", "<code>testElseIf(0)</code> should return "Smaller than 5"");'
|
||||
- text: <code>testElseIf(5)</code>应该返回“5到10之间”
|
||||
testString: 'assert(testElseIf(5) === "Between 5 and 10", "<code>testElseIf(5)</code> should return "Between 5 and 10"");'
|
||||
- text: <code>testElseIf(7)</code>应返回“5到10之间”
|
||||
testString: 'assert(testElseIf(7) === "Between 5 and 10", "<code>testElseIf(7)</code> should return "Between 5 and 10"");'
|
||||
- text: <code>testElseIf(10)</code>应返回“5到10之间”
|
||||
testString: 'assert(testElseIf(10) === "Between 5 and 10", "<code>testElseIf(10)</code> should return "Between 5 and 10"");'
|
||||
- text: <code>testElseIf(12)</code>应返回“大于10”
|
||||
testString: 'assert(testElseIf(12) === "Greater than 10", "<code>testElseIf(12)</code> should return "Greater than 10"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testElseIf(val) {
|
||||
if (val > 10) {
|
||||
return "Greater than 10";
|
||||
}
|
||||
|
||||
if (val < 5) {
|
||||
return "Smaller than 5";
|
||||
}
|
||||
|
||||
return "Between 5 and 10";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testElseIf(7);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244da
|
||||
title: Introducing Else Statements
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 介绍其他声明
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">当<code>if</code>语句的条件为真时,将执行其后面的代码块。当那个条件是假的时候怎么办?通常什么都不会发生。使用<code>else</code>语句,可以执行备用代码块。 <blockquote> if(num> 10){ <br>返回“大于10”; <br> } else { <br>返回“10或更少”; <br> } </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>if</code>语句组合到单个<code>if/else</code>语句中。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您应该只在编辑器中有一个<code>if</code>语句
|
||||
testString: 'assert(code.match(/if/g).length === 1, "You should only have one <code>if</code> statement in the editor");'
|
||||
- text: 你应该使用<code>else</code>语句
|
||||
testString: 'assert(/else/g.test(code), "You should use an <code>else</code> statement");'
|
||||
- text: <code>testElse(4)</code>应返回“5或更小”
|
||||
testString: 'assert(testElse(4) === "5 or Smaller", "<code>testElse(4)</code> should return "5 or Smaller"");'
|
||||
- text: <code>testElse(5)</code>应返回“5或更小”
|
||||
testString: 'assert(testElse(5) === "5 or Smaller", "<code>testElse(5)</code> should return "5 or Smaller"");'
|
||||
- text: <code>testElse(6)</code>应该返回“大于5”
|
||||
testString: 'assert(testElse(6) === "Bigger than 5", "<code>testElse(6)</code> should return "Bigger than 5"");'
|
||||
- text: <code>testElse(10)</code>应该返回“大于5”
|
||||
testString: 'assert(testElse(10) === "Bigger than 5", "<code>testElse(10)</code> should return "Bigger than 5"");'
|
||||
- text: 请勿更改行上方或下方的代码。
|
||||
testString: 'assert(/var result = "";/.test(code) && /return result;/.test(code), "Do not change the code above or below the lines.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testElse(val) {
|
||||
var result = "";
|
||||
// Only change code below this line
|
||||
|
||||
if (val > 5) {
|
||||
result = "Bigger than 5";
|
||||
}
|
||||
|
||||
if (val <= 5) {
|
||||
result = "5 or Smaller";
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
return result;
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testElse(4);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,69 @@
|
||||
---
|
||||
id: 56104e9e514f539506016a5c
|
||||
title: Iterate Odd Numbers With a For Loop
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用For循环迭代奇数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> For循环不必一次迭代一个循环。通过改变我们的<code>final-expression</code> ,我们可以计算偶数。我们将从<code>i = 0</code>开始并在<code>i < 10</code>循环。我们会增加<code>i</code>的2每个回路与<code>i += 2</code> 。 <blockquote> var ourArray = []; <br> for(var i = 0; i <10; i + = 2){ <br> ourArray.push(ⅰ); <br> } </blockquote> <code>ourArray</code>现在包含<code>[0,2,4,6,8]</code> 。让我们改变<code>initialization</code>这样我们就可以用奇数来计算。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>for</code>循环将奇数从1到9推送到<code>myArray</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该为此使用<code>for</code>循环。
|
||||
testString: 'assert(code.match(/for\s*\(/g).length > 1, "You should be using a <code>for</code> loop for this.");'
|
||||
- text: '<code>myArray</code>应该等于<code>[1,3,5,7,9]</code> 。'
|
||||
testString: 'assert.deepEqual(myArray, [1,3,5,7,9], "<code>myArray</code> should equal <code>[1,3,5,7,9]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [];
|
||||
|
||||
for (var i = 0; i < 10; i += 2) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 5675e877dbd60be8ad28edc6
|
||||
title: Iterate Through an Array with a For Loop
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用For循环遍历数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> JavaScript中的一个常见任务是遍历数组的内容。一种方法是使用<code>for</code>循环。此代码将数组<code>arr</code>每个元素输出到控制台: <blockquote> var arr = [10,9,8,7,6]; <br> for(var i = 0; i <arr.length; i ++){ <br> (ARR [I])的console.log; <br> } </blockquote>请记住,数组具有从零开始的编号,这意味着数组的最后一个索引是长度 - 1.我们对此循环的<dfn>条件</dfn>是<code>i < arr.length</code> ,当<code>i</code>长度为1时停止。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">声明并将变量<code>total</code>初始化为<code>0</code> 。使用<code>for</code>循环将<code>myArr</code>数组的每个元素的值添加到<code>total</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 应声明<code>total</code>并初始化为0
|
||||
testString: 'assert(code.match(/var.*?total\s*=\s*0.*?;/), "<code>total</code> should be declared and initialized to 0");'
|
||||
- text: <code>total</code>应该等于20
|
||||
testString: 'assert(total === 20, "<code>total</code> should equal 20");'
|
||||
- text: 您应该使用<code>for</code>循环来遍历<code>myArr</code>
|
||||
testString: 'assert(code.match(/for\s*\(/g).length > 1 && code.match(/myArr\s*\[/), "You should use a <code>for</code> loop to iterate through <code>myArr</code>");'
|
||||
- text: 不要直接将<code>total</code>设置为20
|
||||
testString: 'assert(!code.match(/total[\s\+\-]*=\s*(\d(?!\s*[;,])|[1-9])/g), "Do not set <code>total</code> to 20 directly");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArr = [ 9, 10, 11, 12];
|
||||
var ourTotal = 0;
|
||||
|
||||
for (var i = 0; i < ourArr.length; i++) {
|
||||
ourTotal += ourArr[i];
|
||||
}
|
||||
|
||||
// Setup
|
||||
var myArr = [ 2, 3, 4, 5, 6];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,70 @@
|
||||
---
|
||||
id: 5a2efd662fb457916e1fe604
|
||||
title: Iterate with JavaScript Do...While Loops
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript迭代...循环
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以使用循环多次运行相同的代码。你将学习的下一个类型的循环称为“ <code>do...while</code> ”循环,因为它首先将“ <code>do</code> ”循环内部代码的一次传递,无论如何,然后它运行“ <code>while</code> ”指定条件为真一旦这种情况不再真实就停止。我们来看一个例子。 <blockquote> var ourArray = []; <br> var i = 0; <br>做{ <br> ourArray.push(ⅰ); <br>我++; <br> } while(i <5); </blockquote>这与任何其他类型的循环一样正常,结果数组看起来像<code>[0, 1, 2, 3, 4]</code> 。然而,什么使得<code>do...while</code>与其他循环不同,但是当条件在第一次检查时失败时它的行为如何。让我们看看这个在行动。这是一个常规的while循环,只要<code>i < 5</code> ,它就会在循环中运行代码。 <blockquote> var ourArray = []; <br> var i = 5; <br>而(i <5){ <br> ourArray.push(ⅰ); <br>我++; <br> } </blockquote>请注意,我们将<code>i</code>的值初始化为5.当我们执行下一行时,我们注意到<code>i</code>不小于5.所以我们不执行循环内的代码。结果是<code>ourArray</code>最终没有添加任何内容,因此当上面示例中的所有代码完成运行时,它仍然看起来像这个<code>[]</code> 。现在,看一下<code>do...while</code>循环。 <blockquote> var ourArray = []; <br> var i = 5; <br>做{ <br> ourArray.push(ⅰ); <br>我++; <br> } while(i <5); </blockquote>在这种情况下,我们将<code>i</code>的值初始化为5,就像我们使用while循环一样。当我们到达下一行时,没有检查<code>i</code>的值,所以我们转到花括号内的代码并执行它。我们将在数组中添加一个元素并在进行条件检查之前递增<code>i</code> 。然后,当我们检查<code>i < 5</code>看到<code>i</code>现在是6,这不符合条件检查。所以我们退出循环并完成。在上面的例子的末尾, <code>ourArray</code>的值是<code>[5]</code> 。本质上, <code>do...while</code>循环确保循环内的代码至少运行一次。让我们尝试通过将值推送到数组来实现<code>do...while</code>循环。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将代码中的<code>while</code>循环更改为<code>do...while</code>循环,以便循环将数字10推送到<code>myArray</code> ,当代码完成运行时, <code>i</code>将等于<code>11</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>do...while</code>循环。
|
||||
testString: 'assert(code.match(/do/g), "You should be using a <code>do...while</code> loop for this.");'
|
||||
- text: '<code>myArray</code>应该等于<code>[10]</code> 。'
|
||||
testString: 'assert.deepEqual(myArray, [10], "<code>myArray</code> should equal <code>[10]</code>.");'
|
||||
- text: <code>i</code>应该等于<code>11</code>
|
||||
testString: 'assert.deepEqual(i, 11, "<code>i</code> should equal <code>11</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [];
|
||||
var i = 10;
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
while (i < 5) {
|
||||
myArray.push(i);
|
||||
i++;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,69 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb5bdef
|
||||
title: Iterate with JavaScript For Loops
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript迭代循环
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以使用循环多次运行相同的代码。最常见的JavaScript循环类型称为“ <code>for loop</code> ”,因为它“运行”特定次数。 For循环用三个可选表达式声明,用分号分隔: <code>for ([initialization]; [condition]; [final-expression])</code> <code>initialization</code>语句仅在循环开始之前执行一次。它通常用于定义和设置循环变量。 <code>condition</code>语句在每次循环迭代开始时进行计算,并且只要计算结果为<code>true</code>就会继续。当迭代开始时<code>condition</code>为<code>false</code>时,循环将停止执行。这意味着如果<code>condition</code>以<code>false</code>开头,则循环将永远不会执行。 <code>final-expression</code>在每次循环迭代结束时执行,在下一次<code>condition</code>检查之前执行,通常用于递增或递减循环计数器。在下面的示例中,我们使用<code>i = 0</code>初始化并迭代,而条件<code>i < 5</code>为真。我们将在每个循环迭代中将<code>i</code>递增<code>1</code> ,并使用<code>i++</code>作为<code>final-expression</code> 。 <blockquote> var ourArray = []; <br> for(var i = 0; i <5; i ++){ <br> ourArray.push(ⅰ); <br> } </blockquote> <code>ourArray</code>现在包含<code>[0,1,2,3,4]</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>for</code>循环将值1到5推送到<code>myArray</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该为此使用<code>for</code>循环。
|
||||
testString: 'assert(code.match(/for\s*\(/g).length > 1, "You should be using a <code>for</code> loop for this.");'
|
||||
- text: '<code>myArray</code>应该等于<code>[1,2,3,4,5]</code> 。'
|
||||
testString: 'assert.deepEqual(myArray, [1,2,3,4,5], "<code>myArray</code> should equal <code>[1,2,3,4,5]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [];
|
||||
|
||||
for (var i = 0; i < 5; i++) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,62 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb1bdef
|
||||
title: Iterate with JavaScript While Loops
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在循环时使用JavaScript进行迭代
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以使用循环多次运行相同的代码。我们将学习的第一种类型的循环称为“ <code>while</code> ”循环,因为它在“while”运行时指定的条件为true,并且一旦该条件不再为真就停止。 <blockquote> var ourArray = []; <br> var i = 0; <br>而(i <5){ <br> ourArray.push(ⅰ); <br>我++; <br> } </blockquote>让我们尝试通过将值推送到数组来实现while循环。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>while</code>循环将数字0到4推送到<code>myArray</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>while</code>循环。
|
||||
testString: 'assert(code.match(/while/g), "You should be using a <code>while</code> loop for this.");'
|
||||
- text: '<code>myArray</code>应该等于<code>[0,1,2,3,4]</code> 。'
|
||||
testString: 'assert.deepEqual(myArray, [0,1,2,3,4], "<code>myArray</code> should equal <code>[0,1,2,3,4]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,96 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244bf
|
||||
title: Local Scope and Functions
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 本地范围和功能
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在函数内声明的变量,以及函数参数都具有<dfn>局部</dfn>范围。这意味着,它们仅在该功能中可见。这是一个函数<code>myTest</code>带有一个名为<code>loc</code>的局部变量。 <blockquote> function myTest(){ <br> var loc =“foo”; <br>的console.log(LOC); <br> } <br> MYTEST(); //记录“foo” <br>的console.log(LOC); // loc未定义</blockquote> <code>loc</code>未在函数外定义。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在<code>myLocalScope</code>声明一个局部变量<code>myVar</code> 。运行测试,然后按照编辑器中注释的说明进行操作。 <strong>暗示</strong> <br>如果您遇到问题,刷新页面可能会有所帮助。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 没有全局<code>myVar</code>变量
|
||||
testString: 'assert(typeof myVar === "undefined", "No global <code>myVar</code> variable");'
|
||||
- text: 添加本地<code>myVar</code>变量
|
||||
testString: 'assert(/var\s+myVar/.test(code), "Add a local <code>myVar</code> variable");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function myLocalScope() {
|
||||
'use strict'; // you shouldn't need to edit this line
|
||||
|
||||
console.log(myVar);
|
||||
}
|
||||
myLocalScope();
|
||||
|
||||
// Run and check the console
|
||||
// myVar is not defined outside of myLocalScope
|
||||
console.log(myVar);
|
||||
|
||||
// Now remove the console log line to pass the test
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
|
||||
```js
|
||||
var logOutput = "";
|
||||
var originalConsole = console
|
||||
function capture() {
|
||||
var nativeLog = console.log;
|
||||
console.log = function (message) {
|
||||
logOutput = message;
|
||||
if(nativeLog.apply) {
|
||||
nativeLog.apply(originalConsole, arguments);
|
||||
} else {
|
||||
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
|
||||
nativeLog(nativeMsg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function uncapture() {
|
||||
console.log = originalConsole.log;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 5690307fddb111c6084545d7
|
||||
title: Logical Order in If Else Statements
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 如果其他陈述中的逻辑顺序
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">订单在<code>if</code> , <code>else if</code>语句中很重要。该函数从上到下执行,因此您需要注意首先出现的语句。以这两个函数为例。这是第一个: <blockquote> function foo(x){ <br> if(x <1){ <br>返回“少于一个”; <br> } else if(x <2){ <br>返回“少于两个”; <br> } else { <br>返回“大于或等于2”; <br> } <br> } </blockquote>第二个只是切换语句的顺序: <blockquote>功能栏(x){ <br> if(x <2){ <br>返回“少于两个”; <br> } else if(x <1){ <br>返回“少于一个”; <br> } else { <br>返回“大于或等于2”; <br> } <br> } </blockquote>虽然如果我们将数字传递给两者,这两个函数看起来几乎相同但我们得到不同的输出。 <blockquote> foo(0)//“不到一个” <br> bar(0)//“少于两个” </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改函数中的逻辑顺序,以便在所有情况下都返回正确的语句。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>orderMyLogic(4)</code>应返回“小于5”
|
||||
testString: 'assert(orderMyLogic(4) === "Less than 5", "<code>orderMyLogic(4)</code> should return "Less than 5"");'
|
||||
- text: <code>orderMyLogic(6)</code>应该返回“少于10”
|
||||
testString: 'assert(orderMyLogic(6) === "Less than 10", "<code>orderMyLogic(6)</code> should return "Less than 10"");'
|
||||
- text: <code>orderMyLogic(11)</code>应该返回“大于或等于10”
|
||||
testString: 'assert(orderMyLogic(11) === "Greater than or equal to 10", "<code>orderMyLogic(11)</code> should return "Greater than or equal to 10"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function orderMyLogic(val) {
|
||||
if (val < 10) {
|
||||
return "Less than 10";
|
||||
} else if (val < 5) {
|
||||
return "Less than 5";
|
||||
} else {
|
||||
return "Greater than or equal to 10";
|
||||
}
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
orderMyLogic(7);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,70 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392cc
|
||||
title: Manipulate Arrays With pop()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用pop()操作数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">更改数组中数据的另一种方法是使用<code>.pop()</code>函数。 <code>.pop()</code>用于“弹出”数组末尾的值。我们可以通过将其赋值给变量来存储这个“弹出”值。换句话说, <code>.pop()</code>从数组中删除最后一个元素并返回该元素。任何类型的条目都可以从数组“弹出” - 数字,字符串,甚至嵌套数组。 <blockquote> <code>var threeArr = [1, 4, 6]; <br> var oneDown = threeArr.pop(); <br> console.log(oneDown); // Returns 6 <br> console.log(threeArr); // Returns [1, 4]</code> </blockquote> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>.pop()</code>函数从<code>myArray</code>删除最后一项,将“弹出”值分配给<code>removedFromMyArray</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>myArray</code>应该只包含<code>[["John", 23]]</code> 。'
|
||||
testString: 'assert((function(d){if(d[0][0] == "John" && d[0][1] === 23 && d[1] == undefined){return true;}else{return false;}})(myArray), "<code>myArray</code> should only contain <code>[["John", 23]]</code>.");'
|
||||
- text: 在<code>myArray</code>上使用<code>pop()</code>
|
||||
testString: 'assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code), "Use <code>pop()</code> on <code>myArray</code>");'
|
||||
- text: '<code>removedFromMyArray</code>应该只包含<code>["cat", 2]</code> 。'
|
||||
testString: 'assert((function(d){if(d[0] == "cat" && d[1] === 2 && d[2] == undefined){return true;}else{return false;}})(removedFromMyArray), "<code>removedFromMyArray</code> should only contain <code>["cat", 2]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [1,2,3];
|
||||
var removedFromOurArray = ourArray.pop();
|
||||
// removedFromOurArray now equals 3, and ourArray now equals [1,2]
|
||||
|
||||
// Setup
|
||||
var myArray = [["John", 23], ["cat", 2]];
|
||||
|
||||
// Only change code below this line.
|
||||
var removedFromMyArray;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392cb
|
||||
title: Manipulate Arrays With push()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 用push()操纵数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">将数据附加到数组末尾的简单方法是通过<code>push()</code>函数。 <code>.push()</code>接受一个或多个<dfn>参数</dfn>并将它们“推”到数组的末尾。 <blockquote> var arr = [1,2,3]; <br> arr.push(4); <br> // arr现在是[1,2,3,4] </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>["dog", 3]</code>推到<code>myArray</code>变量的末尾。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>myArray</code>现在应该等于<code>[["John", 23], ["cat", 2], ["dog", 3]]</code> 。'
|
||||
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), "<code>myArray</code> should now equal <code>[["John", 23], ["cat", 2], ["dog", 3]]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = ["Stimpson", "J", "cat"];
|
||||
ourArray.push(["happy", "joy"]);
|
||||
// ourArray now equals ["Stimpson", "J", "cat", ["happy", "joy"]]
|
||||
|
||||
// Setup
|
||||
var myArray = [["John", 23], ["cat", 2]];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392cd
|
||||
title: Manipulate Arrays With shift()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用shift()操纵数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>pop()</code>总是删除数组的最后一个元素。如果你想删除第一个怎么办?这就是<code>.shift()</code>用武之地。它就像<code>.pop()</code>一样工作,除了它删除了第一个元素而不是最后一个元素。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>.shift()</code>函数从<code>myArray</code>删除第一项,将“shift off”值分配给<code>removedFromMyArray</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>myArray</code>现在应该等于<code>[["dog", 3]]</code> 。'
|
||||
testString: 'assert((function(d){if(d[0][0] == "dog" && d[0][1] === 3 && d[1] == undefined){return true;}else{return false;}})(myArray), "<code>myArray</code> should now equal <code>[["dog", 3]]</code>.");'
|
||||
- text: '<code>removedFromMyArray</code>应该包含<code>["John", 23]</code> 。'
|
||||
testString: 'assert((function(d){if(d[0] == "John" && d[1] === 23 && typeof removedFromMyArray === "object"){return true;}else{return false;}})(removedFromMyArray), "<code>removedFromMyArray</code> should contain <code>["John", 23]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = ["Stimpson", "J", ["cat"]];
|
||||
var removedFromOurArray = ourArray.shift();
|
||||
// removedFromOurArray now equals "Stimpson" and ourArray now equals ["J", ["cat"]].
|
||||
|
||||
// Setup
|
||||
var myArray = [["John", 23], ["dog", 3]];
|
||||
|
||||
// Only change code below this line.
|
||||
var removedFromMyArray;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,67 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392ce
|
||||
title: Manipulate Arrays With unshift()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用unshift操作数组()
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">不仅可以<code>shift</code>元件关闭的阵列的开头的,也可以<code>unshift</code>元素添加到数组的开始,即在阵列的前添加元素。 <code>.unshift()</code>工作方式与<code>.push()</code>完全相同,但是不是在数组的末尾添加元素, <code>unshift()</code>会在数组的开头添加元素。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>unshift()</code>将<code>["Paul",35]</code>添加到<code>myArray</code>变量的开头。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>myArray</code>现在应该有[[“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), "<code>myArray</code> should now have [["Paul", 35], ["dog", 3]].");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = ["Stimpson", "J", "cat"];
|
||||
ourArray.shift(); // ourArray now equals ["J", "cat"]
|
||||
ourArray.unshift("Happy");
|
||||
// ourArray now equals ["Happy", "J", "cat"]
|
||||
|
||||
// Setup
|
||||
var myArray = [["John", 23], ["dog", 3]];
|
||||
myArray.shift();
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,86 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244cb
|
||||
title: Manipulating Complex Objects
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 操纵复杂对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有时您可能希望将数据存储在灵活的<dfn>数据结构中</dfn> 。 JavaScript对象是处理灵活数据的一种方法。它们允许<dfn>字符串</dfn> , <dfn>数字</dfn> , <dfn>布尔值</dfn> , <dfn>数组</dfn> , <dfn>函数</dfn>和<dfn>对象的</dfn>任意组合。这是一个复杂数据结构的示例: <blockquote> var ourMusic = [ <br> { <br> “艺术家”:“Daft Punk”, <br> “标题”:“家庭作业”, <br> “release_year”:1997年, <br> “格式”:[ <br> “光盘”, <br> “盒式” <br> “LP” <br> ] <br> “黄金”:是的<br> } <br> ]。 </blockquote>这是一个包含一个对象的数组。该对象具有关于专辑的各种<dfn>元数据</dfn> 。它还有一个嵌套的<code>"formats"</code>数组。如果要添加更多专辑记录,可以通过向顶级数组添加记录来完成此操作。对象将数据保存在属性中,该属性具有键值格式。在上面的示例中, <code>"artist": "Daft Punk"</code>是具有<code>"artist"</code>键和<code>"Daft Punk"</code>值的属性。 <a href="http://www.json.org/" target="_blank">JavaScript Object Notation</a>或<code>JSON</code>是用于存储数据的相关数据交换格式。 <blockquote> { <br> “艺术家”:“Daft Punk”, <br> “标题”:“家庭作业”, <br> “release_year”:1997年, <br> “格式”:[ <br> “光盘”, <br> “盒式” <br> “LP” <br> ] <br> “黄金”:是的<br> } </blockquote> <strong>注意</strong> <br>除非它是数组中的最后一个对象,否则您需要在数组中的每个对象后面放置一个逗号。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将新相册添加到<code>myMusic</code>阵列。添加<code>artist</code>和<code>title</code>字符串, <code>release_year</code>数字和<code>formats</code>字符串数组。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myMusic</code>应该是一个数组
|
||||
testString: 'assert(Array.isArray(myMusic), "<code>myMusic</code> should be an array");'
|
||||
- text: <code>myMusic</code>应该至少有两个元素
|
||||
testString: 'assert(myMusic.length > 1, "<code>myMusic</code> should have at least two elements");'
|
||||
- text: '<code>myMusic[1]</code>应该是一个对象'
|
||||
testString: 'assert(typeof myMusic[1] === "object", "<code>myMusic[1]</code> should be an object");'
|
||||
- text: '<code>myMusic[1]</code>应该至少有4个属性'
|
||||
testString: 'assert(Object.keys(myMusic[1]).length > 3, "<code>myMusic[1]</code> should have at least 4 properties");'
|
||||
- text: '<code>myMusic[1]</code>应该包含一个<code>artist</code>属性,它是一个字符串'
|
||||
testString: 'assert(myMusic[1].hasOwnProperty("artist") && typeof myMusic[1].artist === "string", "<code>myMusic[1]</code> should contain an <code>artist</code> property which is a string");'
|
||||
- text: '<code>myMusic[1]</code>应该包含一个<code>title</code>属性,它是一个字符串'
|
||||
testString: 'assert(myMusic[1].hasOwnProperty("title") && typeof myMusic[1].title === "string", "<code>myMusic[1]</code> should contain a <code>title</code> property which is a string");'
|
||||
- text: '<code>myMusic[1]</code>应该包含一个<code>release_year</code>属性,它是一个数字'
|
||||
testString: 'assert(myMusic[1].hasOwnProperty("release_year") && typeof myMusic[1].release_year === "number", "<code>myMusic[1]</code> should contain a <code>release_year</code> property which is a number");'
|
||||
- text: '<code>myMusic[1]</code>应该包含一个<code>formats</code>属性,它是一个数组'
|
||||
testString: 'assert(myMusic[1].hasOwnProperty("formats") && Array.isArray(myMusic[1].formats), "<code>myMusic[1]</code> should contain a <code>formats</code> property which is an array");'
|
||||
- text: <code>formats</code>应该是一个至少包含两个元素的字符串数组
|
||||
testString: 'assert(myMusic[1].formats.every(function(item) { return (typeof item === "string")}) && myMusic[1].formats.length > 1, "<code>formats</code> should be an array of strings with at least two elements");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var myMusic = [
|
||||
{
|
||||
"artist": "Billy Joel",
|
||||
"title": "Piano Man",
|
||||
"release_year": 1973,
|
||||
"formats": [
|
||||
"CD",
|
||||
"8T",
|
||||
"LP"
|
||||
],
|
||||
"gold": true
|
||||
}
|
||||
// Add record here
|
||||
];
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,66 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb8bdef
|
||||
title: Modify Array Data With Indexes
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用索引修改数组数据
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">与字符串不同,数组的条目是<dfn>可变的</dfn> ,可以自由更改。 <strong>例</strong> <blockquote> var ourArray = [50,40,30]; <br> ourArray [0] = 15; //等于[15,40,30] </blockquote> <strong>注意</strong> <br>数组名称和方括号之间不应有任何空格,如<code>array [0]</code> 。尽管JavaScript能够正确处理,但这可能会让其他程序员在阅读代码时感到困惑。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将存储在<code>myArray</code>索引<code>0</code>处的数据修改为值<code>45</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>myArray</code>现在应该是[45,64,99]。'
|
||||
testString: 'assert((function(){if(typeof myArray != "undefined" && myArray[0] == 45 && myArray[1] == 64 && myArray[2] == 99){return true;}else{return false;}})(), "<code>myArray</code> should now be [45,64,99].");'
|
||||
- text: 您应该使用正确的索引来修改<code>myArray</code>的值。
|
||||
testString: 'assert((function(){if(code.match(/myArray\[0\]\s*=\s*/g)){return true;}else{return false;}})(), "You should be using correct index to modify the value in <code>myArray</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [18,64,99];
|
||||
ourArray[1] = 45; // ourArray now equals [18,45,99].
|
||||
|
||||
// Setup
|
||||
var myArray = [18,64,99];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244df
|
||||
title: Multiple Identical Options in Switch Statements
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 交换机语句中的多个相同选项
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果从<code>switch</code>语句的<code>case</code>省略了<code>break</code>语句,则会执行以下<code>case</code>语句,直到遇到<code>break</code> 。如果您有多个具有相同输出的输入,则可以在<code>switch</code>语句中表示它们,如下所示: <blockquote> switch(val){ <br>情况1: <br>案例2: <br>案例3: <br> result =“1,2或3”; <br>打破; <br>案例4: <br> result =“4 alone”; <br> } </blockquote> 1,2和3的情况都会产生相同的结果。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">写一个switch语句来设置以下范围的<code>answer</code> : <br> <code>1-3</code> - “低” <br> <code>4-6</code> - “中” <br> <code>7-9</code> - “高” <strong>注</strong> <br>您需要为范围中的每个数字都有一个<code>case</code>语句。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>sequentialSizes(1)</code>应返回“Low”
|
||||
testString: 'assert(sequentialSizes(1) === "Low", "<code>sequentialSizes(1)</code> should return "Low"");'
|
||||
- text: <code>sequentialSizes(2)</code>应该返回“Low”
|
||||
testString: 'assert(sequentialSizes(2) === "Low", "<code>sequentialSizes(2)</code> should return "Low"");'
|
||||
- text: <code>sequentialSizes(3)</code>应返回“Low”
|
||||
testString: 'assert(sequentialSizes(3) === "Low", "<code>sequentialSizes(3)</code> should return "Low"");'
|
||||
- text: <code>sequentialSizes(4)</code>应返回“Mid”
|
||||
testString: 'assert(sequentialSizes(4) === "Mid", "<code>sequentialSizes(4)</code> should return "Mid"");'
|
||||
- text: <code>sequentialSizes(5)</code>应返回“Mid”
|
||||
testString: 'assert(sequentialSizes(5) === "Mid", "<code>sequentialSizes(5)</code> should return "Mid"");'
|
||||
- text: <code>sequentialSizes(6)</code>应返回“Mid”
|
||||
testString: 'assert(sequentialSizes(6) === "Mid", "<code>sequentialSizes(6)</code> should return "Mid"");'
|
||||
- text: <code>sequentialSizes(7)</code>应该返回“High”
|
||||
testString: 'assert(sequentialSizes(7) === "High", "<code>sequentialSizes(7)</code> should return "High"");'
|
||||
- text: <code>sequentialSizes(8)</code>应该返回“High”
|
||||
testString: 'assert(sequentialSizes(8) === "High", "<code>sequentialSizes(8)</code> should return "High"");'
|
||||
- text: <code>sequentialSizes(9)</code>应该返回“High”
|
||||
testString: 'assert(sequentialSizes(9) === "High", "<code>sequentialSizes(9)</code> should return "High"");'
|
||||
- text: 您不应该使用任何<code>if</code>或<code>else</code>语句
|
||||
testString: 'assert(!/else/g.test(code) || !/if/g.test(code), "You should not use any <code>if</code> or <code>else</code> statements");'
|
||||
- text: 你应该有九个<code>case</code>陈述
|
||||
testString: 'assert(code.match(/case/g).length === 9, "You should have nine <code>case</code> statements");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function sequentialSizes(val) {
|
||||
var answer = "";
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
return answer;
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
sequentialSizes(1);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,59 @@
|
||||
---
|
||||
id: bd7993c9c69feddfaeb7bdef
|
||||
title: Multiply Two Decimals with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript乘以两个小数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在JavaScript中,您也可以使用十进制数执行计算,就像整数一样。让我们将两位小数相乘得到它们的乘积。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改<code>0.0</code>使产品等于<code>5.0</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 变量<code>product</code>应该等于<code>5.0</code> 。
|
||||
testString: 'assert(product === 5.0, "The variable <code>product</code> should equal <code>5.0</code>.");'
|
||||
- text: 你应该使用<code>*</code>运算符
|
||||
testString: 'assert(/\*/.test(code), "You should use the <code>*</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var product = 2.0 * 0.0;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,59 @@
|
||||
---
|
||||
id: cf1231c1c11feddfaeb5bdef
|
||||
title: Multiply Two Numbers with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript将两个数字相乘
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们也可以将一个数字乘以另一个数字。 JavaScript使用<code>*</code>符号来乘以两个数字。 <p> <strong>例</strong> </p><blockquote> myVar = 13 * 13; //指定169 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改<code>0</code>使产品等于<code>80</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 使变量<code>product</code>等于80
|
||||
testString: 'assert(product === 80,"Make the variable <code>product</code> equal 80");'
|
||||
- text: 使用<code>*</code>运算符
|
||||
testString: 'assert(/\*/.test(code), "Use the <code>*</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var product = 8 * 0;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,61 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb7bdef
|
||||
title: Nest one Array within Another Array
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 将一个Array嵌套在另一个Array中
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您还可以在其他数组中嵌套数组,如: <code>[["Bulls", 23], ["White Sox", 45]]</code> 。这也称为<dfn>多维数组<dfn>。</dfn></dfn> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个名为<code>myArray</code>的嵌套数组。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myArray</code>应至少有一个嵌套在另一个数组中的数组。
|
||||
testString: 'assert(Array.isArray(myArray) && myArray.some(Array.isArray), "<code>myArray</code> should have at least one array nested within another array.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [["the universe", 42], ["everything", 101010]];
|
||||
|
||||
// Only change code below this line.
|
||||
var myArray = [];
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,62 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244e1
|
||||
title: Nesting For Loops
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 嵌套循环
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果您有一个多维数组,则可以使用与先前路点相同的逻辑来遍历数组和任何子数组。这是一个例子: <blockquote> var arr = [ <br> [1,2],[3,4],[5,6] <br> ]。 <br> for(var i = 0; i <arr.length; i ++){ <br> for(var j = 0; j <arr [i] .length; j ++){ <br>的console.log(ARR [i] [j]); <br> } <br> } </blockquote>这个输出在每个子元件<code>arr</code>一次一个。注意,对于内部循环,我们检查<code>arr[i]</code>的<code>.length</code> ,因为<code>arr[i]</code>本身就是一个数组。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">修改函数<code>multiplyAll</code> ,使其乘以<code>product</code>变量乘以<code>arr</code>的子数组中的每个数字</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>multiplyAll([[1],[2],[3]])</code>应该返回<code>6</code>'
|
||||
testString: 'assert(multiplyAll([[1],[2],[3]]) === 6, "<code>multiplyAll([[1],[2],[3]])</code> should return <code>6</code>");'
|
||||
- text: '<code>multiplyAll([[1,2],[3,4],[5,6,7]])</code>应返回<code>5040</code>'
|
||||
testString: 'assert(multiplyAll([[1,2],[3,4],[5,6,7]]) === 5040, "<code>multiplyAll([[1,2],[3,4],[5,6,7]])</code> should return <code>5040</code>");'
|
||||
- text: '<code>multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]])</code>应该返回<code>54</code>'
|
||||
testString: 'assert(multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]]) === 54, "<code>multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]])</code> should return <code>54</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function multiplyAll(arr) {
|
||||
var product = 1;
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
return product;
|
||||
}
|
||||
|
||||
// Modify values below to test your code
|
||||
multiplyAll([[1,2],[3,4],[5,6,7]]);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,97 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244bd
|
||||
title: Passing Values to Functions with Arguments
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 将值传递给带参数的函数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <dfn>参数</dfn>是变量,它们作为调用函数时要输入到函数的值的占位符。定义函数时,通常将其与一个或多个参数一起定义。调用函数时输入(或<dfn>“传递”</dfn> )的实际值称为<dfn>参数</dfn> 。这是一个带有两个参数的函数, <code>param1</code>和<code>param2</code> : <blockquote> function testFun(param1,param2){ <br> console.log(param1,param2); <br> } </blockquote>然后我们可以调用<code>testFun</code> : <code>testFun("Hello", "World");</code>我们通过了两个论点, <code>"Hello"</code>和<code>"World"</code> 。在函数内部, <code>param1</code>将等于“Hello”, <code>param2</code>将等于“World”。请注意,您可以使用不同的参数再次调用<code>testFun</code> ,并且参数将采用新参数的值。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions"><ol><li>创建一个名为<code>functionWithArgs</code> ,该函数接受两个参数并将其总和输出到开发控制台。 </li><li>使用两个数字作为参数调用该函数。 </li></ol></section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>functionWithArgs</code>应该是一个函数
|
||||
testString: 'assert(typeof functionWithArgs === "function", "<code>functionWithArgs</code> should be a function");'
|
||||
- text: '<code>functionWithArgs(1,2)</code>应该输出<code>3</code>'
|
||||
testString: 'if(typeof functionWithArgs === "function") { capture(); functionWithArgs(1,2); uncapture(); } assert(logOutput == 3, "<code>functionWithArgs(1,2)</code> should output <code>3</code>");'
|
||||
- text: '<code>functionWithArgs(7,9)</code>应该输出<code>16</code>'
|
||||
testString: 'if(typeof functionWithArgs === "function") { capture(); functionWithArgs(7,9); uncapture(); } assert(logOutput == 16, "<code>functionWithArgs(7,9)</code> should output <code>16</code>");'
|
||||
- text: 定义后,使用两个数字调用<code>functionWithArgs</code> 。
|
||||
testString: 'assert(/^\s*functionWithArgs\s*\(\s*\d+\s*,\s*\d+\s*\)\s*;/m.test(code), "Call <code>functionWithArgs</code> with two numbers after you define it.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
function ourFunctionWithArgs(a, b) {
|
||||
console.log(a - b);
|
||||
}
|
||||
ourFunctionWithArgs(10, 5); // Outputs 5
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
|
||||
```js
|
||||
var logOutput = "";
|
||||
var originalConsole = console
|
||||
function capture() {
|
||||
var nativeLog = console.log;
|
||||
console.log = function (message) {
|
||||
if(message) logOutput = JSON.stringify(message).trim();
|
||||
if(nativeLog.apply) {
|
||||
nativeLog.apply(originalConsole, arguments);
|
||||
} else {
|
||||
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
|
||||
nativeLog(nativeMsg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function uncapture() {
|
||||
console.log = originalConsole.log;
|
||||
}
|
||||
|
||||
capture();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,62 @@
|
||||
---
|
||||
id: 599a789b454f2bbd91a3ff4d
|
||||
title: Practice comparing different values
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 练习比较不同的值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在最后两个挑战中,我们学习了等于运算符( <code>==</code> )和严格相等运算符( <code>===</code> )。让我们快速回顾一下这些运算符的实践。如果要比较的值不是同一类型,则相等运算符将执行类型转换,然后计算值。但是,严格相等运算符将按原样比较数据类型和值,而不将一种类型转换为另一种类型。 <strong>例子</strong> <blockquote> 3 =='3'//返回true,因为JavaScript执行从字符串到数字的类型转换<br> 3 ==='3'//返回false,因为类型不同并且未执行类型转换</blockquote> <strong>注意</strong> <br>在JavaScript中,您可以使用<code>typeof</code>运算符确定变量的类型或值,如下所示: <blockquote> typeof 3 //返回'number' <br> typeof'3'//返回'string' </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">编辑器中的<code>compareEquality</code>函数使用<code>equality operator</code>比较两个值。修改函数,使其仅在值严格相等时返回“Equal”。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>compareEquality(10, "10")</code>应返回“Not Equal”'
|
||||
testString: 'assert(compareEquality(10, "10") === "Not Equal", "<code>compareEquality(10, "10")</code> should return "Not Equal"");'
|
||||
- text: '<code>compareEquality("20", 20)</code>应该返回“Not Equal”'
|
||||
testString: 'assert(compareEquality("20", 20) === "Not Equal", "<code>compareEquality("20", 20)</code> should return "Not Equal"");'
|
||||
- text: 您应该使用<code>===</code>运算符
|
||||
testString: 'assert(code.match(/===/g), "You should use the <code>===</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function compareEquality(a, b) {
|
||||
if (a == b) { // Change this line
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
compareEquality(10, "10");
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,96 @@
|
||||
---
|
||||
id: 5688e62ea601b2482ff8422b
|
||||
title: Profile Lookup
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 个人资料查询
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们的联系人列表中有一组代表不同人的对象。已经为您预先编写了一个以<code>name</code>和属性( <code>prop</code> )作为参数的<code>lookUpProfile</code>函数。该函数应检查<code>name</code>是否是实际联系人的<code>firstName</code> ,并且给定属性( <code>prop</code> )是该联系人的属性。如果两者都为真,则返回该属性的“值”。如果<code>name</code>与任何联系人不对应,则返回<code>"No such contact"</code>如果<code>prop</code>不符合找到匹配<code>name</code>的联系人的任何有效属性,则返回<code>"No such property"</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>"Kristian", "lastName"</code>应该返回<code>"Vos"</code>'
|
||||
testString: 'assert(lookUpProfile("Kristian","lastName") === "Vos", "<code>"Kristian", "lastName"</code> should return <code>"Vos"</code>");'
|
||||
- text: '<code>"Sherlock", "likes"</code>应该回归<code>["Intriguing Cases", "Violin"]</code>'
|
||||
testString: 'assert.deepEqual(lookUpProfile("Sherlock", "likes"), ["Intriguing Cases", "Violin"], "<code>"Sherlock", "likes"</code> should return <code>["Intriguing Cases", "Violin"]</code>");'
|
||||
- text: '<code>"Harry","likes"</code>应该返回一个阵列'
|
||||
testString: 'assert(typeof lookUpProfile("Harry", "likes") === "object", "<code>"Harry","likes"</code> should return an array");'
|
||||
- text: '<code>"Bob", "number"</code>应该返回“没有这样的联系”'
|
||||
testString: 'assert(lookUpProfile("Bob", "number") === "No such contact", "<code>"Bob", "number"</code> should return "No such contact"");'
|
||||
- text: '<code>"Bob", "potato"</code>应该返回“没有这样的联系”'
|
||||
testString: 'assert(lookUpProfile("Bob", "potato") === "No such contact", "<code>"Bob", "potato"</code> should return "No such contact"");'
|
||||
- text: '<code>"Akira", "address"</code>应该返回“没有这样的财产”'
|
||||
testString: 'assert(lookUpProfile("Akira", "address") === "No such property", "<code>"Akira", "address"</code> should return "No such property"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
//Setup
|
||||
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"]
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
function lookUpProfile(name, prop){
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
// Change these values to test your function
|
||||
lookUpProfile("Akira", "likes");
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,59 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b4
|
||||
title: Quoting Strings with Single Quotes
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 单引号引用字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> JavaScript中的<dfn>字符串</dfn>值可以使用单引号或双引号编写,只要您以相同类型的引号开头和结尾即可。与其他一些编程语言不同,单引号和双引号在JavaScript中的工作方式相同。 <blockquote> doubleQuoteStr =“这是一个字符串”; <br> singleQuoteStr ='这也是一个字符串'; </blockquote>你可能想要使用一种报价而不是另一种报价的原因是你想在字符串中使用这两种报价。如果要将对话保存在字符串中并将对话用引号括起,则可能会发生这种情况。它的另一个用途是在一个字符串中保存带引号中各种属性的<code><a></code>标签。 <blockquote>谈话='芬恩向杰克惊呼,“代数!”'; </blockquote>但是,如果您需要使用其中的最外层引号,这将成为一个问题。请记住,字符串在开头和结尾都有相同的引用。但是如果你在中间的某个地方有相同的引用,字符串将提前停止并抛出错误。 <blockquote> goodStr ='杰克问芬恩,“嘿,我们去冒险吧?” <br> badStr ='芬恩回答,“我们走了!”'; //引发错误</blockquote>在上面的<dfn>goodStr中</dfn> ,您可以使用反斜杠<code>\</code>作为转义字符安全地使用两个引号。 <strong>注意</strong> <br>反斜杠<code>\</code>不应与正斜杠<code>/</code>混淆。他们不做同样的事情。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将提供的字符串更改为在开头和结尾使用单引号的字符串,并且不包含转义字符。现在,字符串中的<code><a></code>标签在任何地方都使用双引号。您需要将外引号更改为单引号,以便删除转义字符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 删除所有<code>backslashes</code> ( <code>\</code> )
|
||||
testString: 'assert(!/\\/g.test(code) && myStr.match("\\s*<a href\\s*=\\s*"http://www.example.com"\\s*target\\s*=\\s*"_blank">\\s*Link\\s*</a>\\s*"), "Remove all the <code>backslashes</code> (<code>\</code>)");'
|
||||
- text: '你应该有两个单引号<code>'</code>和四个双引号<code>"</code>'
|
||||
testString: 'assert(code.match(/"/g).length === 4 && code.match(/"/g).length === 2, "You should have two single quotes <code>'</code> and four double quotes <code>"</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var myStr = "<a href=\"http://www.example.com\" target=\"_blank\">Link</a>";
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,107 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244cf
|
||||
title: Record Collection
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 记录收集
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您将获得一个JSON对象,表示您的音乐专辑集合的一部分。每张专辑都有几个属性和一个唯一的ID号作为其关键。并非所有相册都有完整的信息。写一个功能,它取一个专辑的<code>id</code> (如<code>2548</code> ),一个属性<code>prop</code> (如<code>"artist"</code>或<code>"tracks"</code> ),以及一个<code>value</code> (如<code>"Addicted to Love"</code> )来修改此集合中的数据。如果<code>prop</code>不是<code>"tracks"</code>且<code>value</code>不为空( <code>""</code> ),则更新或设置该记录专辑属性的<code>value</code> 。您的函数必须始终返回整个集合对象。处理不完整数据有几个规则:如果<code>prop</code>是<code>"tracks"</code>但是相册没有<code>"tracks"</code>属性,则在将新值添加到相册的相应属性之前创建一个空数组。如果<code>prop</code>是<code>"tracks"</code>且<code>value</code>不为空( <code>""</code> ),则将<code>value</code>推到专辑现有<code>tracks</code>数组的末尾。如果<code>value</code>为空( <code>""</code> ),则从相册中删除给定的<code>prop</code>属性。 <strong>提示</strong> <br>使用<a href="javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables" target="_blank">变量访问对象属性</a>时使用<code>bracket notation</code> 。 Push是一种可以在<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push" target="_blank">Mozilla Developer Network</a>上阅读的数组方法。您可以参考<a href="javascript-algorithms-and-data-structures/basic-javascript/manipulating-complex-objects" target="_blank">操作复杂对象</a>介绍JavaScript对象表示法(JSON)进行复习。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '在<code>updateRecords(5439, "artist", "ABBA")</code> , <code>artist</code>应该是<code>"ABBA"</code>'
|
||||
testString: 'collection = collectionCopy; assert(updateRecords(5439, "artist", "ABBA")[5439]["artist"] === "ABBA", "After <code>updateRecords(5439, "artist", "ABBA")</code>, <code>artist</code> should be <code>"ABBA"</code>");'
|
||||
- text: '在<code>updateRecords(5439, "tracks", "Take a Chance on Me")</code> , <code>tracks</code>应该以<code>"Take a Chance on Me"</code>作为最后一个元素。'
|
||||
testString: 'assert(updateRecords(5439, "tracks", "Take a Chance on Me")[5439]["tracks"].pop() === "Take a Chance on Me", "After <code>updateRecords(5439, "tracks", "Take a Chance on Me")</code>, <code>tracks</code> should have <code>"Take a Chance on Me"</code> as the last element.");'
|
||||
- text: '在<code>updateRecords(2548, "artist", "")</code> ,不应该设置<code>artist</code>'
|
||||
testString: 'updateRecords(2548, "artist", ""); assert(!collection[2548].hasOwnProperty("artist"), "After <code>updateRecords(2548, "artist", "")</code>, <code>artist</code> should not be set");'
|
||||
- text: '在<code>updateRecords(1245, "tracks", "Addicted to Love")</code> , <code>tracks</code>应该将<code>"Addicted to Love"</code>作为最后一个元素。'
|
||||
testString: 'assert(updateRecords(1245, "tracks", "Addicted to Love")[1245]["tracks"].pop() === "Addicted to Love", "After <code>updateRecords(1245, "tracks", "Addicted to Love")</code>, <code>tracks</code> should have <code>"Addicted to Love"</code> as the last element.");'
|
||||
- text: '在<code>updateRecords(2468, "tracks", "Free")</code> , <code>tracks</code>应该以<code>"1999"</code>作为第一个元素。'
|
||||
testString: 'assert(updateRecords(2468, "tracks", "Free")[2468]["tracks"][0] === "1999", "After <code>updateRecords(2468, "tracks", "Free")</code>, <code>tracks</code> should have <code>"1999"</code> as the first element.");'
|
||||
- text: '在<code>updateRecords(2548, "tracks", "")</code> ,不应设置<code>tracks</code>'
|
||||
testString: 'updateRecords(2548, "tracks", ""); assert(!collection[2548].hasOwnProperty("tracks"), "After <code>updateRecords(2548, "tracks", "")</code>, <code>tracks</code> should not be set");'
|
||||
- text: '在<code>updateRecords(1245, "album", "Riptide")</code> , <code>album</code>应该是<code>"Riptide"</code>'
|
||||
testString: 'assert(updateRecords(1245, "album", "Riptide")[1245]["album"] === "Riptide", "After <code>updateRecords(1245, "album", "Riptide")</code>, <code>album</code> should be <code>"Riptide"</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-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"
|
||||
}
|
||||
};
|
||||
// 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) {
|
||||
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
// Alter values below to test your code
|
||||
updateRecords(5439, "artist", "ABBA");
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,88 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244e0
|
||||
title: Replacing If Else Chains with Switch
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 如果用交换机替换其他链条
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果您有许多选项可供选择,那么<code>switch</code>语句比许多链接的<code>if</code> / <code>else if</code>语句更容易编写。下列: <blockquote> if(val === 1){ <br> answer =“a”; <br> } else if(val === 2){ <br> answer =“b”; <br> } else { <br> answer =“c”; <br> } </blockquote>可以替换为: <blockquote> switch(val){ <br>情况1: <br> answer =“a”; <br>打破; <br>案例2: <br> answer =“b”; <br>打破; <br>默认: <br> answer =“c”; <br> } </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将链接的<code>if</code> / <code>else if</code>语句更改为<code>switch</code>语句。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您不应该在编辑器中的任何位置使用任何<code>else</code>语句
|
||||
testString: 'assert(!/else/g.test(code), "You should not use any <code>else</code> statements anywhere in the editor");'
|
||||
- text: 您不应在编辑器中的任何位置使用任何<code>if</code>语句
|
||||
testString: 'assert(!/if/g.test(code), "You should not use any <code>if</code> statements anywhere in the editor");'
|
||||
- text: 你应该至少有四个<code>break</code>语句
|
||||
testString: 'assert(code.match(/break/g).length >= 4, "You should have at least four <code>break</code> statements");'
|
||||
- text: <code>chainToSwitch("bob")</code>应该是“Marley”
|
||||
testString: 'assert(chainToSwitch("bob") === "Marley", "<code>chainToSwitch("bob")</code> should be "Marley"");'
|
||||
- text: <code>chainToSwitch(42)</code>应该是“答案”
|
||||
testString: 'assert(chainToSwitch(42) === "The Answer", "<code>chainToSwitch(42)</code> should be "The Answer"");'
|
||||
- text: <code>chainToSwitch(1)</code>应该是“没有#1”
|
||||
testString: 'assert(chainToSwitch(1) === "There is no #1", "<code>chainToSwitch(1)</code> should be "There is no #1"");'
|
||||
- text: <code>chainToSwitch(99)</code>应该是“错过了我这么多!”
|
||||
testString: 'assert(chainToSwitch(99) === "Missed me by this much!", "<code>chainToSwitch(99)</code> should be "Missed me by this much!"");'
|
||||
- text: <code>chainToSwitch(7)</code>应该是“Ate Nine”
|
||||
testString: 'assert(chainToSwitch(7) === "Ate Nine", "<code>chainToSwitch(7)</code> should be "Ate Nine"");'
|
||||
- text: <code>chainToSwitch("John")</code>应为“”(空字符串)
|
||||
testString: 'assert(chainToSwitch("John") === "", "<code>chainToSwitch("John")</code> should be "" (empty string)");'
|
||||
- text: <code>chainToSwitch(156)</code>应为“”(空字符串)
|
||||
testString: 'assert(chainToSwitch(156) === "", "<code>chainToSwitch(156)</code> should be "" (empty string)");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function chainToSwitch(val) {
|
||||
var answer = "";
|
||||
// Only change code below this line
|
||||
|
||||
if (val === "bob") {
|
||||
answer = "Marley";
|
||||
} else if (val === 42) {
|
||||
answer = "The Answer";
|
||||
} else if (val === 1) {
|
||||
answer = "There is no #1";
|
||||
} else if (val === 99) {
|
||||
answer = "Missed me by this much!";
|
||||
} else if (val === 7) {
|
||||
answer = "Ate Nine";
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
return answer;
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
chainToSwitch(7);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c2
|
||||
title: Return a Value from a Function with Return
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 从带返回的函数返回值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们可以将值传递给带<dfn>参数</dfn>的函数。您可以使用<code>return</code>语句从函数中发回一个值。 <strong>例</strong> <blockquote> function plusThree(num){ <br>返回num + 3; <br> } <br> var answer = plusThree(5); // 8 </blockquote> <code>plusThree</code>接受<code>num</code>的<dfn>参数</dfn>并返回一个等于<code>num + 3</code>的值。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个接受一个参数的函数<code>timesFive</code> ,将其乘以<code>5</code> ,然后返回新值。有关如何测试<code>timesFive</code>函数的示例,请参阅编辑器中的最后一行。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>timesFive</code>应该是一个功能
|
||||
testString: 'assert(typeof timesFive === "function", "<code>timesFive</code> should be a function");'
|
||||
- text: <code>timesFive(5)</code>应该返回<code>25</code>
|
||||
testString: 'assert(timesFive(5) === 25, "<code>timesFive(5)</code> should return <code>25</code>");'
|
||||
- text: <code>timesFive(2)</code>应该返回<code>10</code>
|
||||
testString: 'assert(timesFive(2) === 10, "<code>timesFive(2)</code> should return <code>10</code>");'
|
||||
- text: <code>timesFive(0)</code>应该返回<code>0</code>
|
||||
testString: 'assert(timesFive(0) === 0, "<code>timesFive(0)</code> should return <code>0</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
function minusSeven(num) {
|
||||
return num - 7;
|
||||
}
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
console.log(minusSeven(10));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,71 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c4
|
||||
title: Return Early Pattern for Functions
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 返回函数的早期模式
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">当达到<code>return</code>语句时,当前函数的执行停止,控制返回到调用位置。 <strong>例</strong> <blockquote> function myFun(){ <br>的console.log( “你好”); <br>回归“世界”; <br>的console.log( “BYEBYE”) <br> } <br> myFun(); </blockquote>上面输出“Hello”到控制台,返回“World”,但是<code>"byebye"</code>永远不输出,因为函数退出<code>return</code>语句。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">修改函数<code>abTest</code>以便如果<code>a</code>或<code>b</code>小于<code>0</code> ,函数将立即以<code>undefined</code>值退出。 <strong>暗示</strong> <br>请记住, <a href="http://www.freecodecamp.org/challenges/understanding-uninitialized-variables" target="_blank"><code>undefined</code>是一个关键字</a> ,而不是一个字符串。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>abTest(2,2)</code>应返回一个数字'
|
||||
testString: 'assert(typeof abTest(2,2) === "number" , "<code>abTest(2,2)</code> should return a number");'
|
||||
- text: '<code>abTest(2,2)</code>应该返回<code>8</code>'
|
||||
testString: 'assert(abTest(2,2) === 8 , "<code>abTest(2,2)</code> should return <code>8</code>");'
|
||||
- text: '<code>abTest(-2,2)</code>应返回<code>undefined</code>'
|
||||
testString: 'assert(abTest(-2,2) === undefined , "<code>abTest(-2,2)</code> should return <code>undefined</code>");'
|
||||
- text: '<code>abTest(2,-2)</code>应返回<code>undefined</code>'
|
||||
testString: 'assert(abTest(2,-2) === undefined , "<code>abTest(2,-2)</code> should return <code>undefined</code>");'
|
||||
- text: '<code>abTest(2,8)</code>应该返回<code>18</code>'
|
||||
testString: 'assert(abTest(2,8) === 18 , "<code>abTest(2,8)</code> should return <code>18</code>");'
|
||||
- text: '<code>abTest(3,3)</code>应该返回<code>12</code>'
|
||||
testString: 'assert(abTest(3,3) === 12 , "<code>abTest(3,3)</code> should return <code>12</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function abTest(a, b) {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
|
||||
}
|
||||
|
||||
// Change values below to test your code
|
||||
abTest(2,2);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,63 @@
|
||||
---
|
||||
id: 5679ceb97cbaa8c51670a16b
|
||||
title: Returning Boolean Values from Functions
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 从函数返回布尔值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以从<a href="waypoint-comparison-with-the-equality-operator" target="_blank">与Equality运算符的比较中</a>回忆一下,所有比较运算符都返回布尔值<code>true</code>或<code>false</code>值。有时人们使用if / else语句进行比较,如下所示: <blockquote> function isEqual(a,b){ <br> if(a === b){ <br>返回true; <br> } else { <br>返回虚假; <br> } <br> } </blockquote>但是有一种更好的方法可以做到这一点。由于<code>===</code>返回<code>true</code>或<code>false</code> ,我们可以返回比较结果: <blockquote> function isEqual(a,b){ <br>返回a === b; <br> } </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">修复函数<code>isLess</code>以删除<code>if/else</code>语句。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>isLess(10,15)</code>应该返回<code>true</code>'
|
||||
testString: 'assert(isLess(10,15) === true, "<code>isLess(10,15)</code> should return <code>true</code>");'
|
||||
- text: '<code>isLess(15,10)</code>应该返回<code>false</code>'
|
||||
testString: 'assert(isLess(15, 10) === false, "<code>isLess(15,10)</code> should return <code>false</code>");'
|
||||
- text: 您不应该使用任何<code>if</code>或<code>else</code>语句
|
||||
testString: 'assert(!/if|else/g.test(code), "You should not use any <code>if</code> or <code>else</code> statements");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function isLess(a, b) {
|
||||
// Fix this code
|
||||
if (a < b) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Change these values to test
|
||||
isLess(10, 15);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,70 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244dd
|
||||
title: Selecting from Many Options with Switch Statements
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 从带有开关语句的多个选项中进行选择
|
||||
---
|
||||
|
||||
## Description
|
||||
undefined
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">编写一个switch语句,测试<code>val</code>并设置以下条件的<code>answer</code> : <br> <code>1</code> - “alpha” <br> <code>2</code> - “beta” <br> <code>3</code> - “伽玛” <br> <code>4</code> - “三角洲” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>caseInSwitch(1)</code>的值应为“alpha”
|
||||
testString: 'assert(caseInSwitch(1) === "alpha", "<code>caseInSwitch(1)</code> should have a value of "alpha"");'
|
||||
- text: <code>caseInSwitch(2)</code>的值应为“beta”
|
||||
testString: 'assert(caseInSwitch(2) === "beta", "<code>caseInSwitch(2)</code> should have a value of "beta"");'
|
||||
- text: <code>caseInSwitch(3)</code>的值应为“gamma”
|
||||
testString: 'assert(caseInSwitch(3) === "gamma", "<code>caseInSwitch(3)</code> should have a value of "gamma"");'
|
||||
- text: <code>caseInSwitch(4)</code>的值应为“delta”
|
||||
testString: 'assert(caseInSwitch(4) === "delta", "<code>caseInSwitch(4)</code> should have a value of "delta"");'
|
||||
- text: 您不应该使用任何<code>if</code>或<code>else</code>语句
|
||||
testString: 'assert(!/else/g.test(code) || !/if/g.test(code), "You should not use any <code>if</code> or <code>else</code> statements");'
|
||||
- text: 你应该至少有3个<code>break</code>语句
|
||||
testString: 'assert(code.match(/break/g).length > 2, "You should have at least 3 <code>break</code> statements");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function caseInSwitch(val) {
|
||||
var answer = "";
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
return answer;
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
caseInSwitch(1);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244bc
|
||||
title: Shopping List
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 购物清单
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在变量<code>myList</code>创建购物清单。该列表应该是包含多个子阵列的多维数组。每个子数组中的第一个元素应包含一个带有项目名称的字符串。第二个元素应该是一个代表数量的数字,即<code>["Chocolate Bar", 15]</code>列表中应该至少有5个子数组。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myList</code>应该是一个数组
|
||||
testString: 'assert(isArray, "<code>myList</code> should be an array");'
|
||||
- text: 每个子数组中的第一个元素都必须是字符串
|
||||
testString: 'assert(hasString, "The first elements in each of your sub-arrays must all be strings");'
|
||||
- text: 每个子数组中的第二个元素都必须是数字
|
||||
testString: 'assert(hasNumber, "The second elements in each of your sub-arrays must all be numbers");'
|
||||
- text: 您的列表中必须至少有5个项目
|
||||
testString: 'assert(count > 4, "You must have at least 5 items in your list");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var myList = [];
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,106 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c6
|
||||
title: Stand in Line
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 站在队中
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在计算机科学中, <dfn>队列</dfn>是一个抽象的<dfn>数据结构</dfn> ,其中项目按顺序保存。可以在<code>queue</code>的后面添加新项目,并从<code>queue</code>的前面取出旧项目。编写一个函数<code>nextInLine</code> ,它接受一个数组( <code>arr</code> )和一个数字( <code>item</code> )作为参数。将数字添加到数组的末尾,然后删除数组的第一个元素。然后, <code>nextInLine</code>函数应返回已删除的元素。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>nextInLine([], 5)</code>应返回一个数字。'
|
||||
testString: 'assert.isNumber(nextInLine([],5), "<code>nextInLine([], 5)</code> should return a number.");'
|
||||
- text: '<code>nextInLine([], 1)</code>应该返回<code>1</code>'
|
||||
testString: 'assert(nextInLine([],1) === 1, "<code>nextInLine([], 1)</code> should return <code>1</code>");'
|
||||
- text: '<code>nextInLine([2], 1)</code>应返回<code>2</code>'
|
||||
testString: 'assert(nextInLine([2],1) === 2, "<code>nextInLine([2], 1)</code> should return <code>2</code>");'
|
||||
- text: '<code>nextInLine([5,6,7,8,9], 1)</code>应该返回<code>5</code>'
|
||||
testString: 'assert(nextInLine([5,6,7,8,9],1) === 5, "<code>nextInLine([5,6,7,8,9], 1)</code> should return <code>5</code>");'
|
||||
- text: '在<code>nextInLine(testArr, 10)</code> , <code>testArr[4]</code>应为<code>10</code>'
|
||||
testString: 'nextInLine(testArr, 10); assert(testArr[4] === 10, "After <code>nextInLine(testArr, 10)</code>, <code>testArr[4]</code> should be <code>10</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function nextInLine(arr, item) {
|
||||
// Your code here
|
||||
|
||||
return item; // Change this line
|
||||
}
|
||||
|
||||
// Test Setup
|
||||
var testArr = [1,2,3,4,5];
|
||||
|
||||
// Display Code
|
||||
console.log("Before: " + JSON.stringify(testArr));
|
||||
console.log(nextInLine(testArr, 6)); // Modify this line to test
|
||||
console.log("After: " + JSON.stringify(testArr));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
|
||||
```js
|
||||
var logOutput = [];
|
||||
var originalConsole = console
|
||||
function capture() {
|
||||
var nativeLog = console.log;
|
||||
console.log = function (message) {
|
||||
logOutput.push(message);
|
||||
if(nativeLog.apply) {
|
||||
nativeLog.apply(originalConsole, arguments);
|
||||
} else {
|
||||
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
|
||||
nativeLog(nativeMsg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function uncapture() {
|
||||
console.log = originalConsole.log;
|
||||
}
|
||||
|
||||
capture();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,65 @@
|
||||
---
|
||||
id: bd7993c9c69feddfaeb8bdef
|
||||
title: Store Multiple Values in one Variable using JavaScript Arrays
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript数组在一个变量中存储多个值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">使用JavaScript <code>array</code>变量,我们可以在一个地方存储多个数据。你开始一个带有开口方括号的数组声明,用一个结束的方括号结束,并在每个条目之间加一个逗号,如下所示: <code>var sandwich = ["peanut butter", "jelly", "bread"]</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">修改新数组<code>myArray</code> ,使其包含<code>string</code>和<code>number</code> ( <code>myArray</code>顺序)。 <strong>暗示</strong> <br>如果卡住,请参阅文本编辑器中的示例代码。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myArray</code>应该是一个<code>array</code> 。
|
||||
testString: 'assert(typeof myArray == "object", "<code>myArray</code> should be an <code>array</code>.");'
|
||||
- text: <code>myArray</code>的第一项应该是一个<code>string</code> 。
|
||||
testString: 'assert(typeof myArray[0] !== "undefined" && typeof myArray[0] == "string", "The first item in <code>myArray</code> should be a <code>string</code>.");'
|
||||
- text: <code>myArray</code>的第二项应该是一个<code>number</code> 。
|
||||
testString: 'assert(typeof myArray[1] !== "undefined" && typeof myArray[1] == "number", "The second item in <code>myArray</code> should be a <code>number</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = ["John", 23];
|
||||
|
||||
// Only change code below this line.
|
||||
var myArray = [];
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244a8
|
||||
title: Storing Values with the Assignment Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用赋值运算符存储值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在JavaScript中,您可以使用<dfn>赋值</dfn>运算符将值存储在变量中。 <code>myVariable = 5;</code>这将<code>Number</code>值<code>5</code>分配给<code>myVariable</code> 。作业总是从右到左。在将值分配给运算符左侧的变量之前,将解析<code>=</code>运算符右侧的所有内容。 <blockquote> myVar = 5; <br> myNum = myVar; </blockquote>这将为<code>myVar</code>分配<code>5</code> ,然后再次将<code>myVar</code>解析为<code>5</code>并将其分配给<code>myNum</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将值<code>7</code>分配给变量<code>a</code> 。分配的内容<code>a</code>变量<code>b</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 不要更改行上方的代码
|
||||
testString: 'assert(/var a;/.test(code) && /var b = 2;/.test(code), "Do not change code above the line");'
|
||||
- text: <code>a</code>的值应为7
|
||||
testString: 'assert(typeof a === "number" && a === 7, "<code>a</code> should have a value of 7");'
|
||||
- text: <code>b</code>的值应为7
|
||||
testString: 'assert(typeof b === "number" && b === 7, "<code>b</code> should have a value of 7");'
|
||||
- text: <code>a</code>应分配给<code>b</code> <code>=</code>
|
||||
testString: 'assert(/b\s*=\s*a\s*;/g.test(code), "<code>a</code> should be assigned to <code>b</code> with <code>=</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var a;
|
||||
var b = 2;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
|
||||
```js
|
||||
if (typeof a != 'undefined') {
|
||||
a = undefined;
|
||||
}
|
||||
if (typeof b != 'undefined') {
|
||||
b = undefined;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,59 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb4bdef
|
||||
title: Subtract One Number from Another with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript从另一个数字中减去一个数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们也可以从另一个数字中减去一个数字。 JavaScript使用<code>-</code>符号进行减法。 <p> <strong>例</strong> </p><blockquote> myVar = 12 - 6; //分配6 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改<code>0</code>因此差异为<code>12</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: ''
|
||||
testString: 'assert(difference === 12, "Make the variable <code>difference</code> equal 12.");'
|
||||
- text: 只从45中减去一个数字。
|
||||
testString: 'assert(/var\s*difference\s*=\s*45\s*-\s*[0-9]*;(?!\s*[a-zA-Z0-9]+)/.test(code),"Only subtract one number from 45.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var difference = 45 - 0;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,67 @@
|
||||
---
|
||||
id: 567af2437cbaa8c51670a16c
|
||||
title: Testing Objects for Properties
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 测试属性的对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有时检查给定对象的属性是否存在是有用的。我们可以使用对象的<code>.hasOwnProperty(propname)</code>方法来确定该对象是否具有给定的属性名称。 <code>.hasOwnProperty()</code>如果找到属性则返回<code>true</code>或<code>false</code> 。 <strong>例</strong> <blockquote> var myObj = { <br>顶部:“帽子”, <br>底部:“裤子” <br> }; <br> myObj.hasOwnProperty( “顶部”); //真的<br> myObj.hasOwnProperty( “中间”); //假</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">修改函数<code>checkObj</code>以测试<code>myObj</code>的<code>checkProp</code> 。如果找到该属性,则返回该属性的值。如果没有,请返回<code>"Not Found"</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>checkObj("gift")</code>应该返回<code>"pony"</code> 。
|
||||
testString: 'assert(checkObj("gift") === "pony", "<code>checkObj("gift")</code> should return <code>"pony"</code>.");'
|
||||
- text: <code>checkObj("pet")</code>应该返回<code>"kitten"</code> 。
|
||||
testString: 'assert(checkObj("pet") === "kitten", "<code>checkObj("pet")</code> should return <code>"kitten"</code>.");'
|
||||
- text: <code>checkObj("house")</code>应该返回<code>"Not Found"</code> 。
|
||||
testString: 'assert(checkObj("house") === "Not Found", "<code>checkObj("house")</code> should return <code>"Not Found"</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myObj = {
|
||||
gift: "pony",
|
||||
pet: "kitten",
|
||||
bed: "sleigh"
|
||||
};
|
||||
|
||||
function checkObj(checkProp) {
|
||||
// Your Code Here
|
||||
|
||||
return "Change Me!";
|
||||
}
|
||||
|
||||
// Test your code by modifying these values
|
||||
checkObj("gift");
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ba
|
||||
title: Understand String Immutability
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 理解字符串不变性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在JavaScript中, <code>String</code>值是<dfn>不可变的</dfn> ,这意味着一旦创建它们就不能被更改。例如,以下代码: <blockquote> var myStr =“Bob”; <br> myStr [0] =“J”; </blockquote>无法将<code>myStr</code>的值更改为“Job”,因为<code>myStr</code>的内容无法更改。请注意,这<em>并不</em>意味着<code>myStr</code>不能改变的,只是一个<dfn>字符串</dfn>的单个字符不能改变。更改<code>myStr</code>的唯一方法是为其分配一个新字符串,如下所示: <blockquote> var myStr =“Bob”; <br> myStr =“工作”; </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更正<code>myStr</code>的赋值,使其包含<code>Hello World</code>的字符串值,使用上面示例中显示的方法。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myStr</code>应该具有<code>Hello World</code>的值
|
||||
testString: 'assert(myStr === "Hello World", "<code>myStr</code> should have a value of <code>Hello World</code>");'
|
||||
- text: 不要更改行上方的代码
|
||||
testString: 'assert(/myStr = "Jello World"/.test(code), "Do not change the code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myStr = "Jello World";
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
myStr[0] = "H"; // Fix Me
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,66 @@
|
||||
---
|
||||
id: bd7123c9c441eddfaeb5bdef
|
||||
title: Understanding Boolean Values
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 了解布尔值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">另一种数据类型是<dfn>布尔值</dfn> 。 <code>Booleans</code>可能只是两个值中的一个: <code>true</code>或<code>false</code> 。它们基本上是很少的开关,其中<code>true</code>为“on”而<code>false</code>为“off”。这两个国家是相互排斥的。 <strong>注意</strong> <br> <code>Boolean</code>值永远不会用引号写。 <code>strings</code> <code>"true"</code>和<code>"false"</code>不是<code>Boolean</code> ,在JavaScript中没有特殊含义。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">修改<code>welcomeToBooleans</code>函数,以便在单击运行按钮时返回<code>true</code>而不是<code>false</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>welcomeToBooleans()</code>函数应该返回一个布尔值(true / false)。
|
||||
testString: 'assert(typeof welcomeToBooleans() === "boolean", "The <code>welcomeToBooleans()</code> function should return a boolean (true/false) value.");'
|
||||
- text: <code>welcomeToBooleans()</code>应该返回true。
|
||||
testString: 'assert(welcomeToBooleans() === true, "<code>welcomeToBooleans()</code> should return true.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function welcomeToBooleans() {
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
return false; // Change this line
|
||||
|
||||
// Only change code above this line.
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,67 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ab
|
||||
title: Understanding Case Sensitivity in Variables
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 了解变量中的大小写敏感度
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在JavaScript中,所有变量和函数名称都区分大小写。这意味着资本化很重要。 <code>MYVAR</code>与<code>MyVar</code>和<code>myvar</code> 。可以有多个具有相同名称但不同外壳的不同变量。强烈建议您为清晰起见, <em>不要</em>使用此语言功能。 <h4>最佳实践</h4>在<dfn>camelCase中用</dfn> JavaScript编写变量名。在<dfn>camelCase中</dfn> ,多字变量名称的第一个单词为小写,每个后续单词的首字母大写。 <strong>例子:</strong> <blockquote> var someVariable; <br> var anotherVariableName; <br> var thisVariableNameIsSoLong; </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">修改现有的声明和赋值,使其名称使用<dfn>camelCase</dfn> 。 <br>不要创建任何新变量。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>studlyCapVar</code>已定义且值为<code>10</code>
|
||||
testString: 'assert(typeof studlyCapVar !== "undefined" && studlyCapVar === 10, "<code>studlyCapVar</code> is defined and has a value of <code>10</code>");'
|
||||
- text: <code>properCamelCase</code>已定义且值为<code>"A String"</code>
|
||||
testString: 'assert(typeof properCamelCase !== "undefined" && properCamelCase === "A String", "<code>properCamelCase</code> is defined and has a value of <code>"A String"</code>");'
|
||||
- text: <code>titleCaseOver</code>已定义,其值为<code>9000</code>
|
||||
testString: 'assert(typeof titleCaseOver !== "undefined" && titleCaseOver === 9000, "<code>titleCaseOver</code> is defined and has a value of <code>9000</code>");'
|
||||
- text: <code>studlyCapVar</code>应该在声明和赋值部分使用camelCase。
|
||||
testString: 'assert(code.match(/studlyCapVar/g).length === 2, "<code>studlyCapVar</code> should use camelCase in both declaration and assignment sections.");'
|
||||
- text: <code>properCamelCase</code>应该在声明和赋值部分使用camelCase。
|
||||
testString: 'assert(code.match(/properCamelCase/g).length === 2, "<code>properCamelCase</code> should use camelCase in both declaration and assignment sections.");'
|
||||
- text: <code>titleCaseOver</code>应该在声明和赋值部分使用camelCase。
|
||||
testString: 'assert(code.match(/titleCaseOver/g).length === 2, "<code>titleCaseOver</code> should use camelCase in both declaration and assignment sections.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Declarations
|
||||
var StUdLyCapVaR;
|
||||
var properCamelCase;
|
||||
var TitleCaseOver;
|
||||
|
||||
// Assignments
|
||||
STUDLYCAPVAR = 10;
|
||||
PRoperCAmelCAse = "A String";
|
||||
tITLEcASEoVER = 9000;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 598e8944f009e646fc236146
|
||||
title: Understanding Undefined Value returned from a Function
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 了解从函数返回的未定义值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">函数可以包含<code>return</code>语句但不必包含。在函数没有<code>return</code>语句的情况下,当您调用它时,该函数处理内部代码但返回的值是<code>undefined</code> 。 <strong>例</strong> <blockquote> var sum = 0; <br> function addSum(num){ <br> sum = sum + num; <br> } <br> var returnedValue = addSum(3); //将修改sum,但返回值未定义</blockquote> <code>addSum</code>是一个没有<code>return</code>语句的函数。该函数将更改全局<code>sum</code>变量,但函数的返回值<code>undefined</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个没有任何参数的函数<code>addFive</code> 。此函数向<code>sum</code>变量添加5,但其返回值<code>undefined</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>addFive</code>应该是一个函数
|
||||
testString: 'assert(typeof addFive === "function", "<code>addFive</code> should be a function");'
|
||||
- text: <code>sum</code>应该等于8
|
||||
testString: 'assert(sum === 8, "<code>sum</code> should be equal to 8");'
|
||||
- text: <code>addFive</code>返回值应该是<code>undefined</code>
|
||||
testString: 'assert(addFive() === undefined, "Returned value from <code>addFive</code> should be <code>undefined</code>");'
|
||||
- text: 在函数内部,向<code>sum</code>变量添加5
|
||||
testString: 'assert(code.match(/(sum\s*\=\s*sum\s*\+\s*5)|(sum\s*\+\=\s*5)/g).length === 1, "Inside of your functions, add 5 to the <code>sum</code> variable");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var sum = 0;
|
||||
function addThree() {
|
||||
sum = sum + 3;
|
||||
}
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
var returnedValue = addFive();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244aa
|
||||
title: Understanding Uninitialized Variables
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 了解未初始化的变量
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">声明JavaScript变量时,它们的初始值为<code>undefined</code> 。如果对<code>undefined</code>变量进行数学运算,则结果将为<code>NaN</code> ,表示<dfn>“非数字”</dfn> 。如果将字符串与<code>undefined</code>变量连接起来,您将得到一个<code>"undefined"</code>的文字<dfn>字符串</dfn> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">初始化三个变量<code>a</code> , <code>b</code>和<code>c</code>与<code>5</code> , <code>10</code> ,和<code>"I am a"</code>分别让他们不会<code>undefined</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应定义并评估其值为<code>6</code>
|
||||
testString: 'assert(typeof a === "number" && a === 6, "<code>a</code> should be defined and evaluated to have the value of <code>6</code>");'
|
||||
- text: 应定义和评估<code>b</code>的值为<code>15</code>
|
||||
testString: 'assert(typeof b === "number" && b === 15, "<code>b</code> should be defined and evaluated to have the value of <code>15</code>");'
|
||||
- text: <code>c</code>不应该包含<code>undefined</code>并且值应为“我是一个字符串!”
|
||||
testString: 'assert(!/undefined/.test(c) && c === "I am a String!", "<code>c</code> should not contain <code>undefined</code> and should have a value of "I am a String!"");'
|
||||
- text: 不要更改行下方的代码
|
||||
testString: 'assert(/a = a \+ 1;/.test(code) && /b = b \+ 5;/.test(code) && /c = c \+ " String!";/.test(code), "Do not change code below the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Initialize these three variables
|
||||
var a;
|
||||
var b;
|
||||
var c;
|
||||
|
||||
// Do not change code below this line
|
||||
|
||||
a = a + 1;
|
||||
b = b + 5;
|
||||
c = c + " String!";
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d1
|
||||
title: Updating Object Properties
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 更新对象属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在创建JavaScript对象之后,您可以随时更新其属性,就像更新任何其他变量一样。您可以使用点或括号表示法进行更新。例如,让我们看看我们的<code>ourDog</code> : <blockquote> var ourDog = { <br> “名字”:“露营者”, <br> “腿”:4, <br> “尾巴”:1, <br> “朋友们”:[“一切!”] <br> }; </blockquote>由于他是一只特别开心的狗,让我们改名为“快乐露营者”。以下是我们更新对象名称属性的方法: <code>ourDog.name = "Happy Camper";</code>或者我们的<code>ourDog["name"] = "Happy Camper";</code>现在,当我们评估我们的<code>ourDog.name</code> ,而不是获得“Camper”时,我们将获得他的新名字“Happy Camper”。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更新<code>myDog</code>对象的name属性。让我们将她的名字从“Coder”改为“Happy Coder”。您可以使用点或括号表示法。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 将<code>myDog</code>的<code>"name"</code>属性更新为“Happy Coder”。
|
||||
testString: 'assert(/happy coder/gi.test(myDog.name), "Update <code>myDog</code>'s <code>"name"</code> property to equal "Happy Coder".");'
|
||||
- text: 不要编辑<code>myDog</code>定义
|
||||
testString: 'assert(/"name": "Coder"/.test(code), "Do not edit the <code>myDog</code> definition");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"]
|
||||
};
|
||||
|
||||
ourDog.name = "Happy Camper";
|
||||
|
||||
// Setup
|
||||
var myDog = {
|
||||
"name": "Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"]
|
||||
};
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,70 @@
|
||||
---
|
||||
id: bd7123c9c549eddfaeb5bdef
|
||||
title: Use Bracket Notation to Find the First Character in a String
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用括号表示法查找字符串中的第一个字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>Bracket notation</code>是一种在字符串中的特定<code>index</code>处获取字符的方法。大多数现代编程语言,如JavaScript,都不像人类那样开始计算。它们从0开始。这称为<dfn>基于零的</dfn>索引。例如,单词“Charles”中索引0处的字符是“C”。因此,如果<code>var firstName = "Charles"</code> ,则可以使用<code>firstName[0]</code>获取字符串第一个字母的值。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<dfn>括号表示法</dfn>查找<code>lastName</code>变量中的第一个字符并将其分配给<code>firstLetterOfLastName</code> 。 <strong>暗示</strong> <br>如果卡住,请尝试查看<code>firstLetterOfFirstName</code>变量声明。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>firstLetterOfLastName</code>变量的值应为<code>L</code>
|
||||
testString: 'assert(firstLetterOfLastName === "L", "The <code>firstLetterOfLastName</code> variable should have the value of <code>L</code>.");'
|
||||
- text: 您应该使用括号表示法。
|
||||
testString: 'assert(code.match(/firstLetterOfLastName\s*?=\s*?lastName\[.*?\]/), "You should use bracket notation.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var firstLetterOfFirstName = "";
|
||||
var firstName = "Ada";
|
||||
|
||||
firstLetterOfFirstName = firstName[0];
|
||||
|
||||
// Setup
|
||||
var firstLetterOfLastName = "";
|
||||
var lastName = "Lovelace";
|
||||
|
||||
// Only change code below this line
|
||||
firstLetterOfLastName = lastName;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,67 @@
|
||||
---
|
||||
id: bd7123c9c451eddfaeb5bdef
|
||||
title: Use Bracket Notation to Find the Last Character in a String
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用括号表示法查找字符串中的最后一个字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">为了获得字符串的最后一个字母,您可以从字符串的长度中减去一个字母。例如,如果<code>var firstName = "Charles"</code> ,则可以使用<code>firstName[firstName.length - 1]</code>获取字符串最后一个字母的值。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<dfn>括号表示法</dfn>查找<code>lastName</code>变量中的最后一个字符。 <strong>暗示</strong> <br>如果卡住了,请尝试查看<code>lastLetterOfFirstName</code>变量声明。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>lastLetterOfLastName</code>应为“e”。
|
||||
testString: 'assert(lastLetterOfLastName === "e", "<code>lastLetterOfLastName</code> should be "e".");'
|
||||
- text: 你必须使用<code>.length</code>来获取最后一个字母。
|
||||
testString: 'assert(code.match(/\.length/g).length === 2, "You have to use <code>.length</code> to get the last letter.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var firstName = "Ada";
|
||||
var lastLetterOfFirstName = firstName[firstName.length - 1];
|
||||
|
||||
// Setup
|
||||
var lastName = "Lovelace";
|
||||
|
||||
// Only change code below this line.
|
||||
var lastLetterOfLastName = lastName;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,67 @@
|
||||
---
|
||||
id: bd7123c9c450eddfaeb5bdef
|
||||
title: Use Bracket Notation to Find the Nth Character in a String
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用括号表示法查找字符串中的第N个字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您还可以使用<dfn>括号表示法</dfn>来获取字符串中其他位置的字符。请记住,计算机从<code>0</code>开始计数,因此第一个字符实际上是第0个字符。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">让我们尝试使用括号表示法将<code>thirdLetterOfLastName</code>设置为等于<code>lastName</code>变量的第三个字母。 <strong>暗示</strong> <br>如果卡住,请尝试查看<code>secondLetterOfFirstName</code>变量声明。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>thirdLetterOfLastName</code>变量的值应为<code>v</code> 。
|
||||
testString: 'assert(thirdLetterOfLastName === "v", "The <code>thirdLetterOfLastName</code> variable should have the value of <code>v</code>.");'
|
||||
- text: 您应该使用括号表示法。
|
||||
testString: 'assert(code.match(/thirdLetterOfLastName\s*?=\s*?lastName\[.*?\]/), "You should use bracket notation.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var firstName = "Ada";
|
||||
var secondLetterOfFirstName = firstName[1];
|
||||
|
||||
// Setup
|
||||
var lastName = "Lovelace";
|
||||
|
||||
// Only change code below this line.
|
||||
var thirdLetterOfLastName = lastName;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,67 @@
|
||||
---
|
||||
id: bd7123c9c452eddfaeb5bdef
|
||||
title: Use Bracket Notation to Find the Nth-to-Last Character in a String
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用括号表示法查找字符串中的第N个到最后一个字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以使用我们刚刚用于检索字符串中最后一个字符的相同原理来检索N到最后一个字符。例如,您可以使用<code>firstName[firstName.length - 3]</code>获取<code>var firstName = "Charles"</code>字符串的倒数第三个字母的值</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<dfn>括号表示法</dfn>查找<code>lastName</code>字符串中倒数第二个字符。 <strong>暗示</strong> <br>如果卡住,请尝试查看<code>thirdToLastLetterOfFirstName</code>变量声明。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>secondToLastLetterOfLastName</code>应为“c”。
|
||||
testString: 'assert(secondToLastLetterOfLastName === "c", "<code>secondToLastLetterOfLastName</code> should be "c".");'
|
||||
- text: 你必须使用<code>.length</code>来获得倒数第二个字母。
|
||||
testString: 'assert(code.match(/\.length/g).length === 2, "You have to use <code>.length</code> to get the second last letter.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var firstName = "Ada";
|
||||
var thirdToLastLetterOfFirstName = firstName[firstName.length - 3];
|
||||
|
||||
// Setup
|
||||
var lastName = "Lovelace";
|
||||
|
||||
// Only change code below this line
|
||||
var secondToLastLetterOfLastName = lastName;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@@ -0,0 +1,77 @@
|
||||
---
|
||||
id: cf1111c1c12feddfaeb3bdef
|
||||
title: Use Conditional Logic with If Statements
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用条件逻辑和If语句
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>If</code>语句用于在代码中做出决定。关键字<code>if</code>告诉JavaScript在括号中定义的特定条件下执行花括号中的代码。这些条件称为<code>Boolean</code>条件,它们可能只是<code>true</code>或<code>false</code> 。当条件计算结果为<code>true</code> ,程序将执行花括号内的语句。当布尔条件的计算结果为<code>false</code> ,大括号内的语句将不会执行。 <strong>伪代码</strong> <blockquote> if( <i>condition为true</i> ){ <br> <i>声明被执行</i> <br> } </blockquote> <strong>例</strong> <blockquote>功能测试(myCondition){ <br> if(myCondition){ <br>回归“这是真的”; <br> } <br>返回“这是假的”; <br> } <br>测试(真); //返回“这是真的” <br>测试(假); //返回“这是假的” </blockquote>当使用值<code>true</code>调用<code>test</code> , <code>if</code>语句将评估<code>myCondition</code>以查看它是否为<code>true</code> 。因为它是<code>true</code> ,函数返回<code>"It was true"</code> 。当我们使用<code>false</code>值调用<code>test</code>时, <code>myCondition</code> <em>不为</em> <code>true</code>并且不执行花括号中的语句,函数返回<code>"It was false"</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在函数内部创建一个<code>if</code>语句<code>"Yes, that was true"</code>如果参数<code>wasThatTrue</code>为<code>true</code>则返回<code>"Yes, that was true"</code> <code>"No, that was false"</code>否则返回<code>"No, that was false"</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>trueOrFalse</code>应该是一个函数
|
||||
testString: 'assert(typeof trueOrFalse === "function", "<code>trueOrFalse</code> should be a function");'
|
||||
- text: <code>trueOrFalse(true)</code>应该返回一个字符串
|
||||
testString: 'assert(typeof trueOrFalse(true) === "string", "<code>trueOrFalse(true)</code> should return a string");'
|
||||
- text: <code>trueOrFalse(false)</code>应该返回一个字符串
|
||||
testString: 'assert(typeof trueOrFalse(false) === "string", "<code>trueOrFalse(false)</code> should return a string");'
|
||||
- text: <code>trueOrFalse(true)</code>应该返回“是的,那是真的”
|
||||
testString: 'assert(trueOrFalse(true) === "Yes, that was true", "<code>trueOrFalse(true)</code> should return "Yes, that was true"");'
|
||||
- text: <code>trueOrFalse(false)</code>应该返回“No,that was false”
|
||||
testString: 'assert(trueOrFalse(false) === "No, that was false", "<code>trueOrFalse(false)</code> should return "No, that was false"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
function ourTrueOrFalse(isItTrue) {
|
||||
if (isItTrue) {
|
||||
return "Yes, it's true";
|
||||
}
|
||||
return "No, it's false";
|
||||
}
|
||||
|
||||
// Setup
|
||||
function trueOrFalse(wasThatTrue) {
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
|
||||
|
||||
// Only change code above this line.
|
||||
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
trueOrFalse(true);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user