Why doesn’t this code work? I’m testing it out in my console and all the examples that it says I’m failing are coming out correct?
Your code so far
function truncateString(str, num) {
// Clear out that junk in your trunk
str.slice(0, num) + '...';
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; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.
Oh wait so am I just returning the string parameter as soon as it’s past to the function?
function truncateString(str, num) {
// so the modified string exits in this line
str.slice(0, num) + ‘…’;
//while instead of taking that modified string I’m taking the parameter unchanged?
return str;
}
Part of being a dev is playing very close attention to details.
Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.
It doesn’t say to truncate it to num characters and then add the ellipsis. Only add the ellipsis is the string was more than num characters.
If you look at the bottom left, it tells you exactly what failed and why:
truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length) should return “A-tisket a-tasket A green and yellow basket”.
truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2) should return “A-tisket a-tasket A green and yellow basket”.
Read closely.
Geo was trying to point you in the right direction when he said, “You only return the original str if num === str.length, so you will need a condition there (if…).”