Last active
October 16, 2022 17:12
-
-
Save moghanbari/bdfe0a3d825c72fd6bcb5f518a1d9cde to your computer and use it in GitHub Desktop.
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
// Using Repeat | |
const breakNumberIntoObject = (inputNumber) => { | |
if(!Number(inputNumber)) { | |
return; | |
} | |
const inputNumberArray = String(inputNumber).split(''); | |
const output = {}; | |
inputNumberArray.map((num) => { | |
const repeatedKey = num.repeat(Number(num)); | |
Object.assign(output, {[num]: repeatedKey}); | |
}); | |
console.log(output); | |
}; | |
breakNumberIntoObject(125); | |
breakNumberIntoObject(5125); | |
// Using simple loop!!! | |
const breakNumberIntoObjectWithForLoop = (inputNumber) => { | |
if(!Number(inputNumber)) { | |
return; | |
} | |
const inputNumberArray = String(inputNumber).split(''); | |
let output = []; | |
inputNumberArray.map((num) => { | |
let a = ''; | |
for (let index = 1; index <= Number(num); index++) { | |
a += num; | |
} | |
output.unshift({[num]: a}); | |
}); | |
output = Object.assign({}, ...output); | |
console.log(output); | |
}; | |
breakNumberIntoObjectWithForLoop(432); | |
breakNumberIntoObjectWithForLoop(5125); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment