2012-12-13

JavaScript puzzle: equal, but not the same

The following puzzle has been posted on Reddit by davvblack: What are the values of x and y, given the following interaction?
    > x === y
    true
    > 1/x > 1/y
    true
Show answer

2 comments:

svcurcio said...

x=0, y=-0

AlK said...

That's nice!

I had two answers, the one with the 0s, and another playing with the valueOf:


x = y = { count: 1, valueOf: function () { return this.count++; }};
x === y; // obviously as there is no coercion
1/x > 1/y; // because valueOf is called once for each coercion, so 1/x is 1/1 and 1/y is 1/2

Web Analytics