DEV Community

Bukunmi Odugbesan
Bukunmi Odugbesan

Posted on

Coding Challenge Practice - Question 67

The task is to implement the function Object.is()

The boilerplate code

function is(a, b) {
  // your code here
}
Enter fullscreen mode Exit fullscreen mode

Object.is() works the same way except for 2 special cases. +0 and -0 are the same, but Object.is() says they are different. Also, NaN === NaN is false, but Object.is() says they are the same.

With that in mind, if +0 and -0 look equal, check if they have the same sign. It is checked by dividing 1 by both

  if (a === b) {
    return a !== 0 || 1 / a === 1 / b;
  }

Enter fullscreen mode Exit fullscreen mode

For NaN, check if the value is equal to itself

 return a !== a && b !== b;
Enter fullscreen mode Exit fullscreen mode

The final code

function is(a, b) {
  // your code here
  if(a === b) {
    return a !== 0 || 1 / a === 1 / b;
  }
  return a !== a && b !== b;
}
Enter fullscreen mode Exit fullscreen mode

That's all folks!

Top comments (0)