Truncate a String 0

Truncate a String 0
0

#1

Tell us what’s happening:
How does this code work?

Your code so far


function truncateString(str, num) {
  if (str.length > num) {
    return (str.substr(0, num) + "...");
  }
  return (str.substr(0, num));
}

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

Your browser information:

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

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


#2

Take a look at the documentation of String.prototype.substr