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;
}
Community
  • 1
  • 1
13aal
  • 1,634
  • 1
  • 21
  • 47
  • 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? – gen_Eric Apr 29 '16 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. – Matt Burland Apr 29 '16 at 18:29
  • @RocketHazmat Because the way this is setup with the first two strings if you `slice(0, num) + '...'` and return the string it returns as `A-tisket a...` but if you subtract three from this, it returns correctly. – 13aal Apr 29 '16 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. – Matt Burland Apr 29 '16 at 18:30
  • @13aal: Are you trying to make sure you don't split a word in half? – gen_Eric Apr 29 '16 at 18:30
  • @MattBurland I added the rules of the project – 13aal Apr 29 '16 at 18:31
  • 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. – timolawl Apr 29 '16 at 18:32
  • @timolawl Example please? – 13aal Apr 29 '16 at 18:33
  • 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 – timolawl Apr 29 '16 at 18:36
  • @timolawl How would that help with the `A-`? If I cut three characters there's nothing there, if I add to the `A-` then it will return `A-...` – 13aal Apr 29 '16 at 18:38
  • 1
    Reread the IF part of my pseudo-code. That answers your question. For your `(A-, 1)` example, you're only truncating by 1, then appending the `...` because the original string length was less than 3. – timolawl Apr 29 '16 at 18:39
  • There is a solution here: https://stackoverflow.com/questions/1301512/truncate-a-string-straight-javascript/52536190#52536190 – Arnaud Anato Sep 27 '18 at 11:58

5 Answers5

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));
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
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).

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Tania Shulga
  • 228
  • 2
  • 8
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..."
Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
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);
Aconic
  • 737
  • 8
  • 4
0

My short solution for this problem.

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