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, 'A'
and '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
inefficient*.
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.
The newPointStructure
object will be created and tested during Task 4. Below are examples of what the new object storage will look like, in addition to testing the new object itself. You will not be able to test the newPointStructure
object until Task 4!
Examples of the new key storage:
a
is worth1
b
is worth3
c
is worth3
j
is worth8
In newPointStructure
, the letters themselves are keys, so a single search
will identify a point value.
Example of 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"]);
Console Output
Scrabble scoring values for
letter a: 1
letter j: 8
letter z: 10