2

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);
4
  • 3
    You don't return arguments.callee(...). Commented Oct 6, 2013 at 2:23
  • OK, so I rewrote it with a named function and now it's working. 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)); Commented Oct 6, 2013 at 2:31
  • I recommend not using arguments.callee or arguments.caller; it will be removed in ECMAScript 6, and is already not available in ECMAScript 5 Strict Mode. Commented Oct 6, 2013 at 2:39
  • "OK, so I rewrote it with a named function and now it's working" - Yes, but it works more because you added the missing return keyword 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. Commented Oct 6, 2013 at 2:42

2 Answers 2

1

Change it to

return arguments.callee( s + y[0], y.slice(1))

Or just use reduce :-) :

[1,2,3,4].reduce( function(sum, x) { return sum + x; }, 0 );

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Sign up to request clarification or add additional context in comments.

3 Comments

A return is also needed before the IIFE. return (function (s, y) {...
Why do you pass a second argument to reduce? It is unnecessary.
It's the initial value to pass to the first arg to the callback. It's for clarity.
0

If what you said in the code's comment is true, this is what you need.

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.

    return (function (s, y) {
        if (y instanceof Array && y.length !== 0) {
            return arguments.callee(arguments.callee(s, y[0]), y.slice(1));
        } else if (typeof y === 'number') {
            return s + y;
        } else {
            return s;
        }
    })(0, i);
}

Output

var x = [1, 2, 3, 4, 5];
console.log(arraySum(x));
x = [1, 2, [3, 4, 5]];
console.log(arraySum(x));
x = [1, "2", 2, [3, 4, 5]];
console.log(arraySum(x));
x = [1, "2", [2, [3, 4, 5]]];
console.log(arraySum(x));

1 Comment

I should have removed those comments. That's the part of the quiz I was still working on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.