Bonfire: Truncate a String Solution

Truncate a String

FreeCodeCamp’s Bonfire wants us to truncate a string this time. To truncate something would mean to cut off a string by a certain amount of characters. It’s been a busy few weeks so I haven’t had time until now to dig into this and post it. But here we are, working with strings again.

Truncating isn’t the same thing as abbreviating or creating an acronym. It is, quite literally, you cutting off a string at a certain point in the character count. Truncation occurs on this blog, and many others, as a way of shortening pages and allowing users a small glimpse of the content before they commit to clicking on something to read or view the rest. So let’s see what FreeCodeCamp has given us:

function truncate(str, num) {
  // Clear out that junk in your trunk
  return str;
}

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

The Bonfire gives us a hint that we may have to use the slice() method. We’ve already used slice(), it essentially allows us to take certain parts of a string. If you need a refresher, slice() was discussed in our Title Case a Sentence Bonfire Solution. Let’s talk out our approach.

Approaching the Problem

  1. Truncate() needs to be able to read in the str and num provided by the arguments and use num to determine how much to truncate. We can do this by evaluating the length of str, by using length.
  2. We have to find a way to weed out a couple of caveats, first what happens if num is larger than str? We shouldn’t have to truncate anything in that instance.
  3. Since the Bonfire expects us to add ellipsis at the end of our truncated string, we’ll have to evaluate to make sure that when num is less than or equal to three (or the characters a plain-text ellipses takes up), str gets truncated by num and appended with an ellipses at the end.
  4. Finally, we need to determine how to truncate a string when it is both larger than the num and when num is greater than three. At which point, we should apply slice() to it using num as the argument minus 3 to allow for the ellipses. Then append an ellipses onto the end.

I know that was a little confusing to suss out, so here it is in a step-by-step format:

Step 1: Truncate() takes in str and num as arguments.
Step 2: Truncate() evaluates to see if the total length of str is less than or equal to num. If it is, no truncation needs to take place because str is already too short to truncate. If str evaluates to be less than or equal to num, then str is returned and no truncation takes place.
Step 3: Truncate() evaluates if num is less than or equal to 3. If it is, then str is sliced starting at 0 and using num as an indicator of where to stop slicing. It then adds an ellipses to the end of str and returned.
Step 4: Truncate() assumes that if the previous two conditions are false, it should truncate str by using slice().

The Solution to Truncate a String

function truncate(str, num) {
  
  if (str.length <= num) {
    return str;
  } else if (num <= 3){
    return str.slice(0, num) + '...';
  } else {
    
    return str.slice(0, num - 3) + '...';
  }    
}

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

The above solution is running an if/else if/else statement to check various scenarios and using slice() as a means to truncate the strings that the Bonfire throws at us. It falls into the first if statement that evaluates the length of string, if string is shorter or equal to number, then no truncation is necessary.

The if statement continues into the else if condition where number is evaluated to see if it is less than or equal to 3. If it evaluates to true then string is sliced starting at the 0 position in the string, and ending at a digit equal to num. Then we append the returned string with an ellipses.

Finally, our last else conditional catches all other scenarios where it slices string starting at the 0 point and uses num minus 3 and appends an ellipses at the end. Send that in and it should validate. Hooray, truncation!

Resources

FCC, Bonfire Truncate String Solution
A nice alternative using substring() as an alternative approach to truncate a string. A little less coding involved with this one too.


6 comments on “Bonfire: Truncate a String Solution

  1. Nancy on

    This solution does not pass the first two 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.
    I tried several times but I can’t get it to pass.

    Reply
    • Khanh on

      I ran this in the Bonfire, maybe try this…

      function truncateString(str, num) {
      
        if (str.length <= num) {
          return str;
        } else if (num <= 3){
          return str.slice(0, num) + '...';
        } else {
          return str.slice(0, num - 3) + '...';
      
        }
      
      }
      
      truncateString("A-tisket a-tasket A green and yellow basket", 11);
      Reply
  2. Mark on

    Hello,

    I have followed this tutorial and have written the code as you have written it. I understand what it’s doing. But it is not working in one scenario presented by Bonfire. I looked meticulously for typos and finally cut and pasted the solution in.
    It succeeded on all but this one scenario: truncateString(“Absolutely Longer”, 2) should return “Ab…”.
    I’m being lazy in asking about it so I’ll give it some thought until I hear from you.

    All the best,

    Mark

    Reply
    • Khanh on

      That’s really odd because the solution should still be sound. I don’t think FCC has changed anything since this was posted. Hmm, I just tested it out on FCC five minutes ago and the solution in this post actually validated and passed, even the last one where it says, ‘Absolutely Longer’.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.