forked from LaunchCodeEducation/Scrabble-Scorer-Autograded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrabble-scorer.js
175 lines (134 loc) · 4.39 KB
/
scrabble-scorer.js
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
// 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");
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 = "";
let totalScore = 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`;
totalScore += Number(pointValue);
}
}
}
return totalScore;
//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 userWord = input.question(`Let's play some scrabble!
Enter a word to score: `);
let regex = /^[a-zA-Z ]+$/;
while (!regex.test(userWord)){
userWord = input.question('Numbers and special characters not allowed. Please enter a word: ')
}
return userWord;
};
let newPointStructure = transform(oldPointStructure);
newPointStructure[" "] = 0;
let simpleScorer = function(word){
word = word.toUpperCase();
let letterPoints = "";
let totalScore = 0;
for (let i = 0; i < word.length; i++) {
totalScore += 1;
}
return totalScore;
};
let vowelBonusScorer = function(word){
word = word.toUpperCase();
let letterPoints = "";
let totalScore = 0;
for (let i = 0; i < word.length; i++) {
if (word[i] === 'A' || word[i] === 'E' || word[i] === 'I' || word[i] === 'O' || word[i] === 'U'){
letterPoints += `Points for '${word[i]}': 3 \n`;
totalScore += 3;
}
else{
totalScore += 1;
}
}
return totalScore;
};
let scrabbleScorer = function(word){
word = word.toUpperCase();
let totalScore = 0;
for (let i = 0; i < word.length; i++) {
for (const letter in newPointStructure){
if(word[i] === letter.toUpperCase()){
totalScore += newPointStructure[letter];
}
}
}
return totalScore;
};
const objectSimpleScore = {
name: "Simple Score",
description: "Each letter is worth 1 point.",
scorerFunction: simpleScorer
};
const objectBonusVowel = {
name: "Bonus Vowels",
description: "Vowels are 3 pts, consonants are 1 pt.",
scorerFunction: vowelBonusScorer
};
const objectOldScorer = {
name: "Scrabble",
description: "The traditional scoring algorithm.",
scorerFunction: oldScrabbleScorer
};
const objectNewScorer = {
name: "Scrabble",
description: "The traditional scoring algorithm but better.",
scorerFunction: scrabbleScorer
};
const scoringAlgorithms = [objectSimpleScore, objectBonusVowel, objectNewScorer];
function scorerPrompt() {
console.log(`Which scoring algorithm would you like to use?: \n`)
for (i=0; i<3; i++){
console.log(`${i} - ${scoringAlgorithms[i]["name"]} : ${scoringAlgorithms[i]["description"]} `);
}
let chosenScorer = Number(input.question(`Enter 0, 1 or 2: `));
while(!(chosenScorer === 0 || chosenScorer === 1 || chosenScorer === 2)){
chosenScorer = Number(input.question(`Please enter either 0, 1 or 2: `));
}
return scoringAlgorithms[chosenScorer];
}
function transform(oldPointStructure) {
let newPointObject = {};
for(const item in oldPointStructure){
for (let i = 0; i < oldPointStructure[item].length ; i++){
newPointObject[oldPointStructure[item][i].toLowerCase()] = Number(item);
}
}
return newPointObject;
};
function runProgram() {
let userWord = initialPrompt();
console.log(`Score for '${userWord}': ${scorerPrompt().scorerFunction(userWord)}`);
}
// 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
};