Created
May 3, 2022 13:20
-
-
Save Daymannovaes/cc9e3dfe9e1f150f7316e68d143ddad5 to your computer and use it in GitHub Desktop.
Given a integer, convert it to a list where each item is a digit of the number; without converting to String
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 getLastDigit(n) { | |
const n10 = n/10; | |
return Math.round((n10 - Math.floor(n10)) * 10); | |
} | |
function removeLastDigit(n) { | |
return parseInt((n - getLastDigit(n))/10); | |
} | |
function splitDigits(n) { | |
let digit; | |
let digits = []; | |
let newN = n; | |
while (newN !== 0) { | |
digit = getLastDigit(newN); | |
newN = removeLastDigit(newN); | |
digits.unshift(digit); | |
} | |
return digits; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment