Truncate a String...what am I doing wrong?

Tell us what’s happening:
After running the tests I keep getting the following errors…

truncateString("A-tisket a-tasket A green and yellow basket", 8) should return "A-tisket..."
truncateString("Peter Piper picked a peck of pickled peppers", 11) should return "Peter Piper...".

Your code so far

function truncateString(str, num) {
  // Clear out that junk in your trunk
 if (num < 3){
   return str.slice(0, num) + "...";
 else {
   return num >= str.length ? str : str.slice(0, num - 3) + "...";
truncateString("A-tisket a-tasket A green and yellow basket", 11);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36.

Link to the challenge:

What is the purpose of this?

More over, what is the purpose of the following?

 if (num < 3){
   return str.slice(0, num) + "...";

Why are you basing the return on if num is less than 3? There is nothing in the instructions about this condition.

do the following:

if you want a condition as you did, say something like if there is no string length (check if its equal to zero or you can use !str.length in your first conditional. then return something to the user saying that there is no string length or just return ‘…’ or however you want to customize your string checking function. You can also add trim() inside of your logic there are many ways to customize this.

The second thing is that num is really the str.length which is the second argument passed into your truncateString() maybe the reason why you got confused is that if you take the string ‘…’ and set it to a variable then you check its length you get 3 ? thinking about these things can help you pass this challenge

send me a pm if you did not understand what I said, if youd like me to explain it better if you have not already passed this challenge.

You’re explanation made sense! After lots of reading I finally figured it out. Thank you!!

1 Like