* feat: removed IIFE and added solution * feat: updated challenges seed, test and solution * style: removed semicolon * feat: updated seed and solution * feat: updated challenges seed and solution * feat: updated test, seed and solution * fix: added seed code to solution * fix: removed function and added solution * fix: removed makeClass function and fixed solution * style: removed excessive semicolons * Fixed spacing for note in instructions section * fix: removed assert messages and used const * fix: regex fails correctly now
1.8 KiB
1.8 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);
- text: The result of <code>increment(5)</code> should be <code>6</code>.
testString: assert(increment(5) === 6);
- text: Default parameter <code>1</code> was used for <code>value</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;