Implement the Truncate String Algorithm - Implement the Truncate a String Algorithm

Tell us what’s happening:

i dont know how to pass number 3 .. or how to make function that not include “…”

Your code so far

function truncateString(str,num) {
const shortString = str.slice(0,num);
const truncate = shortString + "...";
  if (str.length >= num && str.length){
    return truncate}  
  else if (str.length <= num){
    return str
  } 
};
console.log(truncateString("A-tisket a-tasket A green and yellow basket", 8));
console.log(truncateString("Peter Piper picked a peck of pickled peppers", 11));
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 0));
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2));
console.log(truncateString("A-", 1));
console.log(truncateString("Absolutely Longer", 2));









Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36

Challenge Information:

Implement the Truncate String Algorithm - Implement the Truncate a String Algorithm

Could you try to explain logic in this part?

1 Like