Created
March 6, 2025 05:12
-
-
Save mreed4/eefb85f9ad4a8c3580f8df3c7aa46bd0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// mreed4 committed on Nov 27, 2022 | |
function getExplodedNumber(number, toString) { | |
let digits = number.toString(10).length; | |
let explodedNumber = []; | |
for (let i = digits; i > 0; i--) { | |
let x = number % Math.pow(10, i); | |
let y = number % Math.pow(10, i - 1); | |
explodedNumber.push(x - y) | |
} | |
if (toString) { | |
explodedNumber = explodedNumber.map((part, j) => { | |
return part === 0 ? "0".repeat(digits - j) : part | |
}); | |
return explodedNumber.join(" + ") | |
} else { | |
return explodedNumber | |
} | |
} | |
for (let n = 1; n <= 5; n++) { | |
let randNum = Math.floor(Math.random() * 999_999); | |
let randBool = Math.round(Math.random()); // Returns number | |
let isToString = randBool === 1; // Returns boolean | |
console.log([ | |
{ randNum }, | |
{ isToString }, | |
getExplodedNumber(randNum, randBool) | |
]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment