Tell us what’s happening:
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…”.
I don’t understand, why not work? 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;
}
}
truncateString("A-tisket a-tasket A green and yellow basket", 11);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.
where? out of the function? str is a local variable so it can’t be used out of the function. Inside the function whatever is after a return statement is not executed, so you will not be able to use that either. Also, str is never actually changed inside your function
This is the function call: truncateString("A-tisket a-tasket A green and yellow basket", 11)
so wrapping that in the console log will show you what your function returns
This seems the solution from the guild, which is grossly outdated and refer to an old version of the challenge. It is dangerous to use solutions you don’t understand. Try to figure out what is the difference between what this function does and what the challenge asks from you!
the error is that your function is counting also the three dots in the max number of characters when it shouldn’t, so it is returning "Peter Pi..." (which is 11 characters dots included), instead of "Peter Piper..." which is 11 characters dots excluded
but Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.this lesson write,it must write ‘…’ and slice()
so I have try dele ‘…’, still can’t work.I’m confused.
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);
but it is say 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…”.
still can’t work.