Truncate a String I need help

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.

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

If you wrap the function call (last line of the code) in a console.log() statement, you will see what your function returns

For example, with this function call:

Your function is returning "Peter Pi..." instead of "Peter Piper..."

but I try use return console.log(str);
it can’t work.

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!

still same question,can’t work.I don’t know where wrong?

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.:exploding_head::pensive:

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.

Why are you basing your slicing on whether or not num is less than or greater to 3 or not.? There is nothing in the instructions which requires this.

1 Like

You are returning "A-tis...", as in the dots are counted in the max length, which is not what the challenge is asking

1 Like

I understand,I have solve it. Thanks help.:slightly_smiling_face: