30-seconds-of-code: I think it will change the allEqual definition in docs

Created on 22 May 2019  路  6Comments  路  Source: 30-seconds/30-seconds-of-code

As I can see in the README, the allEqual definition is like that:

  const allEqual = arr => arr.every(val => val === arr[0]);

But when I test allEqual([NaN, NaN]), it will return false,

I think it need change the definition like that:

 const allEqual = arr => arr.every(val => Object.is(val, arr[0]));

when I test allEqual([NaN, NaN]), it will return true as I expect.

I just a poor learner at js, so please point out my mistake if I was wrong.

bug good first issue

All 6 comments

That's quite an interesting edge-case, actually, due to the fact that NaN is not equal to itself. Object.is() seems like a solution, but we should check if it has any other side-effects before changing the allEqual method.

In any case, this is a very interesting observation and we should do something about it - either update the code or add a comment about the edge-case.

Ok, that's your attention

If we do update the code it would be considered a breaking change because NaN is meant to be not equal to itself. So if we do go to the more "stable" Object.is() then we will need to note it

@skatcat31 We should add a comment either way. I do agree that NaN's behavior is essentially by-design in this snippet. Maybe we just add a note about it and go our merry way?

Adding a comment either way is a good step, and I think keeping it the way it is now and noting it would be better.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

emelendez picture emelendez  路  4Comments

skatcat31 picture skatcat31  路  5Comments

Bruce-Feldman picture Bruce-Feldman  路  4Comments

ecwyne picture ecwyne  路  4Comments

fejes713 picture fejes713  路  4Comments