1

I'm working on a project for freecodecamp.

Rules of the project:

Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.

Note that inserting the three dots to the end will add to the string length.

However, if the given maximum string length num is less than or equal to 3, then the addition of the three dots does not add to the string length in determining the truncated string.

My test strings include:

truncateString("A-tisket a-tasket A green and yellow basket", 11) should return "A-tisket...".

truncateString("Peter Piper picked a peck of pickled peppers", 14) should return "Peter Piper...".

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

truncateString("A-", 1) should return "A...".

truncateString("Absolutely Longer", 2) should return "Ab...".

Now I've gotten most of this figured out using a ternary operator, so it's fairly simple, except the last two ('A-', 1) and ('Absolutely Longer', 2), my question being, how can I accomplish the truncating successfully and return the expected output of the last two strings?

Source:

function truncateString(str, num) {
  return str.length > num ? str.slice(0, num - 3) + '...' : str;
}
12
  • 1
    You are doing num - 3 on a string ("A-") of length 2 . Why are you doing num-3? What is the purpose of your truncateString supposed to be? Commented Apr 29, 2016 at 18:27
  • 1
    Your question would be a lot clearer if you actually explained what truncateString is actually supposed to do. Instead of having us guess the rules from the input and expected outputs. Commented Apr 29, 2016 at 18:29
  • 1
    @RocketHazmat: I think the purpose is to replace the last three characters with ... if the string is longer than the requested num. But they've neglected the edge case of the string being shorter than 3. Or num being less than 3. Commented Apr 29, 2016 at 18:30
  • 1
    So what you'll need to do is add conditional logic based on the last requirement of the project. Can't just num - 3 every string. Commented Apr 29, 2016 at 18:32
  • 2
    you can accomplish it with an if/else statement. IF string length is less than 3, just append the ... after truncation, ELSE cut off three more characters then append the ... after truncation. Something like that Commented Apr 29, 2016 at 18:36

5 Answers 5

3

You need to handle the case where num is less than or equal to 3. You can just add another condition to do this:

function truncateString(str, num) {
  return str.length > num ? str.slice(0, num >= 3 ? num - 3 : num) + '...' : str;
}

console.log(truncateString("A-tisket a-tasket A green and yellow basket", 11));
console.log(truncateString("Peter Piper picked a peck of pickled peppers", 14));
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length));
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2));
console.log(truncateString("A-", 1));
console.log(truncateString("Absolutely Longer", 2));

Sign up to request clarification or add additional context in comments.

4 Comments

truncateString("A-", 1) should return "A...".
Yes I did, according to the website, it doesn't work
@13aal: It does work. Open your console and run the snippet.
First of all I'd like to apologize for what I just said, I put a ! instead of a ?, my bad, this is exactly what I was looking for, thank you.
3

I noticed that the task was slightly changed in 2018: Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.

Solution for that is:

function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length > num) { 
    return str.slice(0, num) + '...';
  } else {
    return str; 
  }

}
truncateString("A-tisket a-tasket A green and yellow basket", 8);

Explanation: First we check if the number of letters in the string is bigger than a given number. If so, we cut the first part of the string out and add '...' to it. We cut the given number of letters out beginning with the very first letter (0).

Comments

0

Something like this:

function truncateString(str, num) {
  var result = str;

  if(str.length - 3 > num) {
    result = str.length > num ? str.slice(0, num - 3) + '...' : str;
  }
  return result;
}

truncateString("A-", 1)
// "A-"

truncateString("A-tisket a-tasket A green and yellow basket", 11)
// "A-tisket..."

1 Comment

truncateString("A-", 1) should return "A...".
0

This would help. Not perfect but working and supposed to be easy for beginners understanding

function truncateString(str, num) {
        var res = "";
        if (str.length > num) {
          if (num > 3) {
            res = str.slice(0, num - 3) + "...";
          } else {
            res = str.slice(0, num) + "...";
          }
          return res;
        }
        return str;

      }

      truncateString("A-tisket a-tasket A green and yellow basket", 11);

Comments

0

My short solution for this problem.

const truncateString = (str, num) => str.length > num ? 
                                     str.substr(0, num) + "..." : 
                                     str;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.