Back to Freecodecamp

Challenge 178: Truncate the Text

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69738771fb5a7b8b24cca2a4.md

latest1.1 KB
Original Source

--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!".

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

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

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

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

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

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

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

--seed--

--seed-contents--

js
function truncateText(text) {

  return text;
}

--solutions--

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