js中字符串最大的单词总和

时间:2018-11-24 19:04:46

标签: javascript string character

我想用javascript解决此问题: 给定一串单词,您需要找到得分最高的单词。

每个单词的字母根据其在字母表中的位置得分:a = 1,b = 2,c = 3等。

您需要将得分最高的单词作为字符串返回。

如果两个单词得分相同,则返回最早出现在原始字符串中的单词。

所有字母均为小写且所有输入均有效。

我的代码如下:

    function high(x){
  x = x.split(" ");
  var bestWord = 0;
  var bestWordS = "";
  for (var i = 0; i < x.length; i++){
    var word = x[i].split("");
    var letter;
    var wordScore;
      switch(word[i]){
        case "a":
          letter = 1;
          break;
        case "b":
          letter = 2;
          break;
        case "c":
          letter = 3;
          break;
        case "d":
          letter = 4;
          break;
        case "e":
          letter = 5;
          break;
        case "f":
          letter = 6;
          break;
        case "g":
          letter = 7;
          break;
        case "h":
          letter = 8;
          break;
        case "i":
          letter = 9;
          break;
        case "j":
          letter = 10;
          break;
        case "k":
          letter = 11;
          break;
        case "l":
          letter = 12;
          break;
        case "m":
          letter = 13;
          break;
        case "n":
          letter = 14;
          break;
        case "o":
          letter = 15;
          break;
        case "p":
          letter = 16;
          break;
        case "q":
          letter = 17;
          break;
        case "r":
          letter = 18;
          break;
        case "s":
          letter = 19;
          break;
        case "t":
          letter = 20;
          break;
        case "u":
          letter = 21;
          break;
        case "v":
          letter = 22;
          break;
        case "w":
          letter = 23;
          break;
        case "x":
          letter = 24;
          break;
        case "y":
          letter = 25;
          break;
        case "z":
          letter = 26;
          break;     
      }
      wordScore += letter;
      if ( wordScore > bestWord){
        bestWord = wordScore;
        bestWordS = x[i];
      }
    }
    return bestWordS;

}

我不知道这是什么错误,该函数总是返回一个空字符串(“”)

2 个答案:

答案 0 :(得分:0)

假设每个单词的“分数”与您的示例相同,则可以简单地: 编辑:通过@KoshVery

修复了排序功能的问题

 const scoresByLetter = "abcdefghijklmnopqrstuvwxyz".split('');
    const sentence = "azzzzim zoom";
    const words = sentence.split(" ");
    
    //Declearing a function to calculate score for a single word
    const calculateWordWeight = (word)=>{
        return Array.prototype.reduce.apply(word, [(score, letter) =>{
            return score + scoresByLetter.indexOf(letter);
        },0]);
    };
    
    //Calculating score for each word
    const scores = words.map(calculateWordWeight);
    
    //Finding the highest score value
    let highestScore = Math.max(scores);

    //Then finding the highest value index
    let highestScoreIndex = scores.indexOf(highestScore);

    //Finally, finding the word by the index
    let wordWithHighestScore = words[highestScoreIndex];
    
    alert(wordWithHighestScore);

答案 1 :(得分:0)

您可以将所有字母存储在字符串中,并使用indexOf获得字母得分。
另外,您可以使用一个reduce来获得单词得分,而使用另一个reduce来找到得分最高的单词:

const abc = 'abcdefghijklmnopqrstuvwxyz';

const high = (x) => x.split(/\s+/).reduce((a, w) => (w = [w, [...w].reduce((a, l) => a += 1 + abc.indexOf(l), 0)]) && ~~a[1] > w[1] ? a : w, [])[0]

console.log(high('lorem ipsum dolor'));

更新,适用于那些难以阅读oneliners的人,
使用两个嵌套的for循环的易读版本:

const abc = 'abcdefghijklmnopqrstuvwxyz';

const high = (x) => {
  let words = x.split(/\s+/);
  let best, maxscore = 0;
  for (let word of words) {
    let score = 0;
    for (let letter of word) {
      score += 1 + abc.indexOf(letter);
    }
    if (score > maxscore) {
      best = word;
      maxscore = score;
    }
  }
  return best;
}

console.log(high('lorem ipsum dolor'));