Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69738771fb5a7b8b24cca2a4.md
T
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.3 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69738771fb5a7b8b24cca2a4 Challenge 178: Truncate the Text 28 challenge-178

--description--

Given a string, return it as-is if it's 20 characters or shorter. If it's longer than 20 characters, truncate it to the first 17 characters and append "..." to the end of it (so it's 20 characters total) and return the result.

--hints--

truncateText("Hello, world!") should return "Hello, world!".

assert.equal(truncateText("Hello, world!"), "Hello, world!");

truncateText("This string should get truncated.") should return "This string shoul...".

assert.equal(truncateText("This string should get truncated."), "This string shoul...");

truncateText("Exactly twenty chars") should return "Exactly twenty chars".

assert.equal(truncateText("Exactly twenty chars"), "Exactly twenty chars");

truncateText(".....................") should return "....................".

assert.equal(truncateText("....................."), "....................");

--seed--

--seed-contents--

function truncateText(text) {

  return text;
}

--solutions--

function truncateText(text) {
  if (text.length <= 20) return text;
  return text.slice(0, 17) + "...";
}