For your second assignment, we'll ask you to modify and improve our Scrabble Scorer program. Did you think you were going to work on Mars Rover code already? That will come next!
We want you to update our program that asks a user for a word and outputs a score. Your final version will have three scoring algorithms and allow a user to interactively choose which algorithm to use. We've provided some starter code that includes the official Scrabble scoring point system and we'd like you to make some modifications to improve it.
Let's roll.
Note
The requirements below are what your assignment should look like when it's time to submit. Rome wasn't built in a day and neither was Scrabble.
This assignment is broken down so you can complete small pieces as you go. You need to move sequentially starting with Part A below. You'll have a much more enjoyable time writing this program if you read this entire page before even opening repl.it.
initialPrompt()
function that asks a user to input a word.oldScrabbleScorer()
.scoringAlgorithms
array.transform()
function that takes in the oldPointStructure
object and returns a newPointStructure
object.runProgram()
function to serve as the starting point for your
program.Fork this replit.
You only need to pay attention to one file here, scrabble-scorer.js
. Within this JavaScript
file is still some more starter code that you don't need to touch. We'll point out what you
should be modifying here to write your Scrabble Scorer program.
Hit the repl.it run button initially and you'll see a message printed to the console:
> node index
Let's play some Scrabble!
>
Tip
If you don't see this message printed and have exhausted your troubleshooting skills, reach out to your classmates or course staff ASAP so you can get started with the real coding.
initialPrompt()
function to prompt the user to enter a word to score.oldScrabbleScorer()
function provided to score the word provided by the user. Print the result to the console.Before you move on, be sure you're on the right track. At this point, your program should have an output like this:
> node index
Let's play some Scrabble!
Enter a word to score: pineapple
Points for 'P': 3
Points for 'I': 1
Points for 'N': 1
Points for 'E': 1
Points for 'A': 1
Points for 'P': 3
Points for 'P': 3
Points for 'L': 1
Points for 'E': 1
>
Your job here is to write two other scoring algorithms for the Scrabble player.
simpleScore
: Define a function that takes a word as a parameter and
returns a numerical score. Each letter within the word is worth 1 point.vowelBonusScore
: Define a function that takes a word as a parameter and
returns a score. Each vowel within the word is worth 3 points, and each
consonant is worth 1 point.Note
Make each scoring algorithm case insensitive, meaning that they should all ignore case when assigning points.
Once you've written these scoring functions, organize all three of the scoring options into an array.
Your program will use the scoringAlgorithms
array to retrieve information about the
three scoring algorithms and convey that information to the user.
Finish writing the scoringAlgorithms
array. It should be populated with three objects, one for each of the three scoring options.
Each object should contain three keys: name
, description
, and scoringFunction
.
Examine the table for the information to store in name
and
description
. The scoringFunction
for each object should be the name of
one of the three scoring algorithms already defined.
Name | Description | Score Function |
---|---|---|
Simple Score | Each letter is worth 1 point. | A function with a parameter for user input that returns a score. |
Bonus Vowels | Vowels are 3 pts, consonants are 1 pt. | A function that returns a score based on the number of vowels and consonants. |
Scrabble | The traditional scoring algorithm. | Uses the oldScrabbleScorer() function to determine the score for a given
word. |
Finish writing scorerPrompt()
so that the user can select which scoring algorithm to use when the program scores their word.
Use the selected algorithm to determine the score for the word:
0
, have the program output a score using the simple scorer.1
, use the vowel bonus scoring function.2
, use the Scrabble scoring option.scorerPrompt()
should return the object the user has selected.
Tips
Your scoringAlgorithms
structure now holds all of the scoring information required for the program.
To access a scoring object and its properties, use a combination of bracket notation and dot notation.
Examples
// Simple scoring
console.log("algorithm name: ", scoringAlgorithms[0].name);
console.log("scoringFunction result: ", scoringAlgorithms[0].scoringFunction("JavaScript"));
Console Output
algorithm name: Simple Score
scoringFunction result: 10
Call scorerPrompt()
inside of runProgram()
so that the program asks the user for a scoring algorithm after prompting for a word.
Use the scoring object returned from scorerPrompt()
to score the user's word and let the user know what score their word receives.
Before moving forward, your running program should behave roughly like this:
> node index
Let's play some Scrabble!
Enter a word to score: coconut
Which scoring algorithm would you like to use?
0 - Simple: One point per character
1 - Vowel Bonus: Vowels are worth 3 points
2 - Scrabble: Uses scrabble point system
Enter 0, 1, or 2: 0
Score for 'coconut': 7
>
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.
1const oldPointStructure = {
2 1: ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
3 2: ['D', 'G'],
4 3: ['B', 'C', 'M', 'P'],
5 4: ['F', 'H', 'V', 'W', 'Y'],
6 5: ['K'],
7 8: ['J', 'X'],
8 10: ['Q', 'Z']
9};
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
inside the array paired with that key. This search within a search is
inefficient.
Tip
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:
a
is worth 1
b
is worth 3
c
is worth 3
j
is worth 8
In newPointStructure
, the letters themselves are keys, so a single search
will identify a point value.
Example
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
Write the rest of the transform()
function. It will need to take an object
as a parameter - specifically the oldPointStructure
object. Calling
transform(oldPointStructure)
will return an object with lowercase
letters as keys. The value for each key will be the points assigned to that
letter.
Tips
Recall that for...in
loops iterate over the keys within an object.
If you need a reminder of how to assign new key/value pairs, review the
relevant section in the
Objects and Math
chapter.
To access the letter arrays within oldPointStructure
, use bracket
notation (oldPointStructure['key']
).
To access a particular element within a letter array, add a second set of
brackets (oldPointStructure['key'][index]
), or assign the array to a
variable and use variableName[index]
.
Examples
1console.log("Letters with score '4':", oldPointStructure['4']);
2console.log("3rd letter within the key '4' array:", oldPointStructure['4'][2]);
3
4let letters = oldPointStructure['8'];
5console.log("Letters with score '8':", letters);
6console.log("2nd letter within the key '8' array:", letters[1]);
Console Output
Letters with score '4': [ 'F', 'H', 'V', 'W', 'Y' ]
3rd letter within the key '4' array: V
Letters with score '8': [ 'J', 'X' ]
2nd letter within the key '8' array: X
Locate the newPointStructure
object in the starter code and set it equal to
transform(oldPointStructure)
.
Warning
Hard-coding the newPointStructure
object literal like this:
let newPointStructure =
{
a:1,
b: 1,
c: 1,
etc ...
}
won't pass. And you'll lose an opportunity to practice this skill.
Once you've defined newPointStructure
, use it to finish writing the scrabbleScore()
function and then replace
the oldScrabbleScorer()
function in scoringAlgorithms
with this new function.
Tip
oldScrabbleScorer()
uses oldPointStructure
and returns a score for each letter in a word. You'll want to write
scrabbleScore()
to use newPointStructure
and return a cumulative score for the whole word entered.
Here are some words you can use to test your code:
JavaScript
= 24 points using Scrabble, 10 using Simple Score, and 16
using Bonus Vowels.Scrabble
= 14 points using Scrabble, 8 using Simple Score, and 12 using
Bonus Vowels.Zox
= 19 points using Scrabble, 3 using Simple Score, and 5 using Bonus
Vowels.> node index
Let's play some Scrabble!
Enter a word to score: rum
Which scoring algorithm would you like to use?
0 - Simple: One point per character
1 - Vowel Bonus: Vowels are worth 3 points
2 - Scrabble: Uses scrabble point system
Enter 0, 1, or 2: 2
Score for 'rum': 5
>
' '
to the
newPointStructure
object. The point value for a blank tile is 0
.