Truncate a String 5

Tell us what’s happening:
So confused on how to complete this loop. I’ve tried using the “Get a hint” option and tried both solutions, but it kept coming out as incorrect. How do I figure this one out?

Your code so far


function truncateString(str, num) {
  // Clear out that junk in your trunk
  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; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36 Avast/72.0.1174.122.

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

How many of the tests are you getting to work? Remember you need to have two seperate return statements to fulfill the requirements.

One for if the str.length is greater then the num and one for if it’s not.

I tried the basic and advanced tests. Both of them never worked for some reason.

If all you have is “return str” then that won’t work.

You need an if statement to check if the str.length is greater than num. If it is then you need to use slice & num to extract the necessary portion & add “…” with concatenation on the end.

The else statement should be a return of the full str.

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;

}

}

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

That’s my current code.

  1. There should just be an if & else. The if conditional should check if (str.length > num)
  2. this is what is right:

return str.slice(0, num) + ‘…’;
} else {
return str;
}

I’m so confused now. I had it like that, I thought, and looked all through it. I don’t understand what I did wrong in this lesson.

You have it almost right. Change this

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

to

function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length > num) {
    <insert the answer>
  } else {
    <insert the answer>
  }
}```

I’m still getting it wrong, apparently. I keep getting the error about the code being wrong. I don’t know what I’m doing wrong at all.

If you need this is the solution.

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

Thank you so much. I was not sure what I was doing wrong, but now I get it. My brain is just not working today. I’m learning all of this for my college courses I’m fixing to be taking, so I’m trying to get as much done as possible.

You’re welcome. Keep up the good work.

Thanks. I definitely will. I think I need a break once I finish this one lesson I just got onto. I have to get up early to take my nephew to school with my mom then go to two different doc’s appointments. Plus I’m starting my new courses tomorrow, two of them to be exact, and this website was presented to me by one of my favorite professors who said if I wanted to learn code, to come straight to this website, as she used to be a moderator on here, she said.

FreeCodeCamp is very good for beginners. I would also recommend https://javascript.info/ for more information on JS.

Alright :slight_smile: I’ll definitely save that website to my laptop now :slight_smile:

For this challenge the hint is outdated, so that wouldn’t work, in the hint the code includes the three dots in the final string length, instead you don’t need to do that in the new version of the challenge, the three dots are not counted in the length of the string