I doing some practice with find power one number is power of another number. When I reach to base = 1
and result = 2
, it does not print out false
for me. What can I do to fix the issue?
Here is my code:
let base = 1; let result = 2; powerOf(base, result); function powerOf(base, result){ if (base == 1){ result == 1; } let powerOf = 1; while (powerOf < result){ powerOf = powerOf * base; } console.log(powerOf = result); return true; }
Advertisement
Answer
if you use powerOf as a variable to store your power result, when base is 1, powerOf must be 1, you don’t need to enter the while loop
let base = 1; let result = 2; console.log(powerOf(base, result)); function powerOf(base, result) { let powerOf = 1; while (powerOf < result && base != 1) { powerOf = powerOf * base; } return powerOf == result; }