I'm trying to make aan SMS Shortner for a kata at codewarsCodewars.:
Your task is to shorten the message to 160 characters, starting from end, by replacing spaces with camelCase, as much as necessary.
I've tried with a whilewhile
loop and although the logic is correct, it's taking too long to process my tests. How can I optmize this and what parts are taking longer than it could?
const shortener = (message) => {
let r = message.split('').reverse().join('');
while (r.length > 160) {
r = r.replace(/([^\s]\s)/, (m,s) => s.trim().toUpperCase());
}
return r.split('').reverse().join('');
}
My question is how can I optmize this and what parts are taking longer than it could?