Task 4: Transform the Object
Write the rest of the
transform()function. It will need to take an object as a parameter - specifically theoldPointStructureobject. Callingtransform(oldPointStructure)will return an object with lowercase letters as keys. The value for each key will be the points assigned to that letter.TipRecall that
for...inloops 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 Mathchapter.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 usevariableName[index].1 2 3 4 5 6console.log("Letters with score '4':", oldPointStructure[4]); console.log("3rd letter within the key '4' array:", oldPointStructure[4][2]); let letters = oldPointStructure[8]; console.log("Letters with score '8':", letters); console.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
newPointStructureobject in the starter code and set it equal totransform(oldPointStructure).WarningHard-coding the
newPointStructureobject 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 thescrabbleScorer()function and then replace theoldScrabbleScorer()function inscoringAlgorithmswith this new function.TipoldScrabbleScorer()usesoldPointStructureand returns a score for each letter in a word. You’ll want to writescrabbleScorer()to usenewPointStructureand return a cumulative score for the whole word entered.