Hello I want to ask what's wrong with my solution when I am gonna test the solution it appears that expected -1 to equal 12933 The whole instructions are from codewors
The Question: Some numbers have funny properties. For example:
89 --> 8¹ + 9² = 89 * 1
695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2
46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
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 that the sum of the digits of n taken to the successive powers of p is equal to k * n. In other words:
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.
Note: n and p will always be given as strictly positive integers.
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
My Solution:
function digPow(p, n) {
let arrayP = [...String(p)].map((e) => Number(e));
let arrayPLength = arrayP.length;
let nums = [];
let firstResult = [];
for (let i = n; i < 5000; i++) {
nums.push(i);
}
for (let i = n; i < nums.length; i++) {
let currentPow = [];
let j = i;
let k = i + arrayPLength;
while (j < k) {
currentPow.push(j);
j++;
if (j == k) break;
}
let result = 0;
for (let h = 0; h < arrayP.length; h++) {
result = arrayP[h] ** currentPow[h] + result;
}
firstResult.push(result);
}
for (let i = 0; i < firstResult.length; i++) {
for (let j = 0; j < 100; j++) {
if (firstResult[i] == p * j) return j;
}
}
return -1;
}