循环通过字符串,根据对象中的字符值返回最高得分词 - JavaScript

问题描述 投票:0回答:1

我想弄清楚如何在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] - 我怎么定义一个单词呢?

如果可以用我现有的逻辑解决这个Kata(使用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:在Test.describe._中未定义高。

javascript arrays string object increment
1个回答
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"));

2
投票

NaN的意思是“不是数字”。这通常表明你曾试图在某个时候对null或某事做算术。

在这个例子中,你的alphabetScore是一个字母的散列图 - 但你正在寻找数字键let value = alphabetScore[j];:这将返回undefinedundefined + 0 == NaN。相反,你需要说let value = alphabetScore[word[j]] - 获取单词的字母,而不是索引。

按照乔纳斯的建议,做两个新的变量,你的第一个let word = words[i] - 并摆脱它 - let highScore = 0; let highScoreWord = "";保持你找到的最高。也将let wordCount = 0;移动到循环内。现在,对于每个单词,您将获得该单词,并重置计数。

最后,在内循环之后,比如,if (wordCount > highScore) { highScore = wordCount; highScoreWord = word; }。所以,如果它高于你目前的最高值,请保存;否则就忽略它。

那么return highScoreWord,你应该是金色的!

© www.soinside.com 2019 - 2024. All rights reserved.