Skip to content

Instantly share code, notes, and snippets.

@Daymannovaes
Created May 3, 2022 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daymannovaes/cc9e3dfe9e1f150f7316e68d143ddad5 to your computer and use it in GitHub Desktop.
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
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