gpt4 book ai didi

javascript - 计算 Scrabble-Java 脚本的分数

转载 作者:搜寻专家 更新时间:2023-11-01 05:03:10 25 4
gpt4 key购买 nike

我正在尝试计算单词的拼字游戏分数。但每次我输入一个单词时,它都会返回每个字母关联的数字序列。例如,当我为我的拼字游戏输入 if 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())

当拼字游戏分数打印出来时,我试图得到 418 的总数,这等于 13 分。

最佳答案

分数应该是数字。这是表示此操作的简单方法。

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);

关于javascript - 计算 Scrabble-Java 脚本的分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58241255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com