* fix(curriculum): tests quotes * fix(curriculum): fill seed-teardown * fix(curriculum): fix tests and remove unneeded seed-teardown
2.1 KiB
2.1 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
587d7b88367417b2b2512b46 | Set Default Parameters for Your Functions | 1 |
Description
function greeting(name = "Anonymous") {The default parameter kicks in when the argument is not specified (it is undefined). As you can see in the example above, the parameter
return "Hello " + name;
}
console.log(greeting("John")); // Hello John
console.log(greeting()); // Hello Anonymous
name
will receive its default value "Anonymous"
when you do not provide a value for the parameter. You can add default values for as many parameters as you want.
Instructions
increment
by adding default parameters so that it will add 1 to number
if value
is not specified.
Tests
tests:
- text: The result of <code>increment(5, 2)</code> should be <code>7</code>.
testString: assert(increment(5, 2) === 7, 'The result of <code>increment(5, 2)</code> should be <code>7</code>.');
- text: The result of <code>increment(5)</code> should be <code>6</code>.
testString: assert(increment(5) === 6, 'The result of <code>increment(5)</code> should be <code>6</code>.');
- text: default parameter <code>1</code> was used for <code>value</code>.
testString: getUserInput => assert(getUserInput('index').match(/value\s*=\s*1/g), 'default parameter <code>1</code> was used for <code>value</code>.');
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