Truncate a String-gt

Tell us what’s happening:
Can anyone verify if the below mentioned code is also a valid solution to the truncate a string challenge?

Your code so far


function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length > num){
 let  sl1 = str.slice(0,num);
 return sl1 +"..."
  }
 let sl2 = str.slice(0,num);
 return sl2 
}

console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length));

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string

it is a valid solution for sure, just a precisation:

you don’t need to slice three string of the string length is less then the provided limit

1 Like

Yeah got it, that was not a part of requirement of the challenge. Thank you!

it is just that is useless. if the second parameter in slice is more than the string length it just returns the whole string