title: I don't understand why this code works this way in my code
body: <p>I'm not sure what result[i] is that ?
  I just want to understand why this code used this way ?</p>
  
  <pre><code>function countChars(string, character) {
    var result = string;
    var count = 0;
    var i = 0;
    while ( i &lt; string.length) {
      if (result[i] === character) {
        count = count + 1 ;
      } 
  </code></pre>
  
  <p>I can guess this way might work .</p>
  
  <p>string[i]</p>
  
  <p>Is there reason why result[i] should be in there?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function countChars(string, character) {
    var result = string;
    var count = 0;
    var i = 0;
    while ( i &lt; string.length) {
      if (result[i] === character) {
        count = count + 1 ;
      } 
      result.slice(0,1);
      i++
    }
    return count;
  }
  
  console.log(
    countChars("hello","h") // &gt;&gt; 1
  )  </code></pre>
  </div>
  </div>
  </p>