Why does this function return undefined?
The interior function returns the correct value.
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.
(function (s, y) {
if (!y || y.length < 1) {
//console.log(s);
// s is the correct value
return s;
} else {
arguments.callee(s + y[0], y.slice(1));
}
})(0, i);
}
var x = [1, 2, 3, 4, 5];
arraySum(x);
arguments.callee(...).function arraySum(i) { function acc (s, y) { if (!y || y.length < 1) { //console.log(s); // s is the correct value return s; } else { return acc(s + y[0], y.slice(1)); } } return acc(0,i); } var x = [1, 2, 3, 4, 5]; console.log(arraySum(x));arguments.calleeorarguments.caller; it will be removed in ECMAScript 6, and is already not available in ECMAScript 5 Strict Mode.returnkeyword in two places than because you introduced a function name. In any case, if you've figured out a solution you should post it as an answer - it's acceptable here at StackOverflow to answer your own questions.