Created
June 11, 2021 14:06
-
-
Save batoreh/e72fe3e8bef7ece17e1bcb542990f255 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function decimal(number) { | |
return ({ | |
toBase: (base) => { | |
let remainder = number | |
let result = [] | |
let shouldDivide = true | |
while (shouldDivide) { | |
result[result.length] = remainder % base | |
remainder = Math.floor(remainder / base) | |
if (remainder < base) { | |
result[result.length] = remainder | |
shouldDivide = false | |
} | |
} | |
return result.reverse().join('') | |
} | |
}) | |
} | |
console.log(decimal(100).toBase(3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment