Truncate a String2 (SPOILER) (SOLVED)

Tell us what’s happening:

It’s not working . not sure why ?

SOlved here
Your code so far


function truncateString(str, num) {
  // Clear out that junk in your trunk
 if (str.length > num && num > 3) {
    return str.slice(0, (num )) + '...';
  } else if (str.length > num && num <= 3) {
    return str.slice(0, num) + '...';
  } else {
    return str;
  }

}
 
 

truncateString("A-tisket 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/67.0.3396.99 Safari/537.36.

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

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

For the above test case, if (str.length > num && num > 3) { evaluates to true, so you execute the following return statement:

  return str.slice(0, (num - 3)) + '...';

Why? Because str.length (43) is greater than num (8) and num (8) is greater than 3.

If our string length is greater than the num we want to truncate at, and our truncate point is at least three characters or more into the string, we return a slice of our string starting at character 0, and ending at num - 3. We then append our ‘…’ to the end of the string.

Look at this line

if (str.length > num && num > 3) {
  return str.slice(0, (num - 3)) + '...';
}

Why num - 3 ?

  • if str.length === 48 && num === 8
  • then return str.slice(0, 5) + '...'

which just takes the first 5 letters and returns it. Is that what you really want?

Sorry i don’t get it yet . Can you write the correct answer and explain it to me again ?

If I just keep pasting the answers, you won’t learn. So let’s first try to get you to figure it out. It’ll be frustrating until you get it. But if you don’t learn the basics now, you’ll really struggle later.

Ok, so the instructions want you to return the number of characters passed in as an argument.

In this case it’s 8

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

So the information we have so far is

  • str.length === 48 -> this is the length of the string argument
  • num === 8 -> this is the number of characters to keep

But in the if block, you are saying:

  • subtract 3 from num, which is 5
  • keep the first letter until you reach 5 total letters
return str.slice(0, (num - 3)) + '...';

But the instructions are to

  • keep the first letter until you reach the total number of letters requested // 8

So figure out why you’re not returning 8 letters.

So it would be num-0 to get the 8 . I got it .

Thank you so much

1 Like

Nice job figuring it out.

Now think about this: if you subtract 0 from a number, do you even need to subtract at all?

These are the same

  • num(8) - 0 = 8
  • num(8) = 8

In other words, you could just use num, since any number minus 0 equals that number.

Keep up the great work :slight_smile: