title: Letter Count I JavaScript Challenge on Coderbyte
body: <p>I've been on this problem for several hours now and have done all I can to the best of my current newbie javaScript ability to solve this challenge but I just can't figure out exactly what's wrong. I keep getting "UNEXPECTED TOKEN ILLEGAL on here: <a href="http://jsfiddle.net/6n8apjze/14/" rel="nofollow noreferrer">http://jsfiddle.net/6n8apjze/14/</a></p>
  
  <p>and "TypeError: Cannot read property 'length' of null": <a href="http://goo.gl/LIz89F" rel="nofollow noreferrer">http://goo.gl/LIz89F</a></p>
  
  <p>I think the problem is the howManyRepeat variable. I don't understand why I'm getting it can't read the length of null when clearly word is a word from str...</p>
  
  <p>I got the idea for:</p>
  
  <pre><code>word.toLowerCase().split("").sort().join("").match(/([.])\1+/g).length
  </code></pre>
  
  <p>...here: <a href="https://stackoverflow.com/questions/28807016/get-duplicate-characters-count-in-a-string">Get duplicate characters count in a string</a>   </p>
  
  <p><strong>The Challenge:</strong><br>
  Using the JavaScript language, have the function LetterCountI(str) take the str 
  parameter being passed and return the first word with the greatest number of 
  repeated letters. For example: "Today, is the greatest day ever!" should return 
  greatest because it has 2 e's (and 2 t's) and it comes before ever which also 
  has 2 e's. If there are no words with repeating letters return -1. Words will 
  be separated by spaces. </p>
  
  <pre><code>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]&lt;mostRepeatLetters){ 
            mostRepeatLetters=mostRepeatLetters;
          }else if(wordsAndAmount[word]&gt;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"));    
  </code></pre>
  
  <p>Any guidance is much appreciated. Thank you!! ^____^</p>