Add solution to Arguments Optional challenge (#35654)

* Add solution to Arguments Optional challenge

* fix: remove repl.it links

* fix: rename variables

* fix: remove unused parts of template

* fix: update links markdown

* fix: replaced existing Intermediate with this one
This commit is contained in:
Nikolai Kiselev
2019-03-29 17:17:56 +04:00
committed by Randell Dawson
parent e5aabc38f5
commit 03c603787f

View File

@ -16,9 +16,9 @@ It can be quite complicated to understand what needs to be done. There are alway
### Relevant Links ### Relevant Links
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array' target='_blank' rel='nofollow'>Arrays</a> * [Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof' target='_blank' rel='nofollow'>typeof</a> * [Typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments' target='_blank' rel='nofollow'>arguments object</a> * [Arguments Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments)
## ![:speech_balloon:](https://forum.freecodecamp.com/images/emoji/emoji_one/speech_balloon.png?v=3 ":speech_balloon:") Hint: 1 ## ![:speech_balloon:](https://forum.freecodecamp.com/images/emoji/emoji_one/speech_balloon.png?v=3 ":speech_balloon:") Hint: 1
@ -92,7 +92,6 @@ In the case that only one argument was passed, do not worry about how to prompt
// test here // test here
addTogether(2,3); addTogether(2,3);
``` ```
![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") <a href='https://repl.it/CLnz/0' target='_blank' rel='nofollow'>Run Code</a>
### Code Explanation: ### Code Explanation:
@ -105,65 +104,31 @@ In the case that only one argument was passed, do not worry about how to prompt
#### Relevant Links #### Relevant Links
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof' target='_blank' rel='nofollow'>typeof</a> * [Typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments' target='_blank' rel='nofollow'>arguments object</a> * [Arguments Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments)
## ![:sunflower:](https://forum.freecodecamp.com/images/emoji/emoji_one/sunflower.png?v=3 ":sunflower:") Intermediate Code Solution: ## ![:sunflower:](https://forum.freecodecamp.com/images/emoji/emoji_one/sunflower.png?v=3 ":sunflower:") Intermediate Code Solution:
```javascript ```javascript
function addTogether() { function addTogether(first, second) {
var args = new Array(arguments.length); if (typeof first !== 'number') {
//Storing the arguments in an array
for(var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
//Check for the arguments length
if(args.length == 2){
//If there are two arguments,check for the type of both arguments
//Use typeof to check the type of the argument(both should be numbers)
if(typeof args[0] !== 'number' || typeof args[1] !=='number' ){
return undefined; return undefined;
} }
return args[0]+args[1]; const sum = second => (typeof second === 'number' ? first + second : undefined);
} return typeof second === 'undefined' ? second => sum(second) : sum(second);
//When only one argument is provided }
if(args.length == 1){ // test here
a= args[0]; addTogether(2,3);
//Check the argument using typeof
if(typeof a!=='number'){
return undefined;
}
else{
//Making use of closures
return function(b){
//Checking the second argument
if(typeof b !=='number'){
return undefined;
}
else
return a+b;
};
}
}
}
// test here
addTogether(2,3);
``` ```
![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") <a href='https://repl.it/CLoA/0' target='_blank' rel='nofollow'>Run Code</a>
### Code Explanation: ### Code Explanation:
* First store the arguments in an array by creating an array using the constructor method. * Return `undefined` if first argument is not a `number` or second argument is defined, but not a `number`.
* Adds each argument to the new array. * Return sum of the arguments if both are provided otherwise return a sum function.
* Then check for the length of the new array as we need to know if we have enough or not.
* Check for the type of the arguments using `typeof` as they both should be numbers.
* Returns undefined if any of them is not a number, or returns the sum of them if they are.
* If there was only one argument, we still check the type after storing it in a new variable and returning a new function or undefined.
#### Relevant Links #### Relevant Links
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof' target='_blank' rel='nofollow'>typeof</a> * [Typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments' target='_blank' rel='nofollow'>arguments object</a> * [Arguments Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments)
## ![:rotating_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/rotating_light.png?v=3 ":rotating_light:") Advanced Code Solution: ## ![:rotating_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/rotating_light.png?v=3 ":rotating_light:") Advanced Code Solution:
```javascript ```javascript
@ -182,7 +147,6 @@ In the case that only one argument was passed, do not worry about how to prompt
// test here // test here
addTogether(2,3); addTogether(2,3);
``` ```
![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") <a href='https://repl.it/CLoB/0' target='_blank' rel='nofollow'>Run Code</a>
### Code Explanation: ### Code Explanation:
@ -192,9 +156,9 @@ In the case that only one argument was passed, do not worry about how to prompt
#### Relevant Links #### Relevant Links
* <a href='http://forum.freecodecamp.com/t/javascript-array-prototype-reduce/14299' target='_blank' rel='nofollow'>Array.prototype.reduce</a> * [Array.prototype.reduce](https://forum.freecodecamp.com/t/javascript-array-prototype-reduce/14299)
* <a href='http://forum.freecodecamp.com/t/javascript-array-prototype-some/14304' target='_blank' rel='nofollow'>Array.prototype.some</a> * [Array.prototype.some](https://forum.freecodecamp.com/t/javascript-array-prototype-some/14304)
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from' target='_blank' rel='nofollow'>Array.from</a> * [Array.from](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from)
## ![:clipboard:](https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:") NOTES FOR CONTRIBUTIONS: ## ![:clipboard:](https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:") NOTES FOR CONTRIBUTIONS: