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('');
Number.prototype.toLocaleString.input.toLocaleString('en-US'). Specify theen-USfor a 1000's separator. Number representation inen-IN, for example, is different(e.g. with .map or .reduce).You could do ->input.split("").map((m,i) => (a.length - i) % 3 === 0 && (i > 0) ? "," + m : m).join("")