I have the following function that will be applied to:
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
I have doubts About the code you use in the return , I can't figure it out. Specifically what does the '?' Y ':'.
function digPow(n, p) {
var x = String(n).split("").reduce((s, d, i) => s + Math.pow(d, p + i), 0)
return x % n ? -1 : x / n
}
x % n ?
ask if there is a remainderif there is then the output is
-1
otherwise the output is
x/n
so you understand this part:
-1 : x / n
output if true : output if false