Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.5 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
68b06e589bf2273243814775 Challenge 38: Slug Generator 28 challenge-38

--description--

Given a string, return a URL-friendly version of the string using the following constraints:

  • All letters should be lowercase.
  • All characters that are not letters, numbers, or spaces should be removed.
  • All spaces should be replaced with the URL-encoded space code %20.
  • Consecutive spaces should be replaced with a single %20.
  • The returned string should not have leading or trailing %20.

--hints--

generateSlug("helloWorld") should return "helloworld".

assert.equal(generateSlug("helloWorld"), "helloworld");

generateSlug("hello world!") should return "hello%20world".

assert.equal(generateSlug("hello world!"), "hello%20world");

generateSlug(" hello-world ") should return "helloworld".

assert.equal(generateSlug(" hello-world "), "helloworld");

generateSlug("hello world") should return "hello%20world".

assert.equal(generateSlug("hello  world"), "hello%20world");

generateSlug(" ?H^3-1*1]0! W[0%R#1]D ") should return "h3110%20w0r1d".

assert.equal(generateSlug("  ?H^3-1*1]0! W[0%R#1]D  "), "h3110%20w0r1d");

--seed--

--seed-contents--

function generateSlug(str) {

  return str;
}

--solutions--

function generateSlug(str) {
  let cleaned = str.replace(/[^a-zA-Z0-9 ]+/g, "");
  cleaned = cleaned.replace(/\s+/g, " ").trim();
  return cleaned.toLowerCase().replace(/ /g, "%20");
}