Files
Randell Dawson 1494a50123 fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles
* fix: added 3 dashes line before prob expl
* fix: added 3 dashes line before hints
* fix: added 3 dashes line before solutions
2019-07-24 13:29:27 +05:30

1.5 KiB

title
title
Set Default Parameters for Your Functions

Set Default Parameters for Your Functions


Problem Explanation

We'll be modifying the increment function so that the number parameter is incremented by 1 by default, by setting value to 1 if a value for value is not passed to the increment function.


Hints

Hint 1

Let's identify where the parameter value is in JS function

try to solve the problem now

Hint 2

Set value equal to something so that it is that value by default

try to solve the problem now


Solutions

Solution 1 (Click to Show/Hide)
const increment = (function() {
  "use strict";
  return function increment(number, value = 1) {
    return number + value;
  };
})();
console.log(increment(5, 2)); // returns 7
console.log(increment(5)); // returns NaN

Code Explanation

  • This section is pretty straightforward. Pass this section by setting the value parameter equal to 1. When the function comes across test cases where value has not been passed anything, then value will be assigned one by default.

Relevant Links:

JavaScript default parameters

  • DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Categorize the solution in one of the following categories — Basic, Intermediate and Advanced.