Task 4: Transform the Object
Write the rest of the
transform()
function. It will need to take an object as a parameter - specifically theoldPointStructure
object. 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...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 usevariableName[index]
.1 2 3 4 5 6
console.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
newPointStructure
object in the starter code and set it equal totransform(oldPointStructure)
.WarningHard-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 thescrabbleScorer()
function and then replace theoldScrabbleScorer()
function inscoringAlgorithms
with this new function.TipoldScrabbleScorer()
usesoldPointStructure
and returns a score for each letter in a word. You’ll want to writescrabbleScorer()
to usenewPointStructure
and return a cumulative score for the whole word entered.