Truncate a String what's wrong?

So this is my code so far and the first two tasks are marked as X. What am I doing wrong?


function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length > num && num > 3) {  // a simple if statement to determine one of three outcomes…
    return str.slice(0, (num - 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.*/
  } else if (str.length > num && num <= 3) { /*if our string length is greater than the num but num is within the first three characters, we don’t have to count our dots as characters. Therefore, we return the same string as above, with one difference: The endpoint of our slice is now just num. */
    return str.slice(0, num) + '...';
  } else {                                /* Finally, if none of the above situations are true, it means our string length is less than our truncation num. Therefore, we can just return the string*/
    return str;
  }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0.

Link to the challenge:

Looks like your first test is yielding

A-tis…

It has to be

A-tisket…

Make sure you including first 8 spaces.

So I change the: (str.length > num && num > 3) into (str.length > num && num > 8)?

So wherever was the number 3 I put 8 and the first task was solved but then I put 11 and all good. But still I can’t understand why :sweat_smile:

No worries, can you paste your new full code?

With ths code the challenges is passed :

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

You are using the code from the hint, that is outdated code from when the challenge included the three dots in the length of the string, the solution would be much more simple than this

If you can understand the code in the hint you should be able to make it to solve this challenge

1 Like

Actually this code was from the older curriculum that I have as a backup. But thanks

Anyway, the challenge has changed

I was going to ask about the num-3 (: nice to know it was an old thing.
If you need more help, tell us; the best way to learn is understanding it all; and remember, pseudocode is your best friend.

1 Like

Thanks and I appreciate your time :slight_smile:

I got the same issue thanks for asking help… I’ve learned from that!