Created
November 23, 2016 19:09
-
-
Save Ajax30/2d98493715edf0f3723e84c0542a9b14 to your computer and use it in GitHub Desktop.
Add the arguments passed to a function 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add(){ | |
// Create an array from the functions arguments object | |
// then sum the array members using reduce | |
var sum = Array.from(arguments).reduce(function(a, b){ | |
return a + b; | |
}); | |
console.log(sum); | |
} | |
// You can now add as many numbers as you like | |
// just by passing them to the function | |
add(1, 2, 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment