# Using .reduce with strings

Can someone explain to me why this algorithm returns 1 when passing ([“diorite”, “andesite”, “grass”, “dirt”, “pink wool”, “dead shrub”], [“diorite”, “andesite”, “grass”, “dirt”, “dead shrub”])? I want to return [“pink wool”].

When I pass two arrays of numbers I get the result I’m looking for:
diffArray([1, 2, 3, 4, 5], [1, 2, 3, 5]) // returns [4]

``````function diffArray(arr1, arr2) {
var firstCheck = arr1.reduce((result1, thing) =>
arr2.indexOf(thing) == -1? result1.push(thing) : result1, []);

return firstCheck;

}

diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]);

``````

Because you need to return a new array but the push method isn’t doing that. Combining the old array with the new entry using the spread operator would fix it:

``````function diffArray(arr1, arr2) {
var firstCheck = arr1.reduce((result1, thing) =>
arr2.indexOf(thing) == -1? [...result1,thing] : result1, []);
return firstCheck;
}

diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]);
//["pink wool"]
``````

The concat array method would also work here.

Also if you are trying to get the diff of two arrays, you are currently checking for elements that are present in arr1 but not arr2, but you will also need to check for elements that are present in arr2 but not in arr1

As @DouglasDev argued the issue is in the value returned in the callback function.

`result1.push(thing)` actually returns the length of the array. After the first iteration this value is equal to `1` and since `1.indexOf(thing) == -1` is always false, you end up passing the value all the way through.

Thank you… But why does `result1.push(thing)` return the length of the array?

I don’t think there’s a good reason why. JavaScript array methods are weird sometimes. Some return an array and some don’t. If you check MDN, it will tell you what each method returns.

Indeed, the only way I discovered it returns the length of the array is through the MDN docs. For reference here’s the syntax for the push method.