Skip to main content
deleted 15 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Replace Replacing a string until it matches a certain length in javascript

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?

Replace string until it matches a certain length in javascript

I'm trying to make a SMS Shortner for a kata at codewars.

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 while loop and although the logic is correct, it's taking too long to process my tests.

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?

Replacing a string until it matches a certain length

I'm trying to make an SMS Shortner for a kata at Codewars:

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 while 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('');
}
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Source Link
Abensur
  • 143
  • 3

Replace string until it matches a certain length in javascript

I'm trying to make a SMS Shortner for a kata at codewars.

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 while loop and although the logic is correct, it's taking too long to process my tests.

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?