본문 바로가기

Algorithms

freeCodeCamp - 8. Truncate a String [Basic Algorithm Scripting]

Truncate a String

 

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string

 

www.freecodecamp.org

 

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

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