Coderbyte上的字母计数I JavaScript挑战

Coderbyte上的字母计数I JavaScript挑战,javascript,regex,function,loops,object,Javascript,Regex,Function,Loops,Object,我已经解决这个问题好几个小时了,我已经尽了我目前的新手javaScript的最大能力来解决这个问题,但我就是不知道到底出了什么问题。我一直在这里看到“意外令牌非法: 和“TypeError:无法读取null的属性'length': 我想问题是howManyRepeat变量。我不明白为什么我得到它不能读取null的长度,而显然word是str中的一个单词 我的想法是: word.toLowerCase().split("").sort().join("").match(/([.])\1+/g).l

我已经解决这个问题好几个小时了,我已经尽了我目前的新手javaScript的最大能力来解决这个问题,但我就是不知道到底出了什么问题。我一直在这里看到“意外令牌非法:

和“TypeError:无法读取null的属性'length':

我想问题是howManyRepeat变量。我不明白为什么我得到它不能读取null的长度,而显然word是str中的一个单词

我的想法是:

word.toLowerCase().split("").sort().join("").match(/([.])\1+/g).length
……这里:

挑战:
使用JavaScript语言,让函数LetterCountI(str)接受str 正在传递的参数,并返回具有最大 重复的信件。例如:“今天是有史以来最伟大的一天!“你应该回来 最伟大的是因为它有两个e(和两个t),而且它是前所未有的 有2个e。如果没有重复字母的单词,返回-1。单词将 用空格隔开

function LetterCountI(str){
  var wordsAndAmount={};
  var mostRepeatLetters="-1";
  var words=str.split(" ");

     words.forEach(function(word){
       // returns value of how many repeated letters in word.
       var howManyRepeat=word.toLowerCase().split("").sort().join("").match(/([.])\1+/g).length;       
        // if there are repeats(at least one value).
        if(howManyRepeat !== null || howManyRepeat !== 0){ 
          wordsAndAmount[word] = howManyRepeat;
        }else{
         // if no words have repeats will return -1 after for in loop.
         wordsAndAmount[word] = -1; 
         }
     });

     // word is the key, wordsAndAmount[word] is the value of word.
     for(var word in wordsAndAmount){ 
        // if two words have same # of repeats pick the one before it.
        if(wordsAndAmount[word]===mostRepeatLetters){ 
          mostRepeatLetters=mostRepeatLetters;
        }else if(wordsAndAmount[word]<mostRepeatLetters){ 
          mostRepeatLetters=mostRepeatLetters;
        }else if(wordsAndAmount[word]>mostRepeatLetters){
          mostRepeatLetters=word;
        }
      } 

  return mostRepeatLetters;
}

// TESTS
console.log("-----");   
console.log(LetterCountI("Today, is the greatest day ever!"));   
console.log(LetterCountI("Hello apple pie"));    
console.log(LetterCountI("No words"));    
函数LetterCountI(str){
var wordsAndAmount={};
var mostRepeatLetters=“-1”;
var words=str.split(“”);
words.forEach(函数(word){
//返回word中有多少个重复字母的值。
var howManyRepeat=word.toLowerCase().split(“”.sort().join(“”).match(/([。])\1+/g).length;
//如果存在重复(至少一个值)。
如果(howManyRepeat!==null | | howManyRepeat!==0){
words和amount[word]=howManyRepeat;
}否则{
//如果没有单词重复,则for in循环后将返回-1。
words和amount[word]=-1;
}
});
//单词是关键,单词和数量是单词的价值。
对于(字和金额中的var字){
//如果两个单词重复次数相同,则选择前面的一个。
如果(单词和数量[单词]==大多数重复字母){
mostRepeatLetters=mostRepeatLetters;
}else if(单词和金额[单词]多数字母){
大多数字母=单词;
}
} 
回信最多;
}
//测验
console.log(“----”;
log(LetterCountI(“今天,是有史以来最伟大的一天!”);
log(LetterCountI(“你好苹果派”);
控制台日志(LetterCountI(“无文字”);

非常感谢您的指导。谢谢!!^ ^ ^ ^ ^ ^

以下是工作代码片段:

/*
使用JavaScript语言,让函数LetterCountI(str)接受str
正在传递的参数,并返回具有最大
重复的信件。例如:“今天是有史以来最伟大的一天!“你应该回来
最伟大的是因为它有两个e(和两个t),而且它是前所未有的
有2个e。如果没有重复字母的单词,返回-1。单词将
用空格隔开。
log(LetterCountI(“今天,是有史以来最伟大的一天!”)=“最伟大的”);
log(LetterCountI(“你好苹果派”)==“你好”);
console.log(LetterCountI(“无字”)=-1);
提示:
这是一个有趣的问题。我们可以做的是使用string.toLowerCase将字符串转换为小写,然后在“”上拆分,这样我们就得到了一个字符数组。
然后,我们将使用Array.sort对其进行排序。排序后,我们将使用Array.join对其进行联接。然后,我们可以使用regex/()\1+/g,这基本上意味着匹配字母和后续字母(如果相同)。
当我们将String.match与声明的正则表达式一起使用时,我们将得到一个数组,其长度就是答案。还使用了一些try…catch来返回0,以防match返回null并导致TypeError。
/()\1+/g使用match方法将返回一个字母值,这些字母会依次出现。如果没有sort(),这将不起作用。
*/
函数LetterCountI(str){
var wordsAndAmount={};
var mostrepeatlets=“”;
var words=str.split(“”);
words.forEach(函数(word){
var howManyRepeat=word.toLowerCase().split(“”.sort().join(“”.match(/()\1+/g);
如果(howManyRepeat!==null&&howManyRepeat!==0){//如果存在重复(至少一个值)。。
words和amount[word]=howManyRepeat;
}否则{
wordsAndAmount[word]=-1;//如果没有重复的单词,则在for in循环后返回-1。
}
});
//console.log(words和amount);
对于(words和amount中的var word){//word是关键字,words和amount[word]是word的值。
//console.log(“Key=“+word”);
//console.log(“val=”+words和amount[word]);
if(words和amount[word].length>mostRepeatLetters.length){//如果两个单词重复次数相同,则选择其前面的一个。
大多数字母=单词;
}
}	
返回mostRepeatLetters?mostRepeatLetters:-1;
}
//测验
console.log(“----”;
log(LetterCountI(“今天,是有史以来最伟大的一天!”);
log(LetterCountI(“你好苹果派”);
控制台日志(LetterCountI(“无文字”);
/*
分词
var wordsAndAmount={};
var mostRepeatLetters=0;
循环单词
检查单词是否有重复的字母,如果有
将数量推入对象
Like words和amount[word[i]]=一个数字
如果没有重复的字母…没有其他。
循环遍历对象
比较新单词中重复字母的数量和最多重复字母的数量。
最后返回重复字母最多的单词的结果
如果所有单词都没有重复的字母,则返回-1,即。

*/
希望您不介意我重写此代码。我的代码可能没有那么有效。
这里是一个片段

function findGreatest() {
   // ipField is input field
    var getString = document.getElementById('ipField').value.toLowerCase();
    var finalArray = [];
    var strArray = [];
    var tempArray = [];
    strArray = (getString.split(" "));
    // Take only those words which has repeated letter
    for (var i = 0, j = strArray.length; i < j; i++) {
        if ((/([a-zA-Z]).*?\1/).test(strArray[i])) {
            tempArray.push(strArray[i]);
        }
    }
    if (tempArray.length == 0) {       // If no word with repeated Character
        console.log('No such Word');
        return -1;
    } else {                 // If array has words with repeated character
        for (var x = 0, y = tempArray.length; x < y; x++) {
            var m = findRepWord(tempArray[x]);  // Find number of repeated character in it
            finalArray.push({
                name: tempArray[x], 
                repeat: m
            })
        }
      // Sort this array to get word with largest repeated chars
        finalArray.sort(function(z, a) {
            return a.repeat - z.repeat
        })
        document.getElementById('repWord').textContent=finalArray[0].name;
    }
}

// Function to find the word which as highest repeated character(s)
    function findRepWord(str) {
        try {
            return str.match(/(.)\1+/g).length;
        } catch (e) {
            return 0;
        } // if TypeError
    }
函数findCreatest(){
//ipField是输入字段
var getString=document.getElementById('ipField').value.toLowerCase();
var finalArray=[];
var strArray=[];
var tempArray=[];
strArray=(getString.split(“”);
//只取那些有重复字母的单词
对于(变量i=0,j=strArray.length;i