1.6 KiB
1.6 KiB
id, challengeType, forumTopicId, localeTitle
id | challengeType | forumTopicId | localeTitle |
---|---|---|---|
587d7b88367417b2b2512b46 | 1 | 301209 | 设置函数的默认参数 |
Description
const greeting = (name = "Anonymous") => "Hello " + name;
console.log(greeting("John")); // Hello John
console.log(greeting()); // Hello Anonymous
默认参数会在参数没有被指定(值为 undefined )的时候起作用。在上面的例子中,参数name
会在没有得到新的值的时候,默认使用值 "Anonymous"。你还可以给多个参数赋予默认值。
Instructions
increment
加上默认参数,使得在value
没有被赋值的时候,默认给number
加1。
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>value</code>的默认值应该为<code>1</code>。
testString: assert(code.match(/value\s*=\s*1/g));
Challenge Seed
const increment = (number, value) => number + value;
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns 6
Solution
const increment = (number, value = 1) => number + value;