Intermediate Algorithm Scripting - Diff Two Arrays

Hello,

could u plz tell me why my code does not work? it just outputs an empty array.
function diffArray(arr1, arr2) {
let newArr = ;

function checkNum(num){
if (arr1.indexOf(num)===-1 | arr2.indexOf(num)===-1){
return newArr.push(num);
}
}
return newArr;

}

console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));

function diffArray(arr1, arr2) {
  let newArr = [];
  
  function checkNum(num){
    if (arr1.indexOf(num)===-1 | arr2.indexOf(num)===-1){
      newArr=  newArr.push(num);
    }
      }
return newArr;
  
}

console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Diff Two Arrays

Link to the challenge:

Can you explain what this is doing? This function is never called.

1 Like

Hi there!

It seems that you’re never calling inner function checkNum, therefore newArr is never mutated and output results in same empty array that you’ve created.

1 Like

Hi,

Please put the checkNum function outside the diffArray function. And for each number in arr1 or arr2, pass that number to the checkNum function by calling it in diffArr function. Something like this…
checkNum(arr1[i]); or
checkNum(arr2[j]);

1 Like

There in no problem with declaring a function inside of another function in Javascript.

1 Like

Hi,
Thank you. honestly, I seem to have a real problem in calling functions. I happen to repeat the same error each time. Can you explain how I can call the function, so it works?
Thanks

You can review here:

You can call or invoke this function by using its name followed by parentheses, like this: functionName();

I did, still now working: shows empty array:
function diffArray(arr1, arr2) {
let newArr = ;

function checkNum(num){
if (arr1.indexOf(num)===-1 | arr2.indexOf(num)===-1){
newArr.push(num);

}
}
checkNum();

return newArr.filter(checkNum);
}

console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));

I am not sure how you picture that function operating? What is num?

well, num is supposed to be a number checked by the function checkNum, to see if it does not exist in arr1 or arr2, if one of the two conditions is true, then it pushes num to newArr.
if there is any problem, please let me know.

I’m trying to let you know by getting yu talking about the logic here.

Honestly, I’d remove this inner function idea.

What numbers do you want to check to see if they aren’t in the first array or the second array? Right now you aren’t checking any specific values of num.

well you mean that num would be never related to any number inside both arrays?if so, why the other solution works well:
function diffArray(arr1, arr2) {
let newArr = ;
newArr= arr1.concat(arr2);
function checkNum(num){
if (arr1.indexOf(num)===-1 | arr2.indexOf(num)===-1){
return num;

}
}
return newArr.filter(checkNum);
}

Don’t start by copying parts of a solution. That won’t help you practice solving problems in code.

I wouldn’t start with a function if you are not comfortable with array methods. Almost every challenge has a solution that uses for loops and if statements.

1 Like

I understand your points, I know how to use for and if, but if I always go with these, I would never learn functions which are basically my problem.
Thank you anyway for your time.

You start with if and for and then you can refactor to fancier techniques. You cannot use the fancier techniques if you don’t understand how to get to the solution with basic if and for.

Improving code after you get the checkmarks is not covered in the curriculum, but it is an important skill.

1 Like

I know there is no problem with declaring/defining a function inside a function in JS. But here you are confusing between function call and function definition. You are using the whole definition of the function in place of calling it.
If you want to declare checkNum within diffArr, then make sure you explicitly call checkNum by passing arguments to it. What you have is a whole function definition, which is correct, but you also need to call that function, or else it will never execute, and the result array will always return blank. So, one way is you may have to do something like this…

diffArr(){
/Your existing code here/
loop over the first array and get each num
checkNum(num); /*This is how you call the function, by calling its name and actually passing the parameters */
loop over the second array and get each num
checkNum(num);

2 Likes

Thank you so much for full explanation.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.