Udało Mi się skrócić funkcję
[...[...x].reverse().join``.match(/.{1,3}/g).join`.`].reverse().join``;
Przykład użycia
String.prototype.setDots = (function() {
return [...[...this].reverse().join``.match(/.{1,3}/g).join`.`].reverse().join``;
})
let x = 0, y = '';
for (let i=0; i<10; ++i) {
x = Math.floor(Math.random() * (10 - 1) + 1);
y = [...(y + (x).toString())].reverse().join``;
console.log(y, y.setDots());
}
można też podać co ile znaków ma być wstawiony wybrany znak (domyślnie jest co 3 wstawiana kropka)
String.prototype.setChar = (function(x=3, y='.') {
return [...[...this].reverse().join``.match(new RegExp(`.{1,${x}}`, 'g')).join(y)].reverse().join``;
})
Przykład użycia
String.prototype.setChar = (function(x=3, y='.') {
return [...[...this].reverse().join``.match(new RegExp(`.{1,${x}}`, 'g')).join(y)].reverse().join``;
})
let x = 0, y = '';
for (let i=0; i<10; ++i) {
x = Math.floor(Math.random() * (10 - 1) + 1);
y = [...(y + (x).toString())].reverse().join``;
console.log(y, y.setChar(), y.setChar(1,'|'), y.setChar(2,'+'));
}