【问题标题】:Loop Through String of Words, Return Word With Highest Score According To Character Value in Object - JavaScript遍历单词串,根据对象中的字符值返回得分最高的单词 - JavaScript
【发布时间】:2019-08-26 19:20:07
【问题描述】:

我正试图弄清楚如何解决 CodeWars 上的this kata。

函数high 接收一个字符串并根据单词中出现的字母返回具有最高“分数”的单词。字母根据它们在字母表中的位置获得分数。所以a = 1 point, b = 2 points, c = 3 points,等等。

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

如果单词中的字母出现在alphabetScore 中,则该单词将获得其“分数”并继续到单词中的下一个字母,从而增加单词的总分数。

我有:

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

这是返回一个错误说

我没有定义

在let word = words[i] - 那我还怎么定义一个词呢?

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

编辑:将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 未定义 在 Test.describe._

【问题讨论】:

  • 为什么不保留分数最高的单词?
  • 好吧 - 不过,我不知道如何隔离得分最高的单词。
  • 你需要把你的let word = words[i];inside你的for循环里面i定义的地方。
  • @StephenP 在第一个 for 循环中定义 word,在第二个 for 循环中定义 value。该函数现在返回NaN。
  • 先把if (score &gt; bestScore) { bestScore = score; word = bestWord; }改成if (score &gt; bestScore) { bestScore = score; bestWord = word; }

标签: javascript arrays string object increment


【解决方案1】:

NaN 表示“不是数字”。这通常表明您曾尝试在某个时候对 null 或某事进行算术运算。

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

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

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

然后return highScoreWord 你应该是金子!

【讨论】:

    【解决方案2】:

    在代码战上成功运行

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

    【讨论】:

    • 在控制台中工作,但在 Codewars 上不工作,出于某种原因
    • 是的!那么你改变了什么?您已经声明了参数word 和x?您还介意在重构之前发布解决方案吗?
    • 将 let bestWord; 更改为 let bestWord = ""; 以便在输入为空字符串时不返回未定义
    • 那么,如果您查看我最近的编辑(上面的“最新尝试”) - 与您刚刚所做的相比,该代码不正确怎么办?我认为主要区别在于您为每个单词创建了一个辅助函数?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2020-03-04
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 2020-07-12
    相关资源
    最近更新 更多