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]);