DEV Community

Mohammed Awad
Mohammed Awad

Posted on • Updated on

Truncate a String

DESCRIPTION:

Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.

Examples

truncateString("Peter Piper picked a peck of pickled peppers", 11)
 //should return the string `Peter Piper....`

truncateString("A-tisket a-tasket A green and yellow basket",
 "A-tisket a-tasket A green and yellow basket".length)
//should return `A-tisket a-tasket A green and yellow basket.`
Enter fullscreen mode Exit fullscreen mode

My approach for solving this problem:

  • check the str length.
  • if the length is equal to num return str
  • if not return sliced str with num length

My solution:

function truncateString(str, num) {
  let slicedstr;

  if(str.length > num){
    slicedstr = str.slice(0,num)+"..."
  }else{
    slicedstr = str
  }

  return slicedstr;
}

truncateString("A-tisket a-tasket A green and yellow basket", 8)
Enter fullscreen mode Exit fullscreen mode

Any tips or edit are most welcome. share it with me on the comments. Thanks for being here!

Follow Muhmmad Awd on

If you have any questions or feedback, please feel free to contact me at

Top comments (4)

Collapse
 
kalkwst profile image
Kostas Kalafatis

Hello,

Thank you for providing your answer to the "Truncate a string" issue. Your code is clean, straightforward, and simple to understand.

You should think about the following potential areas for improvement in your code:

One potential problem is that the code does not check to see if the second parameter is a number. The function will behave strangely if the argument is a string or an object, for example. You could want to add some extra error handling to guarantee that the function only accepts numeric values.

Another potential issue is that there is no way to adjust the length of the ellipsis attached to the shortened string. The existing system, for example, would not allow a user to truncate a string to 10 characters and add three dots at the end. Consider adding an extra parameter that determines the duration of the ellipsis or making it a customizable option.

You should also think about refactoring your code to make it more efficient or expandable. To simplify the code, the function may, for example, use a ternary operator instead of an if-else expression. It might potentially be expanded to accommodate more sophisticated truncation needs, such as truncating the string in the middle rather than at the end.

Finally, I noticed that you included a paragraph at the end of your post that encouraged readers to like and share your content. While it's understandable to want recognition and validation for your efforts, I would advise against this approach.

We all here strive to maintain a welcoming and respectful community that values the quality of content over its popularity. Encouraging likes and shares can come across as self-promotion and may be perceived as an attempt to manipulate the system. Instead, I would suggest focusing on providing value through your posts and engaging with the community in a meaningful way.

Please don't let the lack of reactions discourage you. Creating content takes time and effort, and it's natural to want recognition for your work. But remember that the value of your contributions goes beyond the number of likes or shares they receive. Keep posting and engaging with the community, and I'm sure you'll find the support and encouragement you need.

Thank you for being a part of our community, and I look forward to seeing more of your posts in the future.

Collapse
 
xmohammedawad profile image
Mohammed Awad • Edited

thank you for these amazing tips, and I promise you I will remove this last paragraph and rely on making valuable content serve the community.

Collapse
 
michey85 profile image
Mikhail Nepomnyashchiy

What about immutable version?

function truncateString(str, num) {
return str.length > num ?
str.slice(0,num)+"..."
:
str;
}

Collapse
 
xmohammedawad profile image
Mohammed Awad

wow, nice and simple