admin管理员组文章数量:1389853
I'm trying to calculate the Scrabble score of a word. But each time I type in a word-it returns the sequence of numbers each letter is associated with. For instance, when I type if fox for my scrabble word I get 418. Why is this happening?
const oldScoreKey = {
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 transform(oldScoreKey){
const newScoreKey = {};
for (const [letterValue, letterArr] of Object.entries(oldScoreKey)) {
for (const letter of letterArr) {
newScoreKey[letter.toLowerCase()] = letterValue;
}
}
return newScoreKey;
}
console.log(transform(oldScoreKey));
// Code your initialPrompt function here:
const input = require('readline-sync');
console.log("Using algorithm: Scrabble");
let word = (input.question("Enter a simple word please: "));
const alphabet = "abcdefghijklmnopqrstuvwxyz";
function simpleScore() {
let score = 0
for(let letter of word.toLowerCase()){
if (alphabet.includes(letter))
score += 1;
}
return score;
}
console.log(simpleScore())
let letter = (input.question("Enter a scrabble word please: "));
letter = letter.toLowerCase();
let newAlphabet = { a: '1',
e: '1',
i: '1',
o: '1',
u: '1',
l: '1',
n: '1',
r: '1',
s: '1',
t: '1',
d: '2',
g: '2',
b: '3',
c: '3',
m: '3',
p: '3',
f: '4',
h: '4',
v: '4',
w: '4',
y: '4',
k: '5',
j: '8',
x: '8',
q: '10',
z: '10' }
function scrabbleScore() {
let sum = 0
let i = 0
let score = 0
for (i = 0; i < word.length; i++) {
letter = word[i];
sum += newAlphabet[letter];
}
return (sum*1);
}
console.log(scrabbleScore())
When the Scrabble Score prints I am trying to get the total number of 418, which would equal 13 points.
I'm trying to calculate the Scrabble score of a word. But each time I type in a word-it returns the sequence of numbers each letter is associated with. For instance, when I type if fox for my scrabble word I get 418. Why is this happening?
const oldScoreKey = {
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 transform(oldScoreKey){
const newScoreKey = {};
for (const [letterValue, letterArr] of Object.entries(oldScoreKey)) {
for (const letter of letterArr) {
newScoreKey[letter.toLowerCase()] = letterValue;
}
}
return newScoreKey;
}
console.log(transform(oldScoreKey));
// Code your initialPrompt function here:
const input = require('readline-sync');
console.log("Using algorithm: Scrabble");
let word = (input.question("Enter a simple word please: "));
const alphabet = "abcdefghijklmnopqrstuvwxyz";
function simpleScore() {
let score = 0
for(let letter of word.toLowerCase()){
if (alphabet.includes(letter))
score += 1;
}
return score;
}
console.log(simpleScore())
let letter = (input.question("Enter a scrabble word please: "));
letter = letter.toLowerCase();
let newAlphabet = { a: '1',
e: '1',
i: '1',
o: '1',
u: '1',
l: '1',
n: '1',
r: '1',
s: '1',
t: '1',
d: '2',
g: '2',
b: '3',
c: '3',
m: '3',
p: '3',
f: '4',
h: '4',
v: '4',
w: '4',
y: '4',
k: '5',
j: '8',
x: '8',
q: '10',
z: '10' }
function scrabbleScore() {
let sum = 0
let i = 0
let score = 0
for (i = 0; i < word.length; i++) {
letter = word[i];
sum += newAlphabet[letter];
}
return (sum*1);
}
console.log(scrabbleScore())
When the Scrabble Score prints I am trying to get the total number of 418, which would equal 13 points.
Share Improve this question asked Oct 4, 2019 at 18:02 codingTiacodingTia 311 silver badge4 bronze badges 2- 2 You are defining the scores as strings, not numbers. – Niet the Dark Absol Commented Oct 4, 2019 at 18:05
- Change the values in newAlphabet to be numbers instead of strings: {a:1,e:1,... – jalynn2 Commented Oct 4, 2019 at 18:09
5 Answers
Reset to default 3The scores should be numbers. Here is an easy way to represent this operation.
const newAlphabet = {
a: 1,
e: 1,
i: 1,
o: 1,
u: 1,
l: 1,
n: 1,
r: 1,
s: 1,
t: 1,
d: 2,
g: 2,
b: 3,
c: 3,
m: 3,
p: 3,
f: 4,
h: 4,
v: 4,
w: 4,
y: 4,
k: 5,
j: 8,
x: 8,
q: 10,
z: 10,
};
const scrabbleScore = word =>
word
.split('')
.map(letter => newAlphabet[letter])
.reduce((a, b) => a + b);
You could take an object with numbers and a default value of zero, if the character does not exist in the object.
function scrabbleScore(word) {
let newAlphabet = { a: 1, e: 1, i: 1, o: 1, u: 1, l: 1, n: 1, r: 1, s: 1, t: 1, d: 2, g: 2, b: 3, c: 3, m: 3, p: 3, f: 4, h: 4, v: 4, w: 4, y: 4, k: 5, j: 8, x: 8, q: 10, z: 10 },
sum = 0,
i;
word = word.toLowerCase();
for (i = 0; i < word.length; i++) {
sum += newAlphabet[word[i]] || 0; // for unknown characters
}
return sum;
}
let letter = prompt("Enter a scrabble word please: ");
console.log(scrabbleScore(letter))
In JS +=
is ok for strings and it not parse value to int but just concatenate.
Try to force it manually:
sum += parseInt(newAlphabet[letter]);
Because it's not convert to integer. I will put code from your code.
function scrabbleScore() {
let sum = 0;
let i = 0;
let score = 0;
for (i = 0; i < word.length; i++) {
letter = word[i];
sum += parseInt(newAlphabet[letter]);
}
return (sum*1);
}
console.log(scrabbleScore());
This will help you.
Here is how I would break this down.
This version includes a cleaner version of the code to build the new score key. (It depends upon flatMap
which isn't yet universal, but which is easy to shim, and which could be replaced easily enough with a reduce
if desired.)
const invertScoreKey = (old) =>
Object.assign (...Object .entries (old) .flatMap (
([k, arr]) => arr .map ((v) => ({[v .toLowerCase ()]: Number (k)}))
))
const sum = (ns) => ns .reduce((total, n) => total + n, 0)
const scoreWord = (scores) => (word) =>
sum ([...word .toLowerCase()] .map(c => scores[c] || 0))
const oldScoreKey = {
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']
}
const scrabbleScore = scoreWord (invertScoreKey (oldScoreKey))
'The quick, brown fox jumped over the lazy dog'.split(/\W+/)
.forEach(word => console.log(`${word} : ${scrabbleScore(word)}`)
)
Note that scoreWord
is generic, accepting a score key and returning a function which takes a word and returns the total score by that key. scrabbleScore
is based on this, using the result of inverting the original key as its key.
本文标签: javascriptCalculating Score for ScrabbleJava ScriptStack Overflow
版权声明:本文标题:javascript - Calculating Score for Scrabble-Java Script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744739515a2622525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论