DEV Community

Cover image for A few JavaScript puzzlers

A few JavaScript puzzlers

Thomas C. Haflich on July 13, 2019

Cover photo by Kyle Glenn on Unsplash. Good morning! 🌥️ How's your weekend going? I've been sick lately, so haven't had a lot of energy...
Collapse
 
karataev profile image
Eugene Karataev

Good puzzles to think about.

  1. Couple minutes of thinking and still can't find a solution
  2. Somehow remember the right answer
  3. I think it's the easiest task if you remember how the prototypes work in JS.
Collapse
 
maxbrettwell profile image
MaxBrettwell • Edited

In the 2nd problem, am I correct that x and y are some type or value that is NaN?

Collapse
 
tchaflich profile image
Thomas C. Haflich

In the second problem:

typeof x; // number
typeof y; // number
isNaN(x); // false
isNaN(y); // false

I can tell you that at least one of x and y is "intuitively" a number.


This is something that ends up being quite peculiar to an algebraic intuition, so if you haven't seen it in the wild it may not be something you can logic out (unless you take quite a leap). It is, however, frequently referenced in lists of language "gotchas".

I don't think I've actually spotted it anywhere else but JS, though technically according to the IEEE 754 specification it should be applicable elsewhere. Possibly it's just that since JS lacks any additional numeric types it ends up being more common.