Game of Threes
Write a recursive function that takes two integers, n and counter as parameters and does the following:
-
If the
nis divisible by three, divide it by three and call the function again, providing the new value asnandcounter+ 1 ascounter -
If not, either add or subtract
1to a get a new value that is divisible by three and repeat step 1. -
If
nis one, simply return the valuecounter.
Example
function gameOfThrees(n, counter){
// your code here
}
gameOfThrees(10000, 0)
// Output: 8