Skip to content

Latest commit

 

History

History
20 lines (17 loc) · 560 Bytes

File metadata and controls

20 lines (17 loc) · 560 Bytes

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

function digPow(n, p) {
  // ...
  let nArr = n.toString().split("");
  let sum = 0;
  for (let i = 0; i < nArr.length; i++) {
    sum += Math.pow(+nArr[i], p);
    p++;
  }
  let res = sum / n;

  return Number.isInteger(res) ? res : -1;
}