What I'm trying to achieve is to create a formatted number with thousands-delimiter from a simple string input.
So my input would look something like let input = "12345"
and my expected return value should look like "12,345"
.
I know there are already several libraries, which take care of this, but I want to keep it simple and just do it by myself. My current solution is a bit redundant (because of the double .reverse()
) and I'm pretty sure, that there is a better solution.
let array = input.split('');
array.reverse();
for (let i = 3; i < array.length; i += 4) {
array.splice(i, 0, ',');
}
array.reverse();
return array.join('');