Task 3: Transform Scrabble Scoring
Currently, the software contains the data structure below for the traditional
Scrabble scoring algorithm. Take a few moments to review how the
oldPointStructure object relates a point value to a letter.
The keys of
oldPointStructure are the Scrabble points, and the
values are arrays of letters. All letters in the array have the Scrabble
point value equal to the key. For example,
'R' are worth 1,
'K' is worth 5, and
'J' is worth 8.
To find the point value for a letter with the old format, the program must
iterate over each key in
oldPointStructure and then check if the letter is
inside the array paired with that key. This search within a search is
Think about this for a second. The scoring action takes in letters in a word as input and outputs numerical point values.
We can improve our program by rewriting the data structure to better fit the action we want to take. Keep this idea in mind as you go on to code your own applications.
It would improve the performance of the program to create a
newPointStructure object that has 26 keys,
one for each letter. The value of each key will be the Scrabble point value.
Examples of the new key storage:
newPointStructure, the letters themselves are keys, so a single search
will identify a point value.
newPointStructure object usage.
console.log("Scrabble scoring values for"); console.log("letter a: ", newPointStructure.a); console.log("letter j: ", newPointStructure.j); console.log("letter z: ", newPointStructure["z"]);
Scrabble scoring values for letter a: 1 letter j: 8 letter z: 10