Another golf-style solution that doesn't abuse regular expressions 😎
const splitInParts = (s, l) => { s.split('').map((c,i) => (i+1) % l ? c : `${c} `).join('').trim(); }
Here's the less abusive solution I would actually use:
const splitInParts = (s, partLength) => { const result = []; for (let start = 0; start < s.length; start += partLength) result.push(s.slice(start, start + partLength)); return result.join(' '); }
This works because String.prototype.slice() will stop at the end of the string if the second argument index is out of bounds.
String.prototype.slice()
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Another golf-style solution that doesn't abuse regular expressions 😎
Here's the less abusive solution I would actually use:
This works because
String.prototype.slice()
will stop at the end of the string if the second argument index is out of bounds.