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