DEV Community

Viren B
Viren B

Posted on • Originally published at virenb.cc

Solving "Truncate a String" / freeCodeCamp Algorithm Challenges

Original post can also be found on my website, https://virenb.cc/fcc-008-truncate-a-string

"Truncate a String" on freeCodeCamp

Let's solve freeCodeCamp's Basic Algorithm Scripting Challenge, "Truncate a String"

Our Starter Code (& Tests)

function truncateString(str, num) {
  return str;
}

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

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

Our Instructions

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


Thoughts

  • We have two arguments as inputs, one being a str being a string, num being a number
  • We have to return a string value
  • Will use .length, which is a property on the String object

MDN Reference: String.length

Further Thoughts

Reading this more carefully, we will only have to alter str if num is a greater value.

With this stipulation, we can run an if statement to see if we'll have to do anything. If num is greater than the length of str, we will just return str without any altering.

if (num >= str.length) return str

If the str length is greater than num, we have to alter the str content by removing the extra characters and adding a '...'.

As substring() has been used in prior challenges, it is a little familiar to us. We can definitely use it in this challenge as well.

MDN Reference: String.substring()

The way to use substring() is as follows:

str.substring(indexStart[, indexEnd])

indexEnd being optional

We want to start at the 0 index, looking at the test cases.
The second argument in substring() will be the value of num, as it will give us the index we want to stop the string at. Then we will will add the '...' per the instructions.

Let's look at the first test and see how substring() will work on that.

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

"A-tisket a-tasket A green and yellow basket".substring(0, 8);

We start counting at 0 so we will go to the 7th index, length being 8.
0: 'A'
1: '-'
2: 't'
3: 'i'
4: 's'
5: 'k'
6: 'e'
7: 't'

Substring() action in the console

Here is it being executed in my browser's console just to double check.

The final part is to add '...' at the end of the string.

Here is some pseudo pseudocode:

function truncateString(str, num) {
    if num > str's length
        return str

    return str.substring(0, num) + ...  
}


Solution

[SPOILER: SOLUTION TO CODE BELOW]

    function truncateString(str, num) {
        if (num >= str.length) return str;
        return str.substring(0, num) + "...";
    }

Links & Resources

Repeat a String Repeat a String Challenge on fCC

freeCodeCamp

Donate to FCC!

Solution on my GitHub

Thank you for reading!

Top comments (3)

Collapse
 
bam92 profile image
Abel Lifaefi Mbula

Thank you for sharing Viren.

Keep doing it.

I've written a simillar post, but for DNA pairing. You can find it here

Collapse
 
virenb profile image
Viren B

Thank you for your kind words Abel.
Your post is great, very thorough, going through multiple solutions. Keep up the great writing!

Collapse
 
bam92 profile image
Abel Lifaefi Mbula

Thanks a lot.