Skip to content

Instantly share code, notes, and snippets.

@freakinbook
Created October 25, 2021 15:15
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 freakinbook/26b4d6f62efad4c0237160e414bf669b to your computer and use it in GitHub Desktop.
Save freakinbook/26b4d6f62efad4c0237160e414bf669b to your computer and use it in GitHub Desktop.
Turn an input string into an array of ints
function deconstructNumber(deconstructee) {
let numberAsArray = [];
let divisor = 10;
let index = 0;
let sign = "";
if (deconstructee.includes("-")) {
deconstructee = deconstructee.substring(1);
sign = "-";
}
if (deconstructee === "0") {
return [0];
}
while (deconstructee / divisor >= 0.1) {
let trimGreaterPositions = deconstructee - Math.floor(deconstructee / divisor) * divisor;
let nextDigit = Math.floor(trimGreaterPositions / (divisor / 10));
divisor *= 10;
numberAsArray[index++] = nextDigit;
}
if (sign === "-") {
numberAsArray[numberAsArray.length] = sign;
}
console.log(numberAsArray);
return numberAsArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment