Skip to content

Instantly share code, notes, and snippets.

@batoreh
Created June 11, 2021 14:06
Show Gist options
  • Save batoreh/e72fe3e8bef7ece17e1bcb542990f255 to your computer and use it in GitHub Desktop.
Save batoreh/e72fe3e8bef7ece17e1bcb542990f255 to your computer and use it in GitHub Desktop.
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