FCC Bonfire Series 111: Truncate a String

Today, we’ll be solving the truncate a string bonfire. In this exercise, we are asked to write a function that takes two arguments: a string of any length, and the maximum number of characters that the string should have after being truncated. The function should return a truncated string, with three dots (…) appended at the end of it. If the given string is already shorter than the given length, simply return the string in it’s original state. We must have a few things in mind first:

  1. The maximum length of the returned string should include the three dots ‘…’ at the end of it.
  2. Since we are appending the dots, we should try fix the result for those cases where the string ends with a space character and the result would look like this: “This is a truncated string …”. It should instead, be converted to “This is a truncated string…”.

Let’s first compare the string length to the given length:

function truncate(string, num) {
  if (string.length <= num) {
    return string;
  } else {
    /* Return truncated string */
  }
}

Let’s evaluate the situation. How do we extract part of a string? We’ve used it before, substr. Well, I’m sorry, but even if substr works perfectly fine in this situation, we’ll be using a new String method, just for the sake of it: slice(). Slice is very similar to substr, we pass it the start position and the end position (the start position being inclusive, and end position exclusive). If we do not provide a second argument, slice will select everything from the start position up to the end of the string. Anyhow, I will not be reinventing the wheel today, here is a great article comparing substr, substring and slice.

We are also going to employ the trim() method. Trim will remove all whitespace characters at the beginning or end of a string. Here’s our finished function:

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

And that’s it! We first shorten our string by using slice, where we give it a zero (0) start position (the beginning of the string) and set the end position to the length given as the second argument minus three (3). We are subtracting three to the end position to account for the dots to be appended at the end. We also use trim to get rid of possible space characters at the end of the string and finally append ‘…’ at the end.

How about we make a version of this function by using the ternary operator?

function truncate(string, num) {
  return string.length <= num ? string : string.slice(0, num - 3).trim() + '...';
}

Neat. This concludes the solution for truncate a string. As always, try to come up with alternative solutions to these problems. Try using substr or substring to solve it, and see if there’s any difference!

 

UPDATE:

Thanks to Alfonso Giron, we now know that FCC has updated truncate a string to require that we pass an additional test: truncate(“A-“, 1)

To account for this change, use one of the following solutions:

function truncate(string, num) {
  if (string.length <= num) {
    return string;
  } else if (string.length <= 4) {
    return string.slice(0, num).trim() + '...'; 
  } else {
    return string.slice(0, num - 3).trim() + '...';
  }
}
function truncate(string, num) {
  return string.length <= num ? string : string.length <= 4 ? string.slice(0, num).trim() + '...' : string.slice(0, num - 3).trim() + '...';
}

 

UPDATE 2 (Dec 12, 2015):

Truncate a string has been changed once again, the following versions should get you passing:

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