forked from LaunchCodeEducation/Scrabble-Scorer-Autograded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrabble-scorer.js
152 lines (128 loc) · 3.92 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
// 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 newPointStructure = transform(oldPointStructure);
function oldScrabbleScorer(word) {
word = word.toUpperCase();
let letterPoints = "";
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() {
console.log("Let's play some scrabble!");
return input.question("Enter a word to score: ");
}
let simpleScorer = function (word) {
score = 0;
for (let i = 0; i < word.length; i++) {
score += 1;
}
return score;
};
let vowelBonusScorer = function (word) {
word = word.toUpperCase();
const bonusVowelPointStructure = {
3: ["A", "E", "I", "O", "U"],
};
let score = 0;
for (let i = 0; i < word.length; i++) {
if (bonusVowelPointStructure["3"].includes(word[i])) {
score += 3;
} else {
score += 1;
}
}
return score;
};
let scrabbleScorer = function (word) {
word = word.toLowerCase();
let score = 0;
for (let i = 0; i < word.length; i++) {
let letterPoints = newPointStructure[word[i]];
score += letterPoints;
}
return score;
};
const simpleScoreObject = {
name: "Simple Score",
description: "Each letter is worth 1 point.",
scorerFunction: simpleScorer,
};
const bonusVowelObject = {
name: "Bonus Vowels",
description: "Vowels are 3 pts, consonants are 1 pt.",
scorerFunction: vowelBonusScorer,
};
const scrabbleScorerObject = {
name: "Scrabble",
description: "The traditional scoring algorithm.",
scorerFunction: oldScrabbleScorer,
};
const newScrabbleScorerObject = {
name: "New Scrabble",
description: "The new scoring algorithm.",
scorerFunction: scrabbleScorer,
};
const scoringAlgorithms = [
simpleScoreObject,
bonusVowelObject,
newScrabbleScorerObject,
];
function scorerPrompt(word) {
console.log("Which scoring algorithm would you like to use?");
console.log("0 - Simple: One point per character");
console.log("1 - Vowel Bonus: Vowels are worth 3 points");
console.log("2 - New Scrabble: Uses new scrabble point system");
let userScorerSelection = input.question("Enter 0, 1, or 2: ");
console.log("algorithm name: ", scoringAlgorithms[userScorerSelection].name);
console.log(
"scoringFunction result: ",
scoringAlgorithms[userScorerSelection].scorerFunction(word)
);
}
function transform(oldPointStructure) {
let newObject = {};
for (const property in oldPointStructure) {
for (let i = 0; i < oldPointStructure[property].length; i++) {
newObject[oldPointStructure[property][i].toLowerCase()] =
Number(property);
}
}
return newObject;
}
function runProgram() {
let word = initialPrompt();
scorerPrompt(word);
console.log(newPointStructure);
}
// 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,
};