Solving "Truncate a String" / freeCodeCamp Algorithm Challenges 
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-05-20 20:27:07

---
title: Solving "Truncate a String" / freeCodeCamp Algorithm Challenges 
published: true
description: My guide, notes, and solution to freeCodeCamp's basic algorithm challenge, "Truncate a String" 
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-008-truncate-a-string
series: Solving freeCodeCamp's Algorithm Challenges
---

Original post can also be found on my website, [https://virenb.cc/fcc-008-truncate-a-string] (https://virenb.cc/fcc-008-truncate-a-string "Post on my website")

!["Truncate a String" on freeCodeCamp](//images.ctfassets.net/yzwpq1epaq68/7NG1i0slxdUU9Y3tJ2fIZ/78822d893733e1db0d7301c70c1c48f9/ss-008.png)

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

## Our Starter Code (& Tests)
```javascript
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length "Mozilla Docs for 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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring "Mozilla Docs for 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](//images.ctfassets.net/yzwpq1epaq68/5mE3ysgbeIAxIt1P0FqpAe/65b4f709149dc56a93baa538b8028655/ss-008-02.png)

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

<details>
  <summary>**[SPOILER: SOLUTION TO CODE BELOW]**</summary>
 	
  ```javascript
	function truncateString(str, num) {
  		if (num >= str.length) return str;
  		return str.substring(0, num) + "...";
	}
  ```
 
</details>

---

### Links & Resources

[Repeat a String Repeat a String Challenge on fCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string "Basic Algorithm Scripting: Truncate a String Repeat a String Challenge on fCC Website")

[freeCodeCamp](https://www.freecodecamp.org/, "freeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "fCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/008---truncate-a-string "GitHub Solution")

Thank you for reading!