Lines 1-14: Line 1 loads the
"Words" database specifically the column
Line 2 declares a new variable and assigns it an empty array value. The name suggests it will hold a subset of the
Lines 6-8 create an event handler in case the length changes. The function
filter changes the words being displayed for us. Lines 10-12 are another event handler for when the leading letter changes. It also calls
Going back to line 4 we are calling
filter to display an initial subset of words. By default, those are 1 letter long words that begin with a.
Lines 15-34: This crazy
filter function takes two parameters
letter. I am not a big fan of abbreviations, but most don’t care.
Lines 16 and 17 guards against too many clicks too fast. They show the international symbol for “wait you fool” and cover the buttons so you can’t click them.
Line 18 re-initializes the variable
filteredWordList to an empty array value. That variable doesn’t need to be initialized in line 2 because we do it here as well.
Line 19 erases any previously displayed words. Clearly, this is written to allow the length and first letter to change on the fly.
Lines 21-25 do the actual filtering based on the length and the first letter.
filteredWordList now has a subset of
wordList just as we suspected it would.
Lines 27-29 test if there are any words that meet our criteria at all. If not we create a special output value.
Lines 31 and 32 make our program interactive again.
Line 33 takes the
filteredWordList and shows those words (or our nothing here message) on the screen.
filter function actually returns
undefined because there is no return statement. It does have a side effect, which is to show a subset of words that meet the criteria set forth by the parameters.