title: A code wars challenge
body: <p>I have been struggling with this challenge and can't seem to find where I'm failing at:</p>
  
  <p>Some numbers have funny properties. For example:</p>
  
  <pre><code>89 --&gt; 8¹ + 9² = 89 * 1
  
  695 --&gt; 6² + 9³ + 5⁴= 1390 = 695 * 2
  
  46288 --&gt; 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
  </code></pre>
  
  <p>Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p we want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n. In other words:</p>
  
  <p>Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
  If it is the case we will return k, if not return -1.</p>
  
  <p>Note: n, p will always be given as strictly positive integers.</p>
  
  <pre><code>digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1
  digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k
  digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2
  digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
  </code></pre>
  
  <p>I'm new with javascript so there may be something off with my code but I can't find it. My whole purpose with this was learning javascript properly but now I want to find out what I'm doing wrong.I tried to convert given integer into digits by getting its modulo with 10, and dividing it with 10 using trunc to get rid of decimal parts. I tried to fill the array with these digits with their respective powers. But the test result just says I'm returning only 0.The only thing returning 0 in my code is the first part, but when I tried commenting it out, I was still returning 0.</p>
  
  <pre><code>function digPow(n, p){
  // ...
  var i;
  var sum;
  var myArray= new Array();
  if(n&lt;0)
    {
      return 0;
    }
  var holder;
  holder=n;
  for(i=n.length-1;i&gt;=0;i--)
   {
     if(holder&lt;10)
      {
       myArray[i]=holder;
       break;
      }
    myArray[i]=holder%10;
    holder=math.trunc(holder/10);
    myArray[i]=math.pow(myArray[i],p+i);
    sum=myArray[i]+sum;
  
  }
   if(sum%n==0)
    {
    return sum/n;
    }  
  else
  {
    return -1;
  }}
  </code></pre>