forked from LaunchCodeEducation/Scrabble-Scorer-Autograded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrabble-scorer.js
195 lines (171 loc) · 5.88 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// 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']
};
let playerInput = "";
let finalWord = "";
// let playersChoice = 0;
//use function to score word provided by the user
function oldScrabbleScorer(word) {
let letterPoints = 0;
for (let i = 0; i < word.length; i++) {
letter = word[i];
for (const pointValue in oldPointStructure) {
if (oldPointStructure[pointValue].includes(word[i])) {
letterPoints += `Points for '${word[i]}': ${pointValue}`
console.log(`Points for '${word[i]}': ${pointValue}\n`);
}
}
}
return letterPoints;
}
// console.log(oldScrabbleScorer(word));
// 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. //
//return a numerical score
//each letter within word = 1pt
function simpleScorer(word) {
let scorePoints = 0;
for (i = 0; i < word.length; i++) {
scorePoints++;
finalWord += word[i];
}
console.log(`Your score for ${finalWord} is ${scorePoints}pts.`);
return scorePoints
}
function vowelBonusScorer(word) {
let bonusScorePoints = 0;
const vowels = ['A', 'E', 'I', 'O', 'U'];
let vowelPoints = 3;
for (let x = 0; x < word.length; x++) {
let gotVowelBonus = false;
for (let j = 0; j < vowels.length; j++) {
if (word[x].toUpperCase() === vowels[j]) {
bonusScorePoints += vowelPoints;
gotVowelBonus = true;
}
}
if (!gotVowelBonus){
bonusScorePoints++;
}
}
console.log(`Your score for ${finalWord} is ${bonusScorePoints}pts.`);
return bonusScorePoints;
}
// console.log("vowelBonusScorer");
// console.log(vowelBonusScorer(word));
function initialPrompt() {
console.log("Let's play some scrabble!");
playerInput = input.question("Enter a word to score: ");
let playersChoice = input.question(`Choose your scoring option:
0 - ${simpleScorer.name}: ${simpleScorer.description}
1 - ${vowelBonusScorer.name}: ${vowelBonusScorer.description}
2 - ${scrabbleScorer.name}: ${scrabbleScorer.description}
Enter 0, 1, or 2: `);
return [playerInput, playersChoice]
}
let simpleScorerOne = {
name: 'Simple Score',
description: 'One point per character',
scorerFunction: simpleScorer
};
let vowelBonusScorerOne = {
name: 'Bonus Vowels',
description: 'Vowels are 3 pts',
scorerFunction: vowelBonusScorer
}
let scrabbleScorerOne = {
name: 'Scrabble',
description: 'Uses scrabble point system',
scorerFunction: scrabbleScorer
};
let returnObject = [];
const scoringAlgorithms = [simpleScorerOne, vowelBonusScorerOne, scrabbleScorerOne];
function scorerPrompt(_playerInput, playersChoice) {
let splitPlayerInput = _playerInput.toUpperCase().split('');
console.log("player input: " + _playerInput + " player's choice: " + playersChoice);
if (playersChoice == 0) {
console.log(`Score for '${_playerInput}': ${simpleScorerOne.scorerFunction(splitPlayerInput)}`);
}
if (playersChoice == 1) {
console.log(`Score for '${_playerInput}': ${vowelBonusScorerOne.scorerFunction(splitPlayerInput)}`);
}
if (playersChoice == 2) {
console.log(`Score for '${_playerInput}': ${scrabbleScorerOne.scorerFunction(splitPlayerInput)}`);
}
}
let newPointStructure = transform(oldPointStructure);
// let simpleScorer;
// let vowelBonusScorer;j
// let scrabbleScorer;
function scrabbleScorer(word){
let wordScore = 0;
for (let i = 0; i < word.length; i++) {
let letter = word[i];
letter = letter.toLowerCase();
// console.log(letter);
// console.log(newPointStructure[letter]);
wordScore += Number(newPointStructure[letter]);
}
return wordScore;
}
//bracket notation to access the point value for the key
//for in loop with two index
//first index is the letter, second is the point value
//for loop to iterate through for points, another inside to iterate through the letters
//set new key/value pair for each letter and point
//iterate through the array
//methods to loop through the index, loop only once
//
function transform(oldPointStructure) {
let pointStructure2 = {};
for (const pointValue in oldPointStructure) {
let letters = oldPointStructure[pointValue];
for(const index in letters){
let key = letters[index].toLowerCase();
// console.log(`${pointValue}`);
// console.log(`${letters[index]}`);
pointStructure2[`${key}`] = Number(pointValue);
}
}
return pointStructure2;
}
function runProgram() {
let derp = [];
derp = initialPrompt();
// // console.log(typeof(derp));
// // for (let i = 0; i < derp.length; i++){
// // console.log(derp[i]);
// // }
// oldScrabbleScorer(playerInput);
// simpleScorer(playerInput);
// vowelBonusScorer(playerInput);
scorerPrompt(derp[0], derp[1]);
// for(const keys in newPointStructure){
// console.log(typeof keys);
// console.log(keys +"-" + newPointStructure[keys]);
// }
// scrabbleScorer("Word");
// console.log(scrabbleScorer("Blabb"));
}
// 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
};