Created
August 14, 2014 11:57
-
-
Save fakefarm/22e704e7ee03e994f228 to your computer and use it in GitHub Desktop.
have some fun
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 sum ( args ) { | |
var array, sum = 0; // Set vars | |
// Conditional to type check what the arguments are | |
if ( typeof args == 'number' ) { // If not an array, then convert it to one. | |
array = Array.prototype.slice.call(arguments, 0); // Gotcha, using the key word 'arguments', not the 'args' word. 'arguments' is a special word in javascript that looks like an array, but is not. | |
} else { | |
array = args; | |
} | |
for ( i in array ) { // I assume recursion could be used here if I knew how recursion worked. | |
sum += array[i]; | |
} | |
return sum; // Must explicity use return or you will get back undefined. | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// chat w/ @dhassler
nth([10,20,30], 2) // returns 20