freeCodeCamp/curriculum/challenges/spanish/02-javascript-algorithms-an.../basic-algorithm-scripting/truncate-a-string.spanish.md

3.6 KiB

id title localeTitle isRequired challengeType
ac6993d51946422351508a41 Truncate a String Truncar una cadena true 5

Description

Truncar una cadena (primer argumento) si es más larga que la longitud de cadena máxima dada (segundo argumento). Devuelve la cadena truncada con un ... final. Recuerda usar Read-Search-Ask si te atascas. Escribe tu propio código.

Instructions

Tests

tests:
  - text: &#39; <code>truncateString(&quot;A-tisket a-tasket A green and yellow basket&quot;, 8)</code> debe devolver &quot;A-tisket ...&quot;.&#39;
    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: &#39; <code>truncateString(&quot;Peter Piper picked a peck of pickled peppers&quot;, 11)</code> debería devolver &quot;Peter Piper ...&quot;.
    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: &#39; <code>truncateString(&quot;A-tisket a-tasket A green and yellow basket&quot;, &quot;A-tisket a-tasket A green and yellow basket&quot;.length)</code> debe devolver &quot;A-tisket a-tasket Una canasta verde y amarilla&quot;.&#39;
    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: &#39; <code>truncateString(&quot;A-tisket a-tasket A green and yellow basket&quot;, &quot;A-tisket a-tasket A green and yellow basket&quot;.length + 2)</code> debe devolver &quot;A-tisket a-tasket Una canasta verde y amarilla&quot; .
    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: &#39; <code>truncateString(&quot;A-&quot;, 1)</code> debe devolver &quot;A ...&quot;.&#39;
    testString: 'assert(truncateString("A-", 1) === "A...", "<code>truncateString("A-", 1)</code> should return "A...".");'
  - text: &#39; <code>truncateString(&quot;Absolutely Longer&quot;, 2)</code> debe devolver &quot;Ab ...&quot;.&#39;
    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);