遍历单词字符串,Return 根据对象中的字符值得分最高的单词 - JavaScript

Loop Through String of Words, Return Word With Highest Score According To Character Value in Object - JavaScript

我想弄清楚如何在 CodeWars 上解决 this kata

函数 high 接收一个字符串和 returns 具有最高 "score" 的单词,根据该单词中出现的字母。这些字母根据它们在字母表中的位置获得分数。所以a = 1 point, b = 2 points, c = 3 points,依此类推

我认为创建一个为字母表中的所有字母都赋值的对象是有意义的:

如果单词中的字母出现在 alphabetScore 中,该单词将接收其 "points" 并继续到单词中的下一个字母,增加单词的总分。

我有:

function high(string) {

  let words = string.split(" ");
  let wordScore = 0;

  const alphabetScore = {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
    e: 5,
    f: 6,
    g: 7,
    h: 8,
    i: 9,
    j: 10,
    k: 11,
    l: 12,
    m: 13,
    n: 14,
    o: 15,
    p: 16,
    q: 17,
    r: 18,
    s: 19,
    t: 20,
    u: 21,
    v: 22,
    w: 23,
    x: 24,
    y: 25,
    z: 26
  }

  let word = words[i];
  let wordCount = 0;

  //loop through all words in the string

  for (let i = 0; i < words.length; i++) {

    let word = words[i];

    //loop through all characters in each word

    for (let j = 0; j < word.length; j++) {

      let value = alphabetScore[j];

      wordCount += alphabetScore[value];

    }
  }
  return wordCount;
}

console.log(high("man i need a taxi up to ubud"));

这会返回一个错误

i is not defined

in let word = words[i] - 那么我还能如何定义一个词呢?

如果可以用我现有的逻辑(使用 for 循环)解决这个套路,请这样做。

编辑:将wordCount = alphabetScore.value++;更改为wordCount += alphabetScore[value];

编辑 2:现在返回 NaN

编辑 3:最新尝试:

function myScore(input) {
    let key = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
    "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
    "w", "x", "y", "z"
    ];
    let bestWord = "";
    let bestScore = 0;
    let words = input.split(" ");
    for (let i = 0; i < words.length; i++) {
      let score = 0;
      let word = words[i];
      for (let j = 0; j < word.length; j++) {
        let char = word[j];
        score += (key.indexOf(char) + 1);
      }
      if (score > bestScore) {
        bestScore = score;
        bestWord = word;
      }
    }
    return bestWord;
  }

ReferenceError: high is not defined at Test.describe._

NaN 表示 "not a number." 这通常表示您曾尝试对 null 或某事进行算术运算。

在这种情况下,您的 alphabetScore 是字母的哈希图 - 但您正在寻找 数字 let value = alphabetScore[j];:这将 return undefinedundefined + 0 == NaN。相反,您需要说 let value = alphabetScore[word[j]] - 获取单词的 字母 ,而不是索引。

按照 Jonas 的建议,在您的第一个 let word = words[i] 所在的位置创建两个新变量 - 并去掉那个 - let highScore = 0; let highScoreWord = ""; 以保存您找到的最高变量。将 let wordCount = 0; 也移到循环内。现在,对于每个单词,您都会得到单词,并重置计数。

最后,在内循环之后,比如说,if (wordCount > highScore) { highScore = wordCount; highScoreWord = word; }。因此,如果它高于您当前的最高价,请保存它;否则就忽略它。

然后return highScoreWord你应该是金色的!

运行 codewars

成功

let key = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
  "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  "w", "x", "y", "z"
];

function wordScore(word) {
  let score = 0;
  for (let j = 0; j < word.length; j++) {
    let char = word[j];
    score += (key.indexOf(char) + 1);
  }
  return score;
}

function high(x) {
  let bestWord = "";
  let bestScore = 0;
  words = x.split(" ");
  for (let i = 0; i < words.length; i++) {
    let word = words[i];
    let score = wordScore(word);
    if (score > bestScore) {
      bestScore = score;
      bestWord = word;
    }
  }
  return bestWord;
}

console.log(high("man i need a taxi up to ubud"));