Skip to content

Instantly share code, notes, and snippets.

@jvnlwn
Created October 4, 2013 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvnlwn/6819029 to your computer and use it in GitHub Desktop.
Save jvnlwn/6819029 to your computer and use it in GitHub Desktop.
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