title: javascript replace character with loop
body: <p>This should be simple but I am not sure why it isn't working:</p>
  
  <pre><code>function kebabToSnake (str){
      var string = "";
      var chart = "";
      for(i=0; i &lt; str.lenght; i++){
          if (str.charAt(i) == "-") {
              chart = "_";
              string = string + chart;
          }
          else {
              chart = str.charAt(i);
              string = string + chart;
          }
      }
      return string
  }
  </code></pre>
  
  <p>I know I could do it with str.replace(/-/g,"_") but I cannot see what's wrong with the above, besides being too long. Any help would be great. </p>