Created
October 4, 2013 00:05
-
-
Save jvnlwn/6819029 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 arraySum(i) { | |
// i will be an array, containing integers, strings and/or arrays like itself. | |
// Sum all the integers you find, anywhere in the nest of arrays. | |
var total = 0; | |
blah(i) | |
function blah(newArray) { | |
array = newArray.slice() | |
newArray = []; | |
if(array.length === 0) { | |
return; | |
} else { | |
array.forEach(function(item) { | |
if(item.constructor === Array) { | |
newArray = item.slice() | |
} else{sum(item)} | |
}) | |
return blah(newArray) | |
} | |
} | |
function sum(value) { | |
if(typeof value === 'number') { | |
total += value; | |
} | |
} | |
return total; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment