forked from LaunchCodeEducation/Scrabble-Scorer-Autograded
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrabble-scorer.js
More file actions
179 lines (153 loc) · 5 KB
/
scrabble-scorer.js
File metadata and controls
179 lines (153 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// This assignment is inspired by a problem on Exercism (https://exercism.org/tracks/javascript/exercises/etl) that demonstrates Extract-Transform-Load using Scrabble's scoring system.
const input = require("readline-sync");
let word = '';
let scoreChoice = '';
const oldPointStructure = {
1: ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
2: ['D', 'G'],
3: ['B', 'C', 'M', 'P'],
4: ['F', 'H', 'V', 'W', 'Y'],
5: ['K'],
8: ['J', 'X'],
10: ['Q', 'Z']
};
// function oldScrabbleScorer(word) {
// word = word.toUpperCase();
// let letterPoints = 0;
// for (let i = 0; i < word.length; i++) {
// for (const pointValue in oldPointStructure) {
// if (oldPointStructure[pointValue].includes(word[i])) {
// letterPoints += `Points for '${word[i]}': ${pointValue}\n`
// }
// }
// }
// return letterPoints;
// }
// your job is to finish writing these functions and variables that we've named //
// don't change the names or your program won't work as expected. //
function initialPrompt() {
let userInput = input.question("Let's play some scrabble! Enter a word:");
word = word + userInput
word.toUpperCase();
};
//let newPointStructure = transform(oldPointStructure);
let simpleScorer = function (word) {
word = word.toUpperCase();
let simplePoints = 0
for (let index = 0; index < word.length; index++) {
simplePoints += 1;
}
return simplePoints;
};
let vowelBonusScorer = function (word) {
word = word.toUpperCase();
let vowels = ['A', 'E', 'I', 'O', 'U'];
let vowelPoints = 0;
for (let index = 0; index < word.length; index++) {
if (vowels.includes(word[index])) {
vowelPoints += 3
} else {
vowelPoints += 1
}
}
return vowelPoints;
};
let scrabbleScorer = function (word) {
letterPoints = 0;
for (let i = 0; i < word.length; i++) {
for (const item in newPointStructure) {
letterPoints += item;
}
}
return letterPoints;
}
const scoringAlgorithms = [{
'name': 'Simple Score',
'description': 'Each letter is worth 1 point.',
scorerFunction: function simpleScorer(word) {
word = word.toUpperCase();
let simplePoints = 0
for (let index = 0; index < word.length; index++) {
simplePoints += 1;
}
return simplePoints;
}
},
{
'name': 'Bonus Vowels',
'description': 'Vowels are 3 pts, consonants are 1 pt.',
scorerFunction: function vowelBonusScorer(word) {
word = word.toUpperCase();
let vowels = ['A', 'E', 'I', 'O', 'U'];
let vowelPoints = 0;
for (let index = 0; index < word.length; index++) {
if (vowels.includes(word[index])) {
vowelPoints += 3
} else {
vowelPoints += 1
}
}
return vowelPoints;
}
},
{
'name': 'Scrabble',
'description': 'The traditional scoring algorithm.',
scorerFunction: function scrabbleScorer(word) {
letterPoints = 0;
for (let i = 0; i < word.length; i++) {
letterPoints += newPointStructure[word[i].toLowerCase()];
}
return letterPoints;
}
}];
function scorerPrompt() {
console.log(`0 - ${scoringAlgorithms[0].name}: ${scoringAlgorithms[0].description}\n1 - ${scoringAlgorithms[1].name}: ${scoringAlgorithms[1].description}\n2 - ${scoringAlgorithms[2].name}: ${scoringAlgorithms[2].description}`);
let userChoice = input.question("Which scoring algorithm would you like to use?");
scoreChoice = scoreChoice + userChoice;
if (scoreChoice == 0) {
console.log(`Entered 0, 1, or 2: ${scoreChoice}`);
console.log("Algorithm name:", scoringAlgorithms[0].name);
console.log("Scoring result:", scoringAlgorithms[0].scorerFunction(word));
} else if (scoreChoice == 1) {
console.log(`Entered 0, 1, or 2: ${scoreChoice}`);
console.log("Algorithm name:", scoringAlgorithms[1].name);
console.log("Scoring result:", scoringAlgorithms[1].scorerFunction(word));
} else if (scoreChoice == 2) {
console.log(`Entered 0, 1, or 2: ${scoreChoice}`);
console.log("Algorithm name:", scoringAlgorithms[2].name);
console.log("Scoring result:", scoringAlgorithms[2].scorerFunction(word));
} else {
console.log("Sorry, invalid input please try again.");
return userChoice;
}
};
let newPointStructure = transform(oldPointStructure);
function transform(oldPointStructure) {
let newScoreObj = {};
for (let key in oldPointStructure) { //Keep in mind key = value in the newPointStructure
const letters = oldPointStructure[key]
for (let i = 0; i < letters.length; i++) {
newScoreObj[letters[i].toLowerCase()] = Number(key);
}
}
return newScoreObj;
};
function runProgram() {
initialPrompt();
scorerPrompt();
}
// Don't write any code below this line //
// And don't change these or your program will not run as expected //
module.exports = {
initialPrompt: initialPrompt,
transform: transform,
oldPointStructure: oldPointStructure,
simpleScorer: simpleScorer,
vowelBonusScorer: vowelBonusScorer,
scrabbleScorer: scrabbleScorer,
scoringAlgorithms: scoringAlgorithms,
newPointStructure: newPointStructure,
runProgram: runProgram,
scorerPrompt: scorerPrompt
};