@@ -33,11 +33,11 @@ function oldScrabbleScorer(word) {
33
33
34
34
function initialPrompt ( ) {
35
35
let info = input . question ( "Let's play some scrabble! \n\nEnter a word to score: " ) ;
36
- // console.log(oldScrabbleScorer(info));
37
36
return info ;
38
37
} ;
39
38
40
- let newPointStructure = { } ; //TODO: make this an object of 26 letter keys and the values consistent with the points from the oldPointStructure
39
+ let newPointStructure = transform ( oldPointStructure ) ;
40
+ console . log ( newPointStructure ) ;
41
41
42
42
let simpleScorer = function ( word ) {
43
43
word = word . toUpperCase ( ) ;
@@ -66,7 +66,18 @@ let vowelBonusScorer = function (word) {
66
66
return letterPoints ;
67
67
} ;
68
68
69
- let scrabbleScorer ;
69
+ let scrabbleScorer = function ( word ) {
70
+ // word = word.toUpperCase();
71
+ let letterPoints = 0 ;
72
+
73
+ for ( let i = 0 ; i < word . length ; i ++ ) {
74
+ let character = word [ i ] ;
75
+ if ( newPointStructure [ character ] ) {
76
+ letterPoints += newPointStructure [ character ] ;
77
+ }
78
+ }
79
+ return letterPoints ;
80
+ } ;
70
81
71
82
const scoringAlgorithms = [
72
83
{
@@ -82,7 +93,7 @@ const scoringAlgorithms = [
82
93
{
83
94
"name" : "Scrabble" ,
84
95
"description" : "Uses scabble point system." ,
85
- "scorerFunction" : oldScrabbleScorer
96
+ "scorerFunction" : scrabbleScorer
86
97
}
87
98
] ;
88
99
@@ -99,30 +110,31 @@ function transform(oldPointStructureObject) {
99
110
let newPointStructureObject = { } ;
100
111
// loop through object
101
112
102
- // for (let item in oldPointStructureObject) {
103
- // // data variables need for new object
104
- // let currentObject = oldPointStructureObject[item];
105
- // let newKey = currentObject.type.toLowerCase();
106
- // let newValue = currentObject.item.toLowerCase();
107
-
108
- // add these to new object key:value pair
109
- newPointStructureObject [ newKey ] = newValue ;
113
+ for ( let key in oldPointStructureObject ) {
114
+ let letters = oldPointStructureObject [ key ] ;
110
115
116
+ // loop through each letter in the array
117
+ for ( let i = 0 ; i < letters . length ; i ++ ) {
118
+ let letter = letters [ i ] . toLowerCase ( ) ;
119
+ newPointStructureObject [ letter ] = Number ( key ) ;
120
+ }
111
121
}
112
122
return newPointStructureObject ;
113
123
} ;
114
- console . log ( "Letters with score '4':" , oldPointStructure [ 4 ] ) ;
115
- console . log ( "3rd letter within the key '4' array:" , oldPointStructure [ 4 ] [ 2 ] ) ;
116
124
117
- let letters = oldPointStructure [ 8 ] ;
118
- console . log ( "Letters with score '8':" , letters ) ;
119
- console . log ( "2nd letter within the key '8' array:" , letters [ 1 ] ) ;
125
+
126
+ // console.log("Letters with score '4':", oldPointStructure[4]);
127
+ // console.log("3rd letter within the key '4' array:", oldPointStructure[4][2]);
128
+
129
+ // let letters = oldPointStructure[8];
130
+ // console.log("Letters with score '8':", letters);
131
+ // console.log("2nd letter within the key '8' array:", letters[1]);
120
132
121
133
function runProgram ( ) {
122
134
let wordInput = initialPrompt ( ) ;
123
135
let scorerFunction = scorerPrompt ( ) ;
124
136
let score = scorerFunction ( wordInput ) ;
125
- // let transformOldToNew = transform(oldPointStructure);
137
+ let transformOldToNew = transform ( oldPointStructure ) ;
126
138
console . log ( `Score for '${ wordInput } ': ${ score } ` ) ;
127
139
128
140
}
0 commit comments