0

I'm trying to accomplish the following :

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.

The code I have will only pass test if my num >2, otherwise it fails.

function truncateString(str, num) {
  // Clear out that junk in your trunk
  var trunString = "";
  if (str.length > num) {
  trunString = str.slice(0, (num -3)) + "...";
    return trunString;
  }
  return str;
}

truncateString("A-", 1);

Found a solution for this:

var trunString = "";
  if (str.length > num && num >= 4 ) {
  trunString = str.slice(0, (num - 3)) + "...";
    return trunString;
     }
  else if (str.length > num && num <= 3) {
    trunString = str.slice(0, (num)) + "...";
    return trunString;
  }
7
  • 1
    Note: Instead of 3 dots (3 characters), maybe you can use the ellipsis character itself (single character). Commented Apr 12, 2016 at 10:44
  • Or consider those 3 characters while manipulating..Like jsfiddle.net/rayon_1990/w7to3g5p Commented Apr 12, 2016 at 10:45
  • 1
    It will as you are subtracting 3 from your num. If it is 2 or smaller then 2 will produce either 0 or negative and your slice won't work on negative numbers Commented Apr 12, 2016 at 10:45
  • What is expected output? Why are you doing this (num -3)? Commented Apr 12, 2016 at 10:47
  • the expected output as per the instructions i included in the question is that you must truncate the string and add '...' to the end. without the -3 you would not be accounting for the three dots... Commented Apr 12, 2016 at 10:54

3 Answers 3

2

You could use a conditional (ternary) operator ?:

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

console.log(truncateString("Abcdefghijk", 5));
console.log(truncateString("A-", 1));
console.log(truncateString("Alpha", 5));
console.log(truncateString("Beta", 5));
console.log(truncateString("Epsilon", 3));

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

4 Comments

Up voted as your solutions are always elegant code. Yet, the question is a bit unclear. It seems OP wants to always add "..." to the output and allow length > n when n < 3.
If you run this code it does not work. Alpha and Beta for not have ellipses.
should 'Alpha' have dots like 'Al...' and 'Beta' become 'Be...' or 'B...'?
nice suggestion. Actually, there is no need to pass the second ternary operator. It will work also with one pair of conditions str.length > num ? str.slice(0, num) + '...' : str;
0
function truncateString(str, num) {
  // clear out that junk in your trunk
  var trunString = '';
  if (str.length && str.length > num) {
    trunString = str.slice(0, num - 1) + '&hellip;';
    return trunString;
  }
  return str;
}

truncateString("A-", 1);

P.S.: read also @techfoobar comment, using &hellip HTML entity will help...

1 Comment

My answer assumes the string length is the number of rendered chars, ("&hellip;".length === 1), and not the lenght of the string in javascript. Hope it fills your requirements...
-1

This is my solution, hoping to help you

   function truncate(str, num) {
      // Clear out that junk in your trunk
      var newStr;
      if(num>3&&num<str.length){
        newStr=str.slice(0,num-3)+"...";
      }else if(num>=str.length){
        newStr=str;
      }else{
        newStr=str.slice(0,num)+"...";
      }
      return newStr;
    }

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

1 Comment

Add some explanation of your answer rather than posting the code only.

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.