# Console.log(array) prints a result, but return(array) is undefined

Hi, this is my first post here.

I’m working on a problem in the Advanced Algorithm Scripting section, and while testing out the basic logic I ran into an issue that has me completely baffled. The following code is not going to be my solution to the challenge, but I would really like to know what’s going on here because either a) there’s a bug in my code that I’m not seeing, or b) there’s something more fundamental that I need to understand.

The core issue: After pushing items into an array, calling console.log will print the array as expected, but calling return yields undefined. I have absolutely no idea what is going wrong.

Here’s my test code:

``````function sym(args) {
var newArgs = Array.prototype.slice.call(arguments);
compare(newArgs[0],newArgs[1]);
}

function compare (a,b) {
var holder = [];
for (var i=0;i<a.length;i++) {
if (b.indexOf(a[i]) === -1) {
holder.push(a[i]);
}
}
for (var j=0;j<b.length;j++) {
if (a.indexOf(b[j]) === -1) {
holder.push(b[j]);
}
}
console.log(holder); //prints the final array as expected
return holder; //returns 'undefined' ?!?!?!
}

sym([1, 7, 2, 5], [2, 3, 5], [3, 4, 5]);
``````

You’re returning holder from compare() to sym() but nothing from sym().

You have to return the `compare(newArgs[0],newArgs[1]);` to get an output, since compare is called from within sym().

Try:

``````function sym(args) {
var newArgs = Array.prototype.slice.call(arguments);
return  compare(newArgs[0],newArgs[1]);
}
``````
1 Like

That works - thank you very much. So there is a fundamental concept that I’m not getting that is evident in my original code. When I call the compare function on an array by itself (and not within the sym function), the return statement yields the new array, but calling it in the same manner within sym yields undefined.

compare[array1,array2] will return the correct answer when called directly, but returns undefined when called inside another function.

``````function compare (a,b) {
var holder = [];
for (var i=0;i<a.length;i++) {
if (b.indexOf(a[i]) === -1) {
holder.push(a[i]);
}
}
for (var j=0;j<b.length;j++) {
if (a.indexOf(b[j]) === -1) {
holder.push(b[j]);
}
}
return holder; //returns the new array as expected
}
compare([1, 7, 2, 5], [2, 3, 5]);
``````