function truncateString(str, num) {
// Clear out that junk in your trunk
if (str.length > num && num > 3) {
return str.slice(0, (num - 3)) + '...';
} else if (str.length > num && num <= 3) {
return str.slice(0, num) + '...';
} else {
return str;
}
}
After doing google many times and think about the pseudo code,
I am unable to understand the code logic to truncate a string.
why we are assigning num > 3 why 3 and num -1
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.
Essentially, the solution that you’re drawing from isn’t up to date. That num > 3 is reflective of an older, more difficult challenge where you had to include the ‘…’ as a part of the string length.