the number is the power of three in recursion method, but it doesn't
const isPowerOfThree = (num, toCompare = 1) => {
if (toCompare > num) return false
if (toCompare === num) return true
return isPowerOfThree(num, toCompare ** 3)
}
Your approach is kind of backwards to the normal approach. Normally you just keep trying to divide by 3 evenly until you get to 1, or it doesn't divide evenly and you know it's not a power of 3.
If toCompare is initially 1, raising it to a power leaves it at 1 still. That's your bug.
Обсуждают сегодня