freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions.english.md
Oliver Eyton-Williams f1c9b08cf3 fix(curriculum): add isHidden: false to challenges
This includes certificates (where it does nothing), but does not
include any translations.
2020-05-25 16:25:19 +05:30

1.8 KiB

id, title, challengeType, isHidden, forumTopicId
id title challengeType isHidden forumTopicId
587d7b88367417b2b2512b46 Set Default Parameters for Your Functions 1 false 301209

Description

In order to help us create more flexible functions, ES6 introduces default parameters for functions. Check out this code:
const greeting = (name = "Anonymous") => "Hello " + name;

console.log(greeting("John")); // Hello John
console.log(greeting()); // Hello 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 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

Modify the function 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: A default parameter value of <code>1</code> should be used for <code>value</code>.
    testString: assert(code.match(/value\s*=\s*1/g));

Challenge Seed

// Only change code below this line
const increment = (number, value) => number + value;
// Only change code above this line

Solution

const increment = (number, value = 1) => number + value;