The task is to implement the function Object.is()
The boilerplate code
function is(a, b) {
// your code here
}
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;
}
For NaN, check if the value is equal to itself
return a !== a && b !== b;
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;
}
That's all folks!
Top comments (0)