我正在尝试计算单词的拼字游戏得分。但是,每次我输入一个单词时,它都会返回每个字母与之关联的数字序列。例如,当我输入fox作为我拼字游戏的单词时,我得到418。为什么会这样?
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())
打印Scrabble分数时,我试图获得总分418,这等于13分。
答案 0 :(得分:4)
分数应为数字。这是表示此操作的简单方法。
[
{
"genre_id": "",
"name": "",
"description": "",
"slug": "",
"url": "",
"videos": [
{
"videos_id": "",
"title": "",
"description": "",
"slug": "",
"release": "",
"is_tvseries": "",
"runtime": "",
"video_quality": "",
"thumbnail_url": "",
"poster_url": ""
},
{
"videos_id": "",
"title": "",
"description": "",
"slug": "",
"release": "",
"is_tvseries": "",
"runtime": " ",
"video_quality": "",
"thumbnail_url": "",
"poster_url": ""
}
]
},
{
"genre_id": "",
"name": "",
"description": " ",
"slug": "",
"url": "",
"videos": [
{
"videos_id": "",
"title": " ",
"description": "",
"slug": "",
"release": "",
"is_tvseries": "",
"runtime": "",
"video_quality": "",
"thumbnail_url": "",
"poster_url": ""
},
{
"videos_id": "",
"title": "",
"description": "",
"slug": "",
"release": "",
"is_tvseries": "",
"runtime": "",
"video_quality": "",
"thumbnail_url": "",
"poster_url": ""
}
]
}
]
答案 1 :(得分:3)
因为它不会转换为整数。 我将从您的代码中提取代码。
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());
这将为您提供帮助。
答案 2 :(得分:2)
如果对象中不存在字符,则可以使用数字和默认值为零的对象。
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))
答案 3 :(得分:0)
在JS中,+=
对于字符串来说是可以的,它不将值解析为int而是仅进行连接。
尝试手动强制:
sum += parseInt(newAlphabet[letter]);
答案 4 :(得分:0)
这是我要分解的方式。
此版本包括用于创建新得分密钥的代码的较干净版本。 (取决于flatMap
尚不通用,但易于填充,并且可以根据需要用reduce
轻松替换。)
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)}`)
)
请注意,scoreWord
是通用的,接受一个分数键并返回一个函数,该函数接受一个单词并通过该键返回总分数。 scrabbleScore
以此为基础,使用了将原始密钥反转为密钥的结果。