Skip to content

Commit 1bd575d

Browse files
committed
passed all npm tests and returns scabble score
1 parent 8b322d5 commit 1bd575d

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

scrabble-scorer.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ function oldScrabbleScorer(word) {
3333

3434
function initialPrompt() {
3535
let info = input.question("Let's play some scrabble! \n\nEnter a word to score: ");
36-
// console.log(oldScrabbleScorer(info));
3736
return info;
3837
};
3938

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);
4141

4242
let simpleScorer = function (word) {
4343
word = word.toUpperCase();
@@ -66,7 +66,18 @@ let vowelBonusScorer = function (word) {
6666
return letterPoints;
6767
};
6868

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+
};
7081

7182
const scoringAlgorithms = [
7283
{
@@ -82,7 +93,7 @@ const scoringAlgorithms = [
8293
{
8394
"name": "Scrabble",
8495
"description": "Uses scabble point system.",
85-
"scorerFunction": oldScrabbleScorer
96+
"scorerFunction": scrabbleScorer
8697
}
8798
];
8899

@@ -99,30 +110,31 @@ function transform(oldPointStructureObject) {
99110
let newPointStructureObject = {};
100111
// loop through object
101112

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];
110115

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+
}
111121
}
112122
return newPointStructureObject;
113123
};
114-
console.log("Letters with score '4':", oldPointStructure[4]);
115-
console.log("3rd letter within the key '4' array:", oldPointStructure[4][2]);
116124

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]);
120132

121133
function runProgram() {
122134
let wordInput = initialPrompt();
123135
let scorerFunction = scorerPrompt();
124136
let score = scorerFunction(wordInput);
125-
// let transformOldToNew = transform(oldPointStructure);
137+
let transformOldToNew = transform(oldPointStructure);
126138
console.log(`Score for '${wordInput}': ${score}`);
127139

128140
}

0 commit comments

Comments
 (0)