# Truncate a string - help

**URL:** <https://forum.freecodecamp.org/t/truncate-a-string-help/313795>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [September 11, 2019, 8:00pm UTC](https://forum.freecodecamp.org/t/truncate-a-string-help/313795 "2019-09-11T20:00:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Owais93](https://avatars.discourse-cdn.com/v4/letter/o/958977/32.png) [@Owais93](https://forum.freecodecamp.org/u/Owais93)\
**Post date:** [September 11, 2019, 8:00pm UTC](https://forum.freecodecamp.org/t/truncate-a-string-help/313795/1 "2019-09-11T20:00:39Z")

</div>

```javascript
function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length > num && num > 3) {
    return str.slice(0, (num - 3)) + '...';
  } else if (str.length > num && num <= 3) {
    return str.slice(0, num) + '...';
  } else {
    return str;
  }

}

```

After doing google many times and think about the pseudo code,  
I am unable to understand the code logic to truncate a string.  
why we are assigning num \> 3 why 3 and num -1

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [September 11, 2019, 8:07pm UTC](https://forum.freecodecamp.org/t/truncate-a-string-help/313795/2 "2019-09-11T20:07:16Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See [this post](http://forum.freecodecamp.com/t/forum-code-formatting/25574) to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

**Note:** Backticks are not single quotes.

![markdown_Forums](https://sea3.discourse-cdn.com/freecodecamp/uploads/default/original/3X/4/b/4b9a26f0c243380dd84935f4e29b92e3ecee8d05.gif)

---

<div class="post-metadata">

**Author:** ![Owais93](https://avatars.discourse-cdn.com/v4/letter/o/958977/32.png) [@Owais93](https://forum.freecodecamp.org/u/Owais93)\
**Post date:** [September 11, 2019, 8:39pm UTC](https://forum.freecodecamp.org/t/truncate-a-string-help/313795/3 "2019-09-11T20:39:08Z")

</div>

Any senior who can help me?

---

<div class="post-metadata">

**Author:** ![enrider](https://avatars.discourse-cdn.com/v4/letter/e/2bfe46/32.png) [@enrider](https://forum.freecodecamp.org/u/enrider)\
**Post date:** [September 26, 2019, 4:01pm UTC](https://forum.freecodecamp.org/t/truncate-a-string-help/313795/4 "2019-09-26T16:01:14Z")

</div>

@Owais93 I recommend reading through this thread: [Basic Algo Scripting: Truncate a String](http://forum.freecodecamp.org/t/basic-algo-scripting-truncate-a-string/316128/2)

Essentially, the solution that you’re drawing from isn’t up to date. That `num > 3` is reflective of an older, more difficult challenge where you had to include the ‘…’ as a part of the string length.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 25, 2021, 12:15pm UTC](https://forum.freecodecamp.org/t/truncate-a-string-help/313795/5 "2021-01-25T12:15:40Z")

</div>

Closed on January 25, 2021, 12:15pm UTC


