Truncate a String --Need Help--

Tell us what’s happening:
Hi guys! Can you help me? I also tried this codes from the hint button:

  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;
  }

}

and:

function truncateString(str, num) {
  if (str.length <= num) {
    return str;
  } else {
    return str.slice(0, num > 3 ? num - 3 : num) + '...';
  }
}

but… The console keeps saying:

// running test
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...".
// tests completed

please help mee

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 - 3)) + '...';
  } else if (str.length > num && num <= 3) {
    return str.slice(0, num) + '...';
  } else {
    return str;
  }

}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

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

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a 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.

markdown_Forums

A good first step is to look at what your code is actually returning for the test cases. This can be done in FCC using the browser’s console, but I’ve put your code in a repl.it to show you.

Why is it always returning undefined hahahha, I Really don’t understand :disappointed_relieved::sob:

The undefined at the end just has to do with the repl session finishing. I actually call your function in the two console statements. That’s where you can see what it’s returning.

How! I’m totally confused right now, I’m really sorry

I really can’t solve it, OMG, can you please help me?

Instead of “A-tisket…”, your code returns “A-tis…”
Instead of “Peter Piper…”, your code returns “Peter Pi…”

This tells you why the tests are failing. From this you can start to see a pattern: both of these are 3 characters shorter than they should be. That should give you a starting point to look at your solution and see if you can figure out where the error in your solution might be.

I really don’t get it sirr :sob:

Spend some time with it. I tried to give you a nudge toward what your problem is, but it will probably take more than 2 minutes to figure it out. Go tinker with your code specifically looking for what logic results in some of your results being too short. You probably want to use console.log statements to walk through your code and see if you can figure out when the error occurs.

When I looked at the solution I was also confused, they didn’t give me any further details that allowed me to understand that I was supposed to do what they were doing in the solutions . Why does my solution pass the tests despite being really basic but using the solutions provided by them the tests don’t pass?

I had already did this exercise before the update to the curriculum but I don’t really recall whether it was like this before or whether not but I think they changed the exercise and made it more basic.

I also tried this code:

function truncateString(str, num) {
  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;
  }
}

but the same thing happens. Please Help Me Guyss!!

The number 3 isn’t necessary to pass the challenge

What do you mean sir?

Ok sir, I’ll do what you said.

Sir, I got the answer, thanks for encouragement :grinning:

function truncateString(str, num) {
  if (str.length < num) return str;
  var truncStr = str.slice(0, num);
  var truncStrArr = truncStr.split(' ');
  var truncStrArrLen=truncStrArr.length;
  
  if(truncStrArrLen > 1 &&
    truncStrArr[truncStrArrLen - 1] !== str.split(' ')[truncStrArrLen - 1]) {
    truncStrArr.pop();
    truncStr = truncStrArr.join(' ');
  }
  return str.length > num ? truncStr + '...' : truncStr;
}

console.log(truncateString("A-tisket a-tasket A green and yellow basket", 

@JanzenGo can you break down your answer for clarification?

Within the 2nd ‘if’ statement after the && regarding truncStrArr[truncStrArrLen - 1] … and so forth

Thanks so much Randell, you are a lifesaver!