I'm trying to check for array equality using forEach in JavaScript. This criteria is key, and the answer is not on Stack Overflow already.
The tests that are checking equality and expecting true are failing. I have a feeling I'm running into an issue to do with scope. Can someone please walk me through it?
function eql(arr1, arr2) {
arr1.forEach((el) => {
if (arr1[el] === arr2[el]) {
return true;
}
})
return false
}
Here are the test results I want to change:
eql([], [])
Expected: true but got: false
eql(['a', 'b'], ['a', 'b'])
Expected: true but got: false
forEachdoes not return a result from an inner return.return truewithin the callback doesn't do anything. It is returning *from the callback` not fromeql. You either need to convert to using a conventional loop, change it so the result is outside the callback and only changed within, or change to.some()/.every()and return the result of that.elis not the index, it's each array item.return trueon the first match (if it would work, see comments above) doesn't tell you if all elements are equal.everyand most array methods skip holes. So, it will return true for[,,3]and[1,2,3]