title: Longest palindrome in a string
body: <p>I wrote the following function to find the longest palindrome in a string. It works fine but it won't work for words like "noon" or "redder".  I fiddled around and changed the first line in the <code>for</code> loop from:</p>
  
  <pre><code>var oddPal = centeredPalindrome(i, i);
  </code></pre>
  
  <p>to </p>
  
  <pre><code>var oddPal = centeredPalindrome(i-1, i);
  </code></pre>
  
  <p>and now it works, but I'm not clear on <em>why</em>. My intuition is that if you are checking an odd-length palindrome it will have one extra character in the beginning (I whiteboarded it out and that's the conclusion I came to). Am I on the right track with my reasoning?</p>
  
  <pre><code>var longestPalindrome = function(string) {
  
    var length = string.length;
    var result = "";
  
    var centeredPalindrome = function(left, right) {
      while (left &gt;= 0 &amp;&amp; right &lt; length &amp;&amp; string[left] === string[right]) {
        //expand in each direction.
        left--;
        right++;
      }
  
      return string.slice(left + 1, right);
    }; 
  
    for (var i = 0; i &lt; length - 1; i++) {
      var oddPal = centeredPalindrome(i, i); 
      var evenPal = centeredPalindrome(i, i);
  
      if (oddPal.length &gt; result.length)
        result = oddPal;
      if (evenPal.length &gt; result.length)
        result = evenPal;
    }
  
    return "the palindrome is: " + result + " and its length is: " + result.length;
  };
  </code></pre>
  
  <p>UPDATE:
  After Paul's awesome <a href="https://stackoverflow.com/a/33677260/1324">answer</a>, I think it makes sense to change both variables for clarity:</p>
  
  <pre><code>var oddPal  = centeredPalindrome(i-1, i + 1);
  var evenPal = centeredPalindrome(i, i+1);
  </code></pre>