Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
boo-who.english.md
chunky-monkey.english.md
confirm-the-ending.english.md
convert-celsius-to-fahrenheit.english.md
factorialize-a-number.english.md
falsy-bouncer.english.md
find-the-longest-word-in-a-string.english.md
finders-keepers.english.md
mutations.english.md
repeat-a-string-repeat-a-string.english.md
return-largest-numbers-in-arrays.english.md
reverse-a-string.english.md
slice-and-splice.english.md
title-case-a-sentence.english.md
truncate-a-string.english.md
where-do-i-belong.english.md
basic-data-structures
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
formattingConversion
math-challenges
requiresTests
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
guide
mock-guide
search-indexing
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.french.md
README.italian.md
README.korean.md
README.md
SECURITY.md
azure-pipelines.yml
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
libcimp_index_js.patch
package-lock.json
package.json
patch_npm_and_install.sh
sample.env
Valeriy 79d9012432 fix(curriculum): quotes in tests ()
* fix(curriculum): tests quotes

* fix(curriculum): fill seed-teardown

* fix(curriculum): fix tests and remove unneeded seed-teardown
2018-10-20 23:32:47 +05:30

3.3 KiB

id, title, isRequired, challengeType
id title isRequired challengeType
ac6993d51946422351508a41 Truncate a String true 5

Description

Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending. Remember to use Read-Search-Ask if you get stuck. Write your own code.

Instructions

Tests

tests:
  - text: <code>truncateString("A-tisket a-tasket A green and yellow basket", 8)</code> should return "A-tisket...".
    testString: assert(truncateString("A-tisket a-tasket A green and yellow basket", 8) === "A-tisket...", '<code>truncateString("A-tisket a-tasket A green and yellow basket", 8)</code> should return "A-tisket...".');
  - text: <code>truncateString("Peter Piper picked a peck of pickled peppers", 11)</code> should return "Peter Piper...".
    testString: assert(truncateString("Peter Piper picked a peck of pickled peppers", 11) === "Peter Piper...", '<code>truncateString("Peter Piper picked a peck of pickled peppers", 11)</code> should return "Peter Piper...".');
  - text: <code>truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)</code> should return "A-tisket a-tasket A green and yellow basket".
    testString: assert(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length) === "A-tisket a-tasket A green and yellow basket", '<code>truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)</code> should return "A-tisket a-tasket A green and yellow basket".');
  - text: <code>truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2)</code> should return "A-tisket a-tasket A green and yellow basket".
    testString: assert(truncateString('A-tisket a-tasket A green and yellow basket', 'A-tisket a-tasket A green and yellow basket'.length + 2) === 'A-tisket a-tasket A green and yellow basket', '<code>truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2)</code> should return "A-tisket a-tasket A green and yellow basket".');
  - text: <code>truncateString("A-", 1)</code> should return "A...".
    testString: assert(truncateString("A-", 1) === "A...", '<code>truncateString("A-", 1)</code> should return "A...".');
  - text: <code>truncateString("Absolutely Longer", 2)</code> should return "Ab...".
    testString: assert(truncateString("Absolutely Longer", 2) === "Ab...", '<code>truncateString("Absolutely Longer", 2)</code> should return "Ab...".');

Challenge Seed

function truncateString(str, num) {
  // Clear out that junk in your trunk
  return str;
}

truncateString("A-tisket a-tasket A green and yellow basket", 8);

Solution

function truncateString(str, num) {
  if (num >= str.length) {
    return str;
  }

  return str.slice(0, num) + '...';
}

truncateString("A-tisket a-tasket A green and yellow basket", 8);