title: count 9's from 1 to n - 5kyu Kata
body: <p>I am working on this kata <a href="https://www.codewars.com/kata/count-9-s-from-1-to-n/train/javascript" rel="nofollow noreferrer">https://www.codewars.com/kata/count-9-s-from-1-to-n/train/javascript</a></p>
  
  <p>and i have written this code for it, but its not working. This question is similar to this one <a href="https://stackoverflow.com/questions/11891984/count-the-number-of-occurrences-of-0s-in-integers-from-1-to-n">Count the number of occurrences of 0&#39;s in integers from 1 to N</a> 
  but it is different because searching for 9's is practically very different to searching for 0's.
  think part of the problem with this code is that it takes too long to run...
  any advice appreciated!</p>
  
  <pre><code>function has9(n) {
  
    var nine = [];
  
    var ninearr = n.toString().split('');
  
    for (var j = 0; j &lt; ninearr.length; j++) {
      if (ninearr[j] == '9') {
  
        nine.push(ninearr[j]);
      }
    }
    return nine.length;
  }
  
  function number9(n) {
    var arr = [];
    var arrnew = [];
  
    for (var i = 0; i &lt;= n; i++) {
      arr.push(i);
    }
  
    for (var l = 0; l &lt; arr.length; l++) {
      arrnew.push(has9(l));
  
    }
  
    var sum = arrnew.reduce((a, b) =&gt; a + b, 0);
    return sum;
  }
  </code></pre>