# Arrow function return undefined results

I am trying to return the filtered result with the arrow function but it says no defined, Confused why is that? It says “diff” not defined why is that???

function diffArray(arr1, arr2) {
return [
…diff(arr1, arr2),
…diff(arr2, arr1)
]

const diff = (a, b)=>{
return a.filter(item => b.indexOf(item) === -1);
}
}

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

===> “diff is not defined”

You define `diff` after the `return` statement. You need to define things before using them.
Also, anything after a `return` statement is like it doesn’t exist, as a `return` stops the function.

1 Like

Thanks but if I use regular function it returns the answer

function diffArray(arr1, arr2) {
return [
…diff(arr1, arr2),
…diff(arr2, arr1)
]

function diff(a, b) {
return a.filter(item => b.indexOf(item) === -1);
}
}

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

it has to do with the way you used `...`.

i dont think you can use 2 of them inside the same array like that

1 Like

Thanks but if I use regular function it returns the answer

function diffArray(arr1, arr2) {
return [
…diff(arr1, arr2),
…diff(arr2, arr1)
]

function diff(a, b) {
return a.filter(item => b.indexOf(item) === -1);
}
}

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

somewhere inside `[…diff(arr1, arr2), …diff(arr2, arr1)]` is where youre problem is though

still working on narrowing it down. 1 sec

Arrow functions are not the same thing as functions invoked with `function` keyword with different syntax, they are just different things
If you want to use arrow function you need to define them before using them.

@alkapwn3d : don’t worry about keeping searching, it is one of the differences between arrow functions and usual functions

this still doesnt fix the issue

``````function diffArray(arr1, arr2) {
return […diff(arr1, arr2), …diff(arr2, arr1)]

function diff(a, b){// changed to es5 function
return a.filter(item => b.indexOf(item) === -1);
}
}
``````

your `diff` function is returning empty array for me. console.log it to make sure its working properly

1 Like

You have copied the code of the OP, which was not correctly formatted, so you now have a “three dots” character, instead of the three dots of the spread operator.

1 Like

Define your arrow function before you use it, and you are fine to go

1 Like

so yeah you cant use `...` the way youre using it. it has to be in its own array

edit: nvm i was wrong. you can use it that way

also in your diff function `=== -1` returns an empty array, but `!== - 1` returns the items that match.

i guess you just cant use spread on an empty array

i dont know if this is the best way to solve the question but it doesnt return `diff not defined` anymore

``````function diffArray(arr1, arr2) {
//return [[...diff(arr1, arr2)], [...diff(arr2, arr1)]]
return [...diff(arr1, arr2), ...diff(arr2, arr1)] // this works
}

// diff- returns arr of a items in b
const diff = (a, b) => {
let result = a.filter(item => b.indexOf(item) !== -1);
return result;
}

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

i see what you mean about the … character

The challenge want you to returns an array with the items that don’t match (@maulikpkanani please always add a link to the challenge, better use the “Ask for help” button inside the challenge)
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/diff-two-arrays

In that way you are defining `diff` before using it, so it’s fine - the function in which it is used is called below

1 Like