Blog Post, TDK's Week 3: Warp Drive On a Learner's Permit - tdkehoe/blog GitHub Wiki

This week we started learning JavaScript. Wednesday morning we learned about associative arrays, a.k.a. hash tables. In almost every programming language a hash table is called a phonebook. A few programming languages call it a dictionary. In JavaScript it's called an object.

Here's a JavaScript object:

var hashes = {
  C: null,
  C#: 'Dictionary',
  C++: 'std:map',
  Cobra: 'dic',
  Go: 'phone_book',
  Erlang: 'keylist',
  Java: 'phonebook',
  JavaScript: 'object',
  Objective-C: 'adictionary',
  OCaml: 'Hashtbl',
  OptimJ: 'phoneBook',
  PHP: 'phonebook',
  Ruby: 'phonebook'
}

Wednesday afternoon we learned about dispatch tables, in which a function or method is stored in an hash table. Our assignment was to create a program in which you can type

paragraph('Hello, World!')

and the program returns

'<p>Hello, World!</p>'

Plus seven other HTML elements. This required creating a hash table with eight keys (a, b, p, div, span, etc.) and the values are functions to generate the HTML code around the provided string.

We worked late into the night to complete this homework. What I couldn't figure out, and Jenny had to help me with, was how the parameter or argument ('Hello, World!') gets passed to the function. I had to draw this picture to understand it:

Thursday Jeff Dean taught us another variant of the dispatch table. The problem was to create a circular game in which fire is stronger than grass, grass is stronger than water, water is stronger than fire, and water is stronger than electric. I suggested a three-column object with two keys for each value:

That wouldn't work because JavaScript doesn't have three-column objects. Even if it did, keys must be unique, so my approach wouldn't work.

Jeff instead taught us how to put a hash table in the value of another hash table. I.e., recursive or nested objects. You then access the value by calling two keys, e.g.,

fireWaterGrassElectric [fire] [grass]

This returns the strength advantage of fire over grass, which is 2.

I felt like we'd just gotten out learner's permits, and now we were being shown how to use the warp drive!

⚠️ **GitHub.com Fallback** ⚠️