.github
api-server
client
config
curriculum
docs
guide
arabic
chinese
english
3D
accessibility
agile
algorithms
android-development
angular
angularjs
apache
aspnet
bash
blockchain
book-recommendations
bootstrap
bsd-os
bulma
c
canvas
certifications
apis-and-microservices
coding-interview-prep
data-visualization
front-end-libraries
information-security-and-quality-assurance
javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
compare-scopes-of-the-var-and-let-keywords
create-an-export-fallback-with-export-default
create-strings-using-template-literals
declare-a-read-only-variable-with-the-const-keyword
explore-differences-between-the-var-and-let-keywords
import-a-default-export
mutate-an-array-declared-with-const
prevent-object-mutation
set-default-parameters-for-your-functions
index.md
understand-the-differences-between-import-and-require
use--to-import-everything-from-a-file
use-arrow-functions-to-write-concise-anonymous-functions
use-class-syntax-to-define-a-constructor-function
use-destructuring-assignment-to-assign-variables-from-arrays
use-destructuring-assignment-to-assign-variables-from-nested-objects
use-destructuring-assignment-to-assign-variables-from-objects
use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters
use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements
use-export-to-reuse-a-code-block
use-getters-and-setters-to-control-access-to-an-object
use-the-rest-operator-with-function-parameters
use-the-spread-operator-to-evaluate-arrays-in-place
write-arrow-functions-with-parameters
write-concise-declarative-functions-with-es6
write-concise-object-literal-declarations-using-simple-fields
write-higher-order-arrow-functions
index.md
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
index.md
responsive-web-design
index.md
chef
clojure
cloud-development
computational-genomics
computer-hardware
computer-science
containers
cplusplus
csharp
css
d3
data-science-tools
design-patterns
designer-tools
developer-ethics
developer-tools
devops
docker
documentation
electron
elixir
elm
erlang
game-development
gatsbyjs
git
go
groovy
haskell
hibernate
html
ionic
java
javascript
joomla
jquery
julia
kotlin
laravel
linux
logic
machine-learning
mathematics
meta
miscellaneous
mobile-app-development
mongodb
natural-language-processing
neovim
network-engineering
nginx
nodejs
php
product-design
progressive-web-apps
puppet
python
r
react
react-native
redux
rest-api
robotics
rt-os
ruby
rust
security
semantic-ui
software-engineering
sql
ssh
svg
svn
swift
terminal-commandline
tools
typescript
typography
user-experience-design
user-experience-research
vagrant
vim
virtualbox
visual-design
voice
vue
vue-cli
web-augmented-reality
web-components
web-performance
web-virtual-reality
wordpress
working-in-tech
xml
Blender
portuguese
russian
spanish
news
tools
.editorconfig
.eslintignore
.eslintrc
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env
71 lines
2.6 KiB
Markdown
71 lines
2.6 KiB
Markdown
![]() |
---
|
||
|
title: Set Default Parameters for Your Functions
|
||
|
---
|
||
|
## Set Default Parameters for Your Functions
|
||
|
|
||
|
|
||
|
:triangular_flag_on_post: Remember to use Read-Search-Ask if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:
|
||
|
|
||
|
|
||
|
### :checkered_flag: Problem Explanation:
|
||
|
```javascript
|
||
|
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 NaN
|
||
|
```
|
||
|
|
||
|
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.
|
||
|
|
||
|
### :speech_balloon: Hint: 1
|
||
|
|
||
|
Let's identify where the parameter **value** is in JS function
|
||
|
|
||
|
try to solve the problem now
|
||
|
|
||
|
### :speech_balloon: Hint: 2
|
||
|
|
||
|
Set **value** equal to something so that it is that value by default
|
||
|
|
||
|
try to solve the problem now
|
||
|
|
||
|
### Spoiler Alert!
|
||
|

|
||
|
|
||
|
Solution ahead!
|
||
|
|
||
|
## :beginner: Basic Code Solution:
|
||
|
```javascript
|
||
|
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
|
||
|
```
|
||
|
:rocket: [Run Code](https://repl.it/@RyanPisuena/PleasingFumblingThings)
|
||
|
|
||
|
## 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters)
|
||
|
|
||
|
# :clipboard: NOTES FOR CONTRIBUTIONS:
|
||
|
|
||
|
* :warning: 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.
|
||
|
* Add an explanation of your solution.
|
||
|
* Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. :traffic_light:
|
||
|
* Please add your username only if you have added any relevant main contents. ( :warning: DO NOT remove any existing usernames)
|
||
|
|
||
|
See :point_right: [Wiki Challenge Solution Template](https://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-template-guide/14272) for reference.
|
||
|
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|