diff --git a/challenges/02-javascript-algorithms-and-data-structures/debugging-cn.json b/challenges/02-javascript-algorithms-and-data-structures/debugging-cn.json new file mode 100644 index 00000000..621ff50f --- /dev/null +++ b/challenges/02-javascript-algorithms-and-data-structures/debugging-cn.json @@ -0,0 +1,646 @@ +{ + "name": "Debugging", + "order": 4, + "time": "1 hour", + "helpRoom": "Help", + "challenges": [ + { + "id": "587d7b83367417b2b2512b33", + "title": "Use the JavaScript Console to Check the Value of a Variable", + "description": [ + "Chrome 和 Firefox 都有出色的 JavaScript 控制台,也称为 DevTools,用于调试你的 JavaScript 代码。", + "你可以在 Chrome 的菜单中找到”开发者工具”或 FireFox 的菜单中的 “Web 控制台”。如果你使用其他浏览器或手机,我们强烈建议你切换到桌面版 Firefox 或 Chrome。", + "console.log() 方法可能是最有用的调试工具,它可以将括号中的内容输出到控制台,。将它放在代码中的关键点可以显示变量的中间值。在查看输出之前,最好先了解输出应该是什么。在整个代码中使用检查点来查看计算状态将有助于缩小问题所在。", + "下面是输出 'Hello world!' 到控制台的示例:", + "console.log('Hello world!');", + "
", + "请使用 console.log() 方法在代码中注明的地方输出变量 a 的值。" + ], + "tests": [ + { + "text": + "你的代码应使用 console.log() 来检查变量 a 的值。", + "testString": + "assert(code.match(/console\\.log\\(a\\)/g), '你的代码应使用 console.log() 来检查变量 a 的值。');" + } + ], + "solutions": [], + "hints": [], + "releasedOn": "Feb 17, 2017", + "challengeType": 1, + "translations": {}, + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "let a = 5;", + "let b = 1;", + "a++;", + "// 请把你的代码写在这条注释以下", + "", + "", + "let sumAB = a + b;", + "console.log(sumAB);" + ], + "head": [], + "tail": [] + } + } + }, + { + "id": "587d7b83367417b2b2512b37", + "title": + "Understanding the Differences between the freeCodeCamp and Browser Console", + "description": [ + "你可能已经注意到一些 freeCodeCamp JavaScript 的挑战有他们自己的控制台。这些控制台的行为与你在上一次挑战中使用的浏览器控制台略有不同。", + "以下挑战旨在强调 freeCodeCamp 控制台与浏览器控制台之间的一些差异。", + "对于浏览器控制台。 当你在浏览器中加载并运行 JavaScript 文件时, console.log() 语句会在控制台中按照你调用的次数准确地打印出你要求的内容。 然而,在 freeCodeCamp 的代码编辑器中使用 console.log() 会略有不同,一开始可能会让你感到困惑。", + "在 freeCodeCamp 代码编辑器中,传给 console.log() 的值会在每组测试执行的时候输出。另外,如果你在代码中还手动调用过挑战题目的函数,调用几次就会增加几次传入值的输出。", + "这就产生了一些有趣的行为,并可能在一开始就让你感到困惑,因为你觉得只会输出一次的值可能会输出多次,具体次数取决于挑战题目本身测试的数量以及这些测试调用挑战函数的方式", + "如果你不打算执行挑战的测试,而只想查看自己调用 console.log() 的输出", + "
", + "使用 console.log() 在代码中指定的位置打印变量。", + "" + ], + "tests": [ + { + "text": + "使用 console.log() 输出变量 outputTwice 的值。 在浏览器控制台中,应该输出两次变量的值。", + "testString": + "assert(code.match(/console\\.log\\(outputTwo\\)/g), '使用 console.log() 输出变量 outputTwice 的值。 在浏览器控制台中,应该输出两次变量的值。');" + }, + { + "text": + "使用 console.log() 输出变量 outputOne 的值。", + "testString": + "assert(code.match(/console\\.log\\(outputOne\\)/g), '使用 console.log() 输出变量 outputOne 的值。');" + }, + { + "text": + "使用 console.clear() 修改输出,以便 outputOne 变量只输出一次。", + "testString": + "assert(code.match(/^(\\s*console.clear\\(\\);?\\s*)$/gm), '使用 console.clear() 修改输出,以便 outputOne 变量只输出一次。');" + } + ], + "solutions": [], + "hints": [], + "releasedOn": "Feb 17, 2017", + "challengeType": 1, + "translations": {}, + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "// 打开你的浏览器控制台", + "let outputTwo = \"This will print to the browser console 2 times\";", + "// 使用 console.log() 输出变量 outputTwo 的值", + "", + "", + "let outputOne = \"Try to get this to log only once to the browser console\";", + "// 在这条注释下面使用 console.clear() 以便输出一次变量 outputOne 的值", + "", + "", + "// 使用 console.log() 输出变量 outputOne 的值", + "", + "" + ], + "head": [], + "tail": [] + } + } + }, + { + "id": "587d7b84367417b2b2512b34", + "title": "Use typeof to Check the Type of a Variable", + "description": [ + "你可以使用 typeof 检查变量的数据结构或类型。在处理多种数据类型时,typeof 会对调试很有帮助。如果你认为你两数相加,而其中一个实际上是一个字符串,则结果可能是错误的。类型错误可能隐藏在计算或函数调用中。当你以 JavaScript 对象(JSON)的形式访问和使用外部数据时尤其要小心。", + "下面是使用 typeof 的一些示例:", + "
console.log(typeof \"\"); // 输出 \"string\"
console.log(typeof 0); // 输出 \"number\"
console.log(typeof []); // 输出 \"object\"
console.log(typeof {}); // 输出 \"object\"
", + "JavaScript 有六种原始(不可变)数据类型:Boolean, Null, Undefined, Number, String, 和 Symbol(ES6 新增)和一种可变的数据类型:Object 。注意,在 JavaScript 中,数组在本质上是一种对象", + "
", + "添加两个 console.log() 语句来检查代码中的两个变量 seventhree 的 code>typeof 值。" + ], + "tests": [ + { + "text": + "你的代码应在两个 console.log() 语句中使用 typeof 来检查变量的类型。", + "testString": + "assert(code.match(/console\\.log\\(typeof[\\( ].*\\)?\\)/g).length == 2, '你的代码应在两个 console.log() 语句中使用 typeof 来检查变量的类型。');" + }, + { + "text": + "你的代码应使用 typeof 来检查变量 seven 的类型。", + "testString": + "assert(code.match(/typeof[\\( ]seven\\)?/g), '你的代码应使用 typeof 来检查变量 seven 的类型。');" + }, + { + "text": + "你的代码应使用 typeof 来检查变量 three 的类型。", + "testString": + "assert(code.match(/typeof[\\( ]three\\)?/g), '你的代码应使用 typeof 来检查变量 three 的类型。');" + } + ], + "solutions": [], + "hints": [], + "releasedOn": "Feb 17, 2017", + "challengeType": 1, + "translations": {}, + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "let seven = 7;", + "let three = \"3\";", + "console.log(seven + three);", + "// 请把你的代码写在这条注释以下", + "" + ], + "head": [], + "tail": [] + } + } + }, + { + "id": "587d7b84367417b2b2512b35", + "title": "Catch Misspelled Variable and Function Names", + "description": [ + "console.log()typeof 方法是检查中间值和程序输出类型的两种主要方法。 现在是时候了解一下 bug 出现的常见的情形。一个语法级别的问题是打字太快带来的低级拼写错误。", + "变量或函数名的错写、漏写或大小写弄混都会让浏览器尝试查找并不存在的东西,并报出“引用错误”。JavaScript 变量和函数名称区分大小写。", + "
", + "修复代码中的两个拼写错误,以便 netWorkingCapital 计算有效。" + ], + "tests": [ + { + "text": + "检查计算 netWorkingCapital 值时使用的两个变量的拼写是否正确,控制台应该输出 \"Net working capital is: 2\"。", + "testString": + "assert(netWorkingCapital === 2, '检查计算 netWorkingCapital 值时使用的两个变量的拼写是否正确,控制台应该输出 \"Net working capital is: 2\"。');" + }, + { + "text": + "代码中不应存在拼写错误的变量。", + "testString": + "assert(!code.match(/recievables/g), '代码中不应存在拼写错误的变量。');" + }, + { + "text": + "应在代码中声明并正确使用 receivables 变量。", + "testString": + "assert(code.match(/receivables/g).length == 2, '应在代码中声明并正确使用 receivables 变量。');" + }, + { + "text": + "代码中不应存在拼写错误的变量。", + "testString": + "assert(!code.match(/payable;/g), '代码中不应存在拼写错误的变量。');" + }, + { + "text": + "应在代码中声明并正确使用 payables 变量。", + "testString": + "assert(code.match(/payables/g).length == 2, '应在代码中声明并正确使用 payables 变量。');" + } + ], + "solutions": [], + "hints": [], + "releasedOn": "Feb 17, 2017", + "challengeType": 1, + "translations": {}, + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "let receivables = 10;", + "let payables = 8;", + "let netWorkingCapital = recievables - payable;", + "console.log(`Net working capital is: ${netWorkingCapital}`);" + ], + "head": [], + "tail": [] + } + } + }, + { + "id": "587d7b84367417b2b2512b36", + "title": "Catch Unclosed Parentheses, Brackets, Braces and Quotes", + "description": [ + "要注意的另一个语法错误是所有的小括号、方括号、花括号和引号都必须配对。当你编辑代码并插入新代码其中带有括号时,很容易忘记括号闭合。 此外,在将代码块嵌套到其他代码块时要小心,例如将回调函数作为参数添加到方法中。", + "避免这种错误的一种方法是,一次性输入完这些符号,然后将光标移回它们之间继续编写。好在,现在大部分编辑器都会帮你自动补全。", + "
", + "修复代码中的两个符号配对错误。" + ], + "tests": [ + { + "text": "你的代码应该修复数组缺少的部分。", + "testString": + "assert(code.match(/myArray\\s*?=\\s*?\\[\\s*?1\\s*?,\\s*?2\\s*?,\\s*?3\\s*?\\];/g), '你的代码应该修复数组缺少的部分。');" + }, + { + "text": + "你的代码应该修复 .reduce() 方法缺少的部分。控制台应输出 \"Sum of array values is: 6\"。", + "testString": + "assert(arraySum === 6, '你的代码应该修复 .reduce() 方法缺少的部分。控制台应输出 \"Sum of array values is: 6\"。');" + } + ], + "solutions": [], + "hints": [], + "releasedOn": "Feb 17, 2017", + "challengeType": 1, + "translations": {}, + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "let myArray = [1, 2, 3;", + "let arraySum = myArray.reduce((previous, current => previous + current);", + "console.log(`Sum of array values is: ${arraySum}`);" + ], + "head": [], + "tail": [] + } + } + }, + { + "id": "587d7b84367417b2b2512b37", + "title": "Catch Mixed Usage of Single and Double Quotes", + "description": [ + "JavaScript允许使用单引号('')和双引号(\"\")声明字符串。决定使用哪一个通常看个人偏好,但有一些例外。 ", + "如果字符串中有缩写或存在一段带引号的文本,你就会明白为什么 JavaScript 允许两种引号了。请注意,不要过早结束字符串,这会导致语法错误。", + "下面是混合使用引号的一些示例:", + "
// 这些是正确的:
const grouchoContraction = \"I've had a perfectly wonderful evening, but this wasn't it.\";
const quoteInString = \"Groucho Marx once said 'Quote me as saying I was mis-quoted.'\";
// 这是不正确的:
const uhOhGroucho = 'I've had a perfectly wonderful evening, but this wasn't it.';
", + "当然,只使用一种引号是可以的。你可以使用反斜杠 (\\) 转义字符来转义字符串中的引号:", + "
// 一种引号的正确使用方式:
const allSameQuotes = 'I\\'ve had a perfectly wonderful evening, but this wasn\\'t it.';
", + "
", + "修复字符串,对 href 属性的值使用不同的引号,或者转义现有的引号。注意,整个字符串外面的双引号要保留。" + ], + "tests": [ + { + "text": + "你的代码应通过更改或转义来修复 href 的值 \"#Home\" 周围的引号。", + "testString": + "assert(code.match(//g), '你的代码应通过更改或转义来修复 href 的值 \"#Home\" 周围的引号。');" + }, + { + "text": + "你的代码应该在整个字符串外围保留双引号。", + "testString": + "assert(code.match(/\"

.*?<\\/p>\";/g), '你的代码应该在整个字符串外围保留双引号。');" + } + ], + "solutions": [], + "hints": [], + "releasedOn": "Feb 17, 2017", + "challengeType": 1, + "translations": {}, + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "let innerHtml = \"

Click here to return home

\";", + "console.log(innerHtml);" + ], + "head": [], + "tail": [] + } + } + }, + { + "id": "587d7b85367417b2b2512b38", + "title": "Catch Use of Assignment Operator Instead of Equality Operator", + "description": [ + "分支程序,即在满足某些条件时执行不同操作的程序,依赖于 JavaScript中的 ifelse ifelse 语句。条件有时采取测试一个结果是否等于一个值的形式。", + "这种逻辑可以表述为“如果 x 等于 y ,则......”,听起来像是可以使用 =(即赋值运算符)。然而,这会导致程序中流程出问题。", + "如前面的挑战所述,JavaScript 中的赋值运算符 (=) 是用来为变量名赋值的。并且 ===== 运算符检查相等性(三等号 === 是用来测试是否严格相等的,严格相等的意思是值和类型都必须相同)。", + "下面的代码将 x 赋值为 2,表达式 x = y 会在执行后得到 true 。JavaScript 会把大部分的值都视为 true,除了所谓的 \"falsy\" 值,即: false0\"\"(空字符串),NaNundefinednull 。", + "
let x = 1;
let y = 2;
if (x = y) {
  // 除了 \"falsy\" 值以外 y 为任意值时这个代码块都将执行
} else {
  // 按本例用意这个代码块应该执行(但其实不会)。
}
", + "
", + "修复条件语句,以便程序运行正确的分支,并给 result 赋上正确的值。" + ], + "tests": [ + { + "text": + "你的代码应该修复条件语句,使其判断是否相等,而不是赋值。", + "testString": + "assert(result == \"Not equal!\", '你的代码应该修复条件语句,使其判断是否相等,而不是赋值。');" + }, + { + "text": + "条件语句可以使用 =====来测试是否相等。", + "testString": + "assert(code.match(/x\\s*?===?\\s*?y/g), '条件语句可以使用 =====来测试是否相等。');" + } + ], + "solutions": [], + "hints": [], + "releasedOn": "Feb 17, 2017", + "challengeType": 1, + "translations": {}, + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "let x = 7;", + "let y = 9;", + "let result = \"to come\";", + "", + "if(x = y) {", + " result = \"Equal!\";", + "} else {", + " result = \"Not equal!\";", + "}", + "", + "console.log(result);" + ], + "head": [], + "tail": [] + } + } + }, + { + "id": "587d7b85367417b2b2512b39", + "title": + "Catch Missing Open and Closing Parenthesis After a Function Call", + "description": [ + "当函数或方法不接受任何参数时,你可能忘记在调用它时加上()左括号和右括号。通常,函数调用的结果会保存在变量中,供其他代码使用。可以通过将变量值(或其类型)打印到控制台,查看输出究竟是一个函数引用还是函数调用的返回值来检测这类错误", + "下面示例中的两个变量是不同的:", + "
function myFunction() {
  return \"You rock!\";
}
let varOne = myFunction; // 将函数赋值给变量
let varTwo = myFunction(); // 将函数返回值 \"You rock!\"赋给变量
", + "
", + "修复代码,把调用函数 getNin​​e 的返回值赋给变量 result。" + ], + "tests": [ + { + "text": + "你的代码应该修复变量 result 使其为函数 getNine 的返回值。", + "testString": + "assert(result == 9, '你的代码应该修复变量 result 使其为函数 getNine 的返回值。');" + }, + { + "text": "你的代码应该调用 getNine 函数。", + "testString": + "assert(code.match(/getNine\\(\\)/g).length == 2, '你的代码应该调用 getNine 函数。');" + } + ], + "solutions": [], + "hints": [], + "releasedOn": "Feb 17, 2017", + "challengeType": 1, + "translations": {}, + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function getNine() {", + " let x = 6;", + " let y = 3;", + " return x + y;", + "}", + "", + "let result = getNine;", + "console.log(result);" + ], + "head": [], + "tail": [] + } + } + }, + { + "id": "587d7b85367417b2b2512b3a", + "title": + "Catch Arguments Passed in the Wrong Order When Calling a Function", + "description": [ + "继续讨论调用函数,需要注意的下一个 bug 是函数的参数传递顺序错误。 如果参数分别是不同的类型,例如接受数组和整数两个参数的函数,参数顺序传错就可能会引发运行时错误。对于接受相同类型参数的函数,传错参数也会导致逻辑错误或运行结果错误。确保以正确的顺序提供所有必需的参数以避免这些问题。", + "
", + "函数 raiseToPower 返回基数 (base) 的指数 (exponent) 次幂。不幸的是,它没有被正确调用 ———— 修改代码,使 power 的值为 8。" + ], + "tests": [ + { + "text": + "你的代码应修复变量 power,使其等于 2 的 3 次方,而不是 3 的 2 次方。", + "testString": + "assert(power == 8, '你的代码应修复变量 power,使其等于 2 的 3 次方,而不是 3 的 2 次方。');" + }, + { + "text": + "你的代码调用 raiseToPower 函数时,传递参数的顺序应正确。", + "testString": + "assert(code.match(/raiseToPower\\(\\s*?base\\s*?,\\s*?exp\\s*?\\);/g), '你的代码调用 raiseToPower 函数时,传递参数的顺序应正确。');" + } + ], + "solutions": [], + "hints": [], + "releasedOn": "Feb 17, 2017", + "challengeType": 1, + "translations": {}, + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function raiseToPower(b, e) {", + " return Math.pow(b, e);", + "}", + "", + "let base = 2;", + "let exp = 3;", + "let power = raiseToPower(exp, base);", + "console.log(power);" + ], + "head": [], + "tail": [] + } + } + }, + { + "id": "587d7b86367417b2b2512b3b", + "title": "Catch Off By One Errors When Using Indexing", + "description": [ + "当你试图访问字符串或数组的特定索引(分割或访问一个片段)或循环索引时,有时会出现 Off by one errors 错误(有时称为 OBOE)。 JavaScript 索引从 0开始,而不是1,这意味着最后一个索引总会比字符串或数组的长度少 1。 如果你尝试访问等于长度的索引,程序可能会抛出“索引超出范围”引用错误或打印出 undefined。", + "当你使用将索引范围作为参数的字符串或数组方法时,阅读相关的文档并了解参数中的索引的包含性(即是否考虑进返回值中)很重要。以下是一些错误的示例:", + "
let alphabet = \"abcdefghijklmnopqrstuvwxyz\";
let len = alphabet.length;
for (let i = 0; i <= len; i++) {
  // loops one too many times at the end
  console.log(alphabet[i]);
}
for (let j = 1; j < len; j++) {
  // loops one too few times and misses the first character at index 0
  console.log(alphabet[j]);
}
for (let k = 0; k < len; k++) {
  // Goldilocks approves - this is just right
  console.log(alphabet[k]);
}
", + "
", + "修复以下函数中的两个索引错误,以便将 1 到 5 之间(包含 1 和 5)的所有数字打印到控制台。" + ], + "tests": [ + { + "text": + "你的代码应该设置循环的初始条件,使循环从第一个索引开始。", + "testString": + "assert(code.match(/i\\s*?=\\s*?0\\s*?;/g).length == 1, '你的代码应该设置循环的初始条件,使循环从第一个索引开始。');" + }, + { + "text": + "你的代码应修复循环的初始条件,使循环从索引 0 开始。", + "testString": + "assert(!code.match(/i\\s?=\\s*?1\\s*?;/g), '你的代码应修复循环的初始条件,使循环从索引 0 开始。');" + }, + { + "text": + "你的代码应设置循环的终止条件,使循环在最后一个索引处停止。", + "testString": + "assert(code.match(/i\\s*?<\\s*?len\\s*?;/g).length == 1, '你的代码应设置循环的终止条件,使循环在最后一个索引处停止。');" + }, + { + "text": + "你的代码应修复循环的终止条件,使循环在索引为字符串长度减 1 时停止。", + "testString": + "assert(!code.match(/i\\s*?<=\\s*?len;/g), '你的代码应修复循环的终止条件,使循环在索引为字符串长度减 1 时停止。');" + } + ], + "solutions": [], + "hints": [], + "releasedOn": "Feb 17, 2017", + "challengeType": 1, + "translations": {}, + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function countToFive() {", + " let firstFive = \"12345\";", + " let len = firstFive.length;", + " // 请修改这条注释以下的代码", + " for (let i = 1; i <= len; i++) {", + " // 请不要修改这条注释以下的代码", + " console.log(firstFive[i]);", + " }", + "}", + "", + "countToFive();" + ], + "head": [], + "tail": [] + } + } + }, + { + "id": "587d7b86367417b2b2512b3c", + "title": "Use Caution When Reinitializing Variables Inside a Loop", + "description": [ + "有时需要在循环中保存信息,增加计数器或重置变量。一个潜在的问题是变量什么时候该重新初始化,什么时候不该重新初始化,反之亦然。如果你不小心重置了用于终止条件的变量,导致无限循环,这将特别危险。", + "使用 console.log() 在每个循环中打印变量值可以发现与重置相关的错误行为,或者重置变量失败。", + "
", + "以下函数应该创建一个具有 m 行和 n 列“零”的二维数组。不幸的是,它没有产生预期的输出,因为row变量没有在外部循环中重新初始化(设置回空数组)。修改代码,使其正确地返回包含 3 行 2 列“零”的二维数组,即 [[0,0],[0,0],[0,0]] 。" + ], + "tests": [ + { + "text": + "你的代码应将变量 matrix 设置为 3 行 2 列“零”的二维数组。", + "testString": + "assert(JSON.stringify(matrix) == \"[[0,0],[0,0],[0,0]]\", '你的代码应将变量 matrix 设置为 3 行 2 列“零”的二维数组。');" + }, + { + "text": "变量 matrix 应有 3 行。", + "testString": + "assert(matrix.length == 3, '变量 matrix 应有 3 行。');" + }, + { + "text": + "变量 matrix 每行应有 2 列。", + "testString": + "assert(matrix[0].length == 2 && matrix[1].length === 2 && matrix[2].length === 2, '变量 matrix 每行应有 2 列。');" + } + ], + "solutions": [], + "hints": [], + "releasedOn": "Feb 17, 2017", + "challengeType": 1, + "translations": {}, + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function zeroArray(m, n) {", + " // 创建一个具有 m 行和 n 列“零”的二维数组", + " let newArray = [];", + " let row = [];", + " for (let i = 0; i < m; i++) {", + " // 添加 m 行到 newArray", + " ", + " for (let j = 0; j < n; j++) {", + " // Pushes n 个“零”到当前行以创建列", + " row.push(0);", + " }", + " // Pushes 当前里面有 n 个“零”的行到 newArray", + " newArray.push(row);", + " }", + " return newArray;", + "}", + "", + "let matrix = zeroArray(3, 2);", + "console.log(matrix);" + ], + "head": [], + "tail": [] + } + } + }, + { + "id": "587d7b86367417b2b2512b3d", + "title": "Prevent Infinite Loops with a Valid Terminal Condition", + "description": [ + "最后一个话题是可怕的无限循环。当你需要程序运行代码块一定次数或满足条件时,循环是很好的工具,但是它们需要终止条件来结束循环。无限循环可能会使浏览器冻结或崩溃,并导致程序执行混乱,没有人想要这样的结果。", + "在本节的介绍中有一个无限循环的例子——它没有终止条件来摆脱 loopy() 内的 while 循环。不要调用这个函数!", + "
function loopy() {
  while(true) {
    console.log(\"Hello, world!\");
  }
}
", + "程序员的工作是确保最终达到终止条件,该条件告诉程序何时跳出循环。有一种错误是从终端条件向错误方向递增或递减计数器变量。另一种是在循环代码中意外重置计数器或索引变量,而不是递增或递减它。", + "
", + "myFunc() 函数包含一个无限循环,因为终止条件 i != 4 永远不会为 false (并中断循环) - i 将每次递增2,然后跳过4,因为 i 是从奇数开始递增。在终端条件中输入比较运算符,使循环仅在 i 小于或等于4的情况下运行。" + ], + "tests": [ + { + "text": + "你的代码应该在 for 循环的终止条件(中间部分)中更改比较运算符。", + "testString": + "assert(code.match(/i\\s*?<=\\s*?4;/g).length == 1, '你的代码应该在 for 循环的终止条件(中间部分)中更改比较运算符。');" + }, + { + "text": + "你的代码应该在循环的终止条件中修复比较运算符。", + "testString": + "assert(!code.match(/i\\s*?!=\\s*?4;/g), '你的代码应该在循环的终止条件中修复比较运算符。');" + } + ], + "solutions": [], + "hints": [], + "releasedOn": "Feb 17, 2017", + "challengeType": 1, + "translations": {}, + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function myFunc() {", + " for (let i = 1; i != 4; i += 2) {", + " console.log(\"Still going!\");", + " }", + "}" + ], + "head": [], + "tail": [] + } + } + } + ] +}