title: Return the first word with the greatest number of repeated letters
body: <p>This is a question from coderbyte’s easy set. Many people asked about it already, but I’m really curious about what’s wrong with my particular solution (I know it’s a pretty dumb and inefficient one..)</p>
  
  <p>Original question:</p>
  
  <blockquote>
    <p>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>
  </blockquote>
  
  <p>My solution works most of the time. But if it seems the last word of the input isn’t valued by my code. For example, for “a bb ccc”, “bb” will be returned instead of “ccc”. But the funny thing here is if the string only contains one word, the result is correct. For example, “ccc” returns “ccc”.</p>
  
  <p>Please tell me where I was wrong. Thank you in advance!</p>
  
  <pre><code>function LetterCountI(str) { 
  
    str.toLowerCase();
  
    var arr = str.split(" ");
  
    var count = 0;
    var word = "-1";
  
    for (var i = 0; i &lt; arr.length; i++) {
     for (var a = 0; a &lt; arr[i].length; a++) {
       var countNew = 0;
       for (var b = a + 1; b &lt; arr[i].length; b++) {
         if(arr[i][a] === arr[i][b])
            countNew += 1;
       }
       if (countNew &gt; count) {
         count = countNew;
         word = arr[i];
       }
     }
     return word;
    }
  
  
  }       
  </code></pre>