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.8 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
68ffb91507a5b645769328c5 Challenge 102: Longest Word 28 challenge-102

--description--

Given a sentence string, return the longest word in the sentence.

  • Words are separated by a single space.
  • Only letters (a-z, case-insensitive) count toward the word's length.
  • If there are multiple words with the same length, return the first one that appears.
  • Return the word as it appears in the given string, with punctuation removed.

--hints--

longestWord("The quick red fox") should return "quick".

assert.equal(longestWord("The quick red fox"), "quick");

longestWord("Hello coding challenge.") should return "challenge".

assert.equal(longestWord("Hello coding challenge."), "challenge");

longestWord("Do Try This At Home.") should return "This".

assert.equal(longestWord("Do Try This At Home."), "This");

longestWord("This sentence... has commas, ellipses, and an exclamation point!") should return "exclamation".

assert.equal(longestWord("This sentence... has commas, ellipses, and an exclamation point!"), "exclamation");

longestWord("A tie? No way!") should return "tie".

assert.equal(longestWord("A tie? No way!"), "tie");

longestWord("Wouldn't you like to know.") should return "Wouldnt".

assert.equal(longestWord("Wouldn't you like to know."), "Wouldnt");

--seed--

--seed-contents--

function longestWord(sentence) {

  return sentence;
}

--solutions--

function longestWord(sentence) {
  const words = sentence.split(" ");
  let longest = "";

  for (let word of words) {
    const cleaned = word.replace(/[^a-z]/gi, "");
    if (cleaned.length > longest.length) {
      longest = cleaned;
    }
  }

  return longest;
}