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