Subject: I'll try to explain stuff
Author:
Posted on: 2016-07-14 13:51:00 UTC

1) Called it :). One thing I might recommend is using tabs to indent (despite the viewer-dependent length issue) since that's one key per level not {2,4,8} spaces.

2) Dictionaries. Also knows as maps, associative arrays, hashes, and maybe several other things. These are surprisingly useful things. They're sort of generalizations of arrays. Arrays let you quickly do Integer → stuff (especially if the integers are small). Dictionaries let you quickly do stuff → stuff (in stronger-typed languages, it's Key type → Value type). The usual way to make one is a hash table (look those up if you want to learn something).

To make an empty dictionary, use {}. Dictionaries with values in them are {key: value, key: value, ...} in JavaScript (some languages will use => instead). To look something up in a dictionary, use dict[key], basically like arrays.

JavaScript is also a bit finicky, because objects are (as far as I can tell) dictionaries. That is, instead of doing "".concat("foo", "bar"), you can do """concat".

This also creates the "feature" that in JavaScript dictionaries, all the keys are automatically converted to strings. (I'm aiming for, roughly, program correctness research, so I'm not a JS fan)

3) Ok, so I'm going to want to go through that.

- We start with the uppercase string txt

- Then, we .split("") it. split(sep) splits the string at places where sep occurs and returns a list of the parts, with the separators removed. In this case, this method creates an array of strings, where each string is one character from the original text

- Now, we come to a function that's well known in functional programming circles, that is array.map(function). Map conceptually replaces each element x of the array by function(x) (it actually creates a new array with those results, but yeah). In our case, the function takes each one-character string in the array, takes the char code of the 0th (and only) character, and then converts that to a string. So ["1", "2"].map(Number) gives [1, 2] and ["A", "B"].map(function (char) { return char.charCodeAt(0).toString(); }) gives ["65", "66"]

- The final .join("") flattens the array of strings into one long string, which gives you the 8-character digits ("AaBbCcDd", as you put it) you were constructing earlier

4) It's not particularly obvious that that's a thing you can do. Thank you Stack Overflow!

5) That is a JavaScript "feature" sigh

6) So, javascript (until last year's standard) is missing some form of set datatype. So, that function fakes one. Instead of having a set of things, you have a dictionary with the things as keys, but their value is always something meaningless and small, like true. Then, you have what you wanted out of a set data structure (fast membership testing, fast add, and fast remove) (ignoring pathological cases that shouldn't ever come up)

6b) So, several general guidelines for most C-like languages (check some variant of "$lang style guide" for specifics and minor cultural things.

- Types (and type-like things, such as constructors for types), start with a capital letter

- Functions and variables start lowercase.

- snakecase vs. camelCase vs.depends-on-what-you're-naming is a social thing per-project, and, in open source code, roughly per language. JavaScript uses camelCase everywhere.

- Constants like PI are ALL
CAPS

- Pick a style for brace placement and stick with it, stay away from the flamewars.

- In general, foo(1) and bar[1] but while (1)

- Extra line breaks to make things fit roughly in 80ish characters helps avoid really long lines, which cause(d) problems

7) Fun. My mother's also a chemist. She did/does sort of polymer-related industrial research, IIRC, but then she went over to the dark side (aka management)

I figure this means you've been around enough physicists (who've hung around with certain groups of mathematicians) to pick up the [1, N] method of numbering N things, instead of the CS people's [0, N). I'm still somewhat salty that a perfectly reasonable uniform floor numbering system made it so the basement of the CS building is floor 1, not floor 0 :).

8) You've run into another JavaScript "feature". If, when you create a variable, you do i = 0; instead of var i = 0;, i become a global variable, and isn't isolated to your functions. This also applies to your for loops. So, use for (var i = 0; i and your problem goes away.

9) Well, that's one good reason to be using functions, along with the whole code reuse thing. It sometimes takes people a while to pick up on this.

10) Are you feeling lazy? Tell us there's a 6 or so character erase code, like "000000", to allow dismissing a Pokémon. Then, just add that in. Saves you making a button.

Reply Return to messages