Javascript Drinking Game and the Scientific Method

Christian Clausen
3 min readMar 17, 2020

--

Spoiler warning: this post contains a minor spoiler of the Goal.

I am a big fan of Java Puzzlers. I love quizzes and games like that. At university, I would regularly host a Java quiz. In the quiz, I would present a few lines of Java code, and then the audience would have to choose from 5 options what the output would be.

Following Atwood’s law: “Everything that can be written in Javascript, will eventually be”. For this reason, I think it is important to learn about some of the pitfalls and corner cases in Javascript. Therefore I have invented a game for learning some of them in Javascript.

The game consists of a lot of tiny code snippets, grouped into small sections. Many of these sections are structured to encourage the player to discover an underlying rule. The way you play this game is much like the Scientific Method:

  • Make an observation
  • Form/refine a hypothesis
  • Test the hypothesis
  • Repeat

Learning to work like this is very useful, in many different areas. It gives us the ability to uncover the underlying structure of something, without being able to observe it directly. This is also a point in the Goal.

This is the same with these games, you have a language like Javascript, with some fixed rules (the interpreter), of course, in this case, we could look up the source code of the interpreter, however, we don’t want to do that. Even if we did the behavior might stem from a complex interaction in the code. Thus we cannot observe the rules directly.

The way we play the game is:

  • You see a code snippet (make an observation)
  • Try to guess what it does (form a hypothesis)
  • Run the code (test the hypothesis)

If your prediction was inaccurate you refine your hypothesis to include the new data, and at this point move onto the next snippet. As mentioned the snippets are grouped into sections that encourage the formation of good hypotheses.

The rules are simple:

  • Open the developer console (f12).
  • Type in the expression
  • Try to predict what the result will be
  • Hit enter
  • If you were wrong you drink (some appropriate amount)

So without further ado here are the snippets:

> "1" + 1> "1" - 1> "1" + - 1--------------> let x = 3> "5" + x - x> "5" - x + x--------------> parseInt("")> isNaN("")> typeof NaN--------------> throw "ball"--------------> []+{}> {}+[]> []+{} === {}+[]--------------> null > 0> null < 0> null == 0> null >= 0> null <= 0> NaN < NaN> NaN >= NaN--------------> function dis() { return this }> five = dis.call(5)> five.wtf = "potato"> five.wtf> five * 5> five.wtf> five++> five.wtf> five.wtf = "potato?"> five.wtf> five--------------> "1000" == "1e3"--------------> new Date("12/08/2016")> new Date("2016-12-08")--------------> 0.1 + 0.2--------------> +[]> {} + []> {}{}{}{}{} + []> [] + []> {}{}{}{}[] + []> [] + {}> {}{}{}{}[] + {}> +{}> {} + {}> {}{}{}{}{} + {}--------------> []?true:false> {}?true:false> ({}?true:false)--------------> [1001, 101, 1, 2].sort()> [1, 2, 101, 1001].sort()> [6, -2, 2, -7].sort()--------------> [1,2,3] == [1,2,3]--------------> let t = [0]> t == t> t == !t--------------> Math.max() > Math.min()> Math.max() < Math.min()--------------> true + true === 2> true - true === 0> true === 1--------------> [] == false> [] ? true : false--------------> let numbers = [1, 2, 3]> numbers.map(n => { value: n })--------------> Ninja = function (name) {this.name = name;}> Ninja.prototype.jump = function () {console.log(this.name + " jumped");}> let john = new Ninja("John");> john.jump()> setTimeout(john.jump, 1000)> setTimeout(() => john.jump(), 1000)> let callback = john.jump> setTimeout(() => callback(), 1000)> callback()--------------> "" == false> false == "0"> "" == "0"--------------> [[][[]]+[]][+[]][++[+[]][+[]]]--------------> ('b' + 'a' + + 'a' + 'a').toLowerCase()--------------> console.log(b)> console.log(b); var b;--------------> var n = 42; n+1 > var name = 42; name+1> let name = 42; name+1

Have fun with Javascript, and always drink responsibly.

--

--

Christian Clausen

I live by my mentor’s words: “The key to being consistently brilliant is: hard work, every day.”