Skip to content

Instantly share code, notes, and snippets.

@moghanbari
Last active October 16, 2022 17:12
Show Gist options
  • Save moghanbari/bdfe0a3d825c72fd6bcb5f518a1d9cde to your computer and use it in GitHub Desktop.
Save moghanbari/bdfe0a3d825c72fd6bcb5f518a1d9cde to your computer and use it in GitHub Desktop.
// 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