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.
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.
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>
}
}```
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.
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.
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