Created
October 25, 2021 15:15
-
-
Save freakinbook/26b4d6f62efad4c0237160e414bf669b to your computer and use it in GitHub Desktop.
Turn an input string into an array of ints
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 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