freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions.chinese.md
Kristofer Koishigawa b3213fc892 fix(i18n): chinese test suite (#38220)
* fix: Chinese test suite

Add localeTiltes, descriptions, and adjust test text and testStrings to get the automated test suite working.

* fix: ran script, updated testStrings and solutions
2020-03-03 18:49:47 +05:30

1.8 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
587d7b88367417b2b2512b46 Set Default Parameters for Your Functions 1 设置函数的默认参数

Description

为了帮助我们创建更灵活的函数ES6引入了函数的默认参数 。看看这段代码:
function greetingname =“Anonymous”{
返回“你好”+名字;
}
的console.log问候语 “约翰”)); // 你好约翰
的console.log问候; //你好匿名
默认参数在未指定参数时启动(未定义)。正如您在上面的示例中所看到的,当您未为参数提供值时,参数name将接收其默认值"Anonymous" 。您可以根据需要为任意数量的参数添加默认值。

Instructions

修改函数increment加入默认参数这样它会加1 number ,如果value未指定。

Tests

tests:
  - text: '<code>increment(5, 2)</code>应为<code>7</code> 。'
    testString: assert(increment(5, 2) === 7);
  - text: <code>increment(5)</code>的结果应为<code>6</code> 。
    testString: assert(increment(5) === 6);
  - text: 默认参数<code>1</code>用于<code>value</code> 。
    testString: assert(code.match(/value\s*=\s*1/g));

Challenge Seed

const increment = (function() {
  "use strict";
  return function increment(number, value) {
    return number + value;
  };
})();
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns 6

Solution

// solution required