title: JS: Repeated string (Hackerrank Challenge)
body: <p>I'm doing one of the challenges in Hackerrank as below:</p>
  
  <blockquote>
    <p>Lilah has a string, s, of lowercase English letters that she repeated
    infinitely many times.</p>
    
    <p>Given an integer, n, find and print the number of letter a’s in the
    first n letters of Lilah’s infinite string.
    The first line contains a single string, s.
    The second line contains an integer, n.</p>
  </blockquote>
  
  <p>I will need to print a single integer denoting the number of the letter a’s in the first N letters of the infinite string created by repeating S infinitely many times.</p>
  
  <p>For example:</p>
  
  <blockquote>
    <p>s is 'aba', n = 10.. The first n = 10 letters of the infinite string
    are 'abaabaabaa...' Because there are 7 a’s, we'll get 7 as the final answer</p>
  </blockquote>
  
  <p>This is my answer. It passed first two cases but failed the rest. </p>
  
  <pre><code>function repeatedString(s, n) {
      var repeat = Math.round(n / s.length);
      var remainder = n % s.length;
      var answer = 0;
      for (var i = 0; i &lt; s.length; i++) {
          if (s.charAt(i) == 'a') {
              answer += repeat;
              if (i &lt; remainder)
                  answer++;
          }
      }
      return answer;
  }
  </code></pre>
  
  <p>If someone could have a look at this and suggest a better solution that would be great. </p>