Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据字符串在字母表中的位置,使用JavaScript从字符串中查找得分最高的单词_Javascript_String_Split_Array.prototype.map - Fatal编程技术网

根据字符串在字母表中的位置,使用JavaScript从字符串中查找得分最高的单词

根据字符串在字母表中的位置,使用JavaScript从字符串中查找得分最高的单词,javascript,string,split,array.prototype.map,Javascript,String,Split,Array.prototype.map,我试图解决CodeWars挑战,但我有一个问题: “给定一串单词,你需要找到得分最高的单词。 单词的每个字母根据其在字母表中的位置得分: a = 1, b = 2, c = 3 etc. 您需要以字符串形式返回得分最高的单词。 如果两个单词得分相同,则返回原始字符串中出现最早的单词。 所有字母均为小写,所有输入均有效。” 我的代码通过了104个案例,但有1个案例出错。 测试用例的错误答案是 'what time are we climbing up the volcano' 根据code

我试图解决CodeWars挑战,但我有一个问题:

“给定一串单词,你需要找到得分最高的单词。 单词的每个字母根据其在字母表中的位置得分:

 a = 1, b = 2, c = 3 etc.
您需要以字符串形式返回得分最高的单词。 如果两个单词得分相同,则返回原始字符串中出现最早的单词。 所有字母均为小写,所有输入均有效。”

我的代码通过了104个案例,但有1个案例出错。 测试用例的错误答案是

'what time are we climbing up the volcano' 
根据codewars的说法,预计是“火山”,而不是“攀登”

有什么想法吗

问题的联系-


您可以使用reduce和object来跟踪最高计数和相应的单词

功能高(x){
让映射器=[…`abcdefghijklmnopqurstuvxyz`].reduce((op,inp,index)=>{
op[inp]=指数+1
返回操作
},{})
返回x.split(“”).reduce((op,inp)=>{
设currentCount=0;
[…inp].forEach(v=>{
currentCount+=映射器[v]
})
如果(currentCount>op.maxCount){
op.maxCount=当前计数
op.word=inp
}
返回操作
},{maxCount:0,单词:'})。单词
}

log(高(“我们什么时候爬上火山”),“火山”)
您可以使用reduce和object来跟踪最高计数和相应的单词

功能高(x){
让映射器=[…`abcdefghijklmnopqurstuvxyz`].reduce((op,inp,index)=>{
op[inp]=指数+1
返回操作
},{})
返回x.split(“”).reduce((op,inp)=>{
设currentCount=0;
[…inp].forEach(v=>{
currentCount+=映射器[v]
})
如果(currentCount>op.maxCount){
op.maxCount=当前计数
op.word=inp
}
返回操作
},{maxCount:0,单词:'})。单词
}
log(高(“我们什么时候爬上火山”),“火山”)
函数高(x){
const words=x.split(“”);
常量alphabetMap={};
对于(设i='a'.charCodeAt(),j=1;i{
常量字符=w.分割(“”);
const sumOfChars=chars.reduce((count,char)=>count+alphabetMap[char.charCodeAt()],0);
if(sumOfChars>最高的scoringword.score){
最高级的单词={word:w,score:sumOfChars};
}
});
返回最高的单词;
}
log(高(‘我们什么时候爬上火山’)//火山;)
功能高(x){
const words=x.split(“”);
常量alphabetMap={};
对于(设i='a'.charCodeAt(),j=1;i{
常量字符=w.分割(“”);
const sumOfChars=chars.reduce((count,char)=>count+alphabetMap[char.charCodeAt()],0);
if(sumOfChars>最高的scoringword.score){
最高级的单词={word:w,score:sumOfChars};
}
});
返回最高的单词;
}
log(高(‘我们什么时候爬上火山’)//火山;)

解决方案是使用字母表数组并为其中的字符位置编制索引

let al = `abcdefghijklmnopqrstuvwxyz`.split('')


   function high(x){
     let words = x.split(" ");
     let out = words.map(word => {
     let score = 0;
     let letters = word.split("");
     letters.map(c => {
      score += al.indexOf(c);
     })
     return [word, score];
    });

    out = out.sort((a,b) => { 
      if(a[1] > b[1]) return -1;
      else if(a[1] < b[1]) return 1; 
      else return 0;  });

return out[0][0];
}
let al=`abcdefghijklmnopqrstuvwxyz`.split(“”)
功能高(x){
让words=x.split(“”);
放出来=单词.map(单词=>{
分数=0;
字母=word.split(“”);
letters.map(c=>{
分数+=总指数(c);
})
返回[单词,分数];
});
out=out.sort((a,b)=>{
如果(a[1]>b[1])返回-1;
否则如果(a[1]
解决方案是使用字母表数组并为其中的字符位置编制索引

let al = `abcdefghijklmnopqrstuvwxyz`.split('')


   function high(x){
     let words = x.split(" ");
     let out = words.map(word => {
     let score = 0;
     let letters = word.split("");
     letters.map(c => {
      score += al.indexOf(c);
     })
     return [word, score];
    });

    out = out.sort((a,b) => { 
      if(a[1] > b[1]) return -1;
      else if(a[1] < b[1]) return 1; 
      else return 0;  });

return out[0][0];
}
let al=`abcdefghijklmnopqrstuvwxyz`.split(“”)
功能高(x){
让words=x.split(“”);
放出来=单词.map(单词=>{
分数=0;
字母=word.split(“”);
letters.map(c=>{
分数+=总指数(c);
})
返回[单词,分数];
});
out=out.sort((a,b)=>{
如果(a[1]>b[1])返回-1;
否则如果(a[1]
我被你的
计数器值+=splitChar.charCodeAt()弄糊涂了行。我不明白
splitChar.charCodeAt()
如何翻译成字母表中的1-26个字母。“单词的每个字母都根据其在字母表中的位置得分”

我可以通过做两件事使您的代码正常工作:

  • 添加地图以存储字母表中每个字母的值。我相信这可以通过多种方式实现,但这是我的方法:
  • 然后在
    counterValue+=splitChar.charCodeAt()中使用它作为<代码>计数器值+=字母值[字母]

  • 我被你的
    counterValue+=splitChar.charCodeAt()弄糊涂了行。我不明白
    splitChar.charCodeAt()
    如何翻译成字母表中的1-26个字母。“单词的每个字母都根据其在字母表中的位置得分”

    我可以通过做两件事使您的代码正常工作:

  • 添加地图以存储字母表中每个字母的值。我相信这可以通过多种方式实现,但这是我的方法:
  • 然后在
    counterValue+=splitChar.charCodeAt()中使用它作为<代码>计数器值+=字母值[字母]
  • 创意:

    • 将分数值存储在表格中以便于查找
    • 按非字母字符将句子拆分为单词
    • 通过首先计算单个字符的分数,然后将它们相加(使用reduce)得到每个单词的分数
    • 如果分数相同,不要忘记按原始位置排序,通过跟踪原始位置进行排序
    演示:

    const ScoreTable={
    “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,
    
    let al = `abcdefghijklmnopqrstuvwxyz`.split('')
    
    
       function high(x){
         let words = x.split(" ");
         let out = words.map(word => {
         let score = 0;
         let letters = word.split("");
         letters.map(c => {
          score += al.indexOf(c);
         })
         return [word, score];
        });
    
        out = out.sort((a,b) => { 
          if(a[1] > b[1]) return -1;
          else if(a[1] < b[1]) return 1; 
          else return 0;  });
    
    return out[0][0];
    }
    
    let letterValues = { 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 };
    
     function high(x){
      let result = '', value =0, counterValue = 0; 
    
      let splittedArray = x.split(' ');
    
      splittedArray.map(splitItem => {
        counterValue = 0;
    
        let splitItemArray = splitItem.split('');
    
        splitItemArray.map(splitChar => { 
          counterValue += splitChar.charCodeAt()-96; // if I subtract 96 then it will calculate a as 1, b as 2......
        })
    
        if(counterValue>value){
          result = splitItem;
          value = counterValue;
        }
      });
      return result;
    }