* fix: move "Explore Differences Between..." to basic JS, update seed and tests * fix: resequence "Declare String Variables" * fix: move "Declare a Read-Only Variable..." to basic JS, update seed and tests * fix: revert changes to non-English "Explore Differences Between..." test text * fix: revert test strings, solutions, and seeds for non-English challenges * fix: update "Declare String Variables" description * fix: sync quotation marks in description and seed * fix: modify note in "Declare a Read-Only..." challenge * fix: update operator and compound assignment challenges * fix: update string challenges * fix: update array and array method challenges * fix: update function and scope challenges, resequence slightly * fix: "Word Blanks" solution * fix: add spacing to seed * fix: concatenating += challenge spacing * fix: appending variables to strings spacing * fix: find the length of a string spacing * fix: removed instances of removedFromMyArray = 0 * fix: switch challenges * fix: function argument and param spacing * fix: update counting cards, object challenges, and record collection * fix: finish rest of Basic JS section * fix: introducing else statements solution * fix: update spacing and wording * fix: update wording for const challenge * fix: update functional programming challenges * fix: intermediate algorithms and cert challenges * fix: revert some spacing and remove comments for fp challenge solutions * feat: add notes with links to moved let and const challenges in first two es6 challenges * fix: update es6 intro text * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/declare-a-read-only-variable-with-the-const-keyword.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * fix: concatenating strings with plus operator seed * fix: add comments back to Declare a Read-Only Variable... seed * feat: add es6 to basic javascript redirect tests for let and const challenges * fix: revert "Concatenating Strings with Plus Operator" seed * fix: move test file to cypress/integration/learn/redirects, separate redirect tests Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
75 lines
2.1 KiB
Markdown
75 lines
2.1 KiB
Markdown
---
|
|
id: 587d7b8e367417b2b2512b5f
|
|
title: Pass Arguments to Avoid External Dependence in a Function
|
|
challengeType: 1
|
|
forumTopicId: 301234
|
|
dashedName: pass-arguments-to-avoid-external-dependence-in-a-function
|
|
---
|
|
|
|
# --description--
|
|
|
|
The last challenge was a step closer to functional programming principles, but there is still something missing.
|
|
|
|
We didn't alter the global variable value, but the function `incrementer` would not work without the global variable `fixedValue` being there.
|
|
|
|
Another principle of functional programming is to always declare your dependencies explicitly. This means if a function depends on a variable or object being present, then pass that variable or object directly into the function as an argument.
|
|
|
|
There are several good consequences from this principle. The function is easier to test, you know exactly what input it takes, and it won't depend on anything else in your program.
|
|
|
|
This can give you more confidence when you alter, remove, or add new code. You would know what you can or cannot change and you can see where the potential traps are.
|
|
|
|
Finally, the function would always produce the same output for the same set of inputs, no matter what part of the code executes it.
|
|
|
|
# --instructions--
|
|
|
|
Let's update the `incrementer` function to clearly declare its dependencies.
|
|
|
|
Write the `incrementer` function so it takes an argument, and then returns a result after increasing the value by one.
|
|
|
|
# --hints--
|
|
|
|
Your function `incrementer` should not change the value of `fixedValue` (which is `4`).
|
|
|
|
```js
|
|
assert(fixedValue === 4);
|
|
```
|
|
|
|
Your `incrementer` function should take an argument.
|
|
|
|
```js
|
|
assert(incrementer.length === 1);
|
|
```
|
|
|
|
Your `incrementer` function should return a value that is one larger than the `fixedValue` value.
|
|
|
|
```js
|
|
const __newValue = incrementer(fixedValue);
|
|
assert(__newValue === 5);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
// The global variable
|
|
let fixedValue = 4;
|
|
|
|
// Only change code below this line
|
|
function incrementer() {
|
|
|
|
|
|
// Only change code above this line
|
|
}
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
let fixedValue = 4;
|
|
|
|
function incrementer(fixedValue) {
|
|
return fixedValue + 1;
|
|
}
|
|
```
|