denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
34.95k stars 2.55k forks source link

Bug in "Math with true and false" #121

Closed sollyucko closed 3 years ago

sollyucko commented 5 years ago

How does the code given in "Math with true and false" work? For me, in Chrome, I get Uncaught TypeError: true is not a function at <anonymous>:2:7. Was there a typo or am I confused?

true +
  true(
    // -> 2
    true + true
  ) *
    (true + true) -
  true; // -> 3

Same thing in "Patching numbers":

Number.prototype.isOne = function() {
  return Number(this) === 1;
};

(1.0).isOne(); // -> true
(1).isOne(); // -> true
(2.0)
  .isOne()(
    // -> false
    7
  )
  .isOne(); // -> false
Mego commented 5 years ago

Looks like bad formatting. It should be:

true + true; // -> 2
(true + true) * (true + true) - true; // -> 3
Number.prototype.isOne = function() {
  return Number(this) === 1;
};

(1.0).isOne(); // -> true
(1).isOne(); // -> true
(2.0).isOne(); // -> false
(7).isOne(); // -> false
denysdovhan commented 3 years ago

This is bad formatting because of Prettier.