# Turncate a string feedback

**URL:** <https://forum.freecodecamp.org/t/turncate-a-string-feedback/63321>\
**Category:** General\
**Created:** [December 8, 2016, 4:07pm UTC](https://forum.freecodecamp.org/t/turncate-a-string-feedback/63321 "2016-12-08T16:07:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tgralak](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tgralak/32/113258_2.png) [@Tgralak](https://forum.freecodecamp.org/u/Tgralak)\
**Post date:** [December 8, 2016, 4:07pm UTC](https://forum.freecodecamp.org/t/turncate-a-string-feedback/63321/1 "2016-12-08T16:07:47Z")

</div>

Hello guys! Would you like to check my code? It passes the tests, but im not sure if it’s, like, globally effective! Thanks for feedback.

```auto
function truncateString(str, num) {
  if (str.length > num && num>4) {
    var result = str.slice(-str.length, num-3)+ "...";
    return result;
  } else if (num <4) {
    var result2 = str.slice(-str.length, num)+"...";
    return result2;
  } else if (num >= str.length && num >4) {
    return str;
  } 
}

truncateString("Absolutely Longer", 2);

```

---

<div class="post-metadata">

**Author:** ![Flopet17](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/flopet17/32/34902_2.png) [@Flopet17](https://forum.freecodecamp.org/u/Flopet17)\
**Post date:** [December 8, 2016, 4:59pm UTC](https://forum.freecodecamp.org/t/turncate-a-string-feedback/63321/2 "2016-12-08T16:59:02Z")

</div>

I’ll show you my version for it. Probably there is room from improvement here also! 🙂

```
function truncateString(str, num) {
  if(str.length > num && num > 3)
    return str.substr(0, num-3) + "...";
  else if (num < 3)
    return str.substr(0, num) + "...";
  else
    return str;
}

truncateString("Absolutely Longer", 2);
```

---

<div class="post-metadata">

**Author:** ![Tgralak](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tgralak/32/113258_2.png) [@Tgralak](https://forum.freecodecamp.org/u/Tgralak)\
**Post date:** [December 8, 2016, 7:09pm UTC](https://forum.freecodecamp.org/t/turncate-a-string-feedback/63321/3 "2016-12-08T19:09:38Z")

</div>

Ok, so we were thinking almost the same way - i guess it’s right then 🙂

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [December 8, 2016, 8:35pm UTC](https://forum.freecodecamp.org/t/turncate-a-string-feedback/63321/4 "2016-12-08T20:35:01Z")

</div>

If the code passes the tests everything is fine. Code works, you are done.

But anyway, I would like to show off a little 🤓

You don’t need the last `else if` statement.  
You don’t have to create `result` variables.

> **Code**
>
> ```auto
> function truncateString(str, num) {
> if (str.length > num && num>4) {
> return str.slice(-str.length, num-3)+ "...";
> } else if (num <4) {
> return str.slice(-str.length, num)+"...";
> } 
> return str;
> }
> 
> ```

Now we see that both `return` statements are almost the same. Lets move the return statement outside the `if`s and in `if` statements we will set the parts that change:

> **Code**
>
> ```auto
> function truncateString(str, num) {
> var cut = 0,
> dots = '';
> if (str.length > num && num>4) {
> cut = 3;
> dots = '...';
> } else if (num <4) {
> dots = '...';
> } 
> return str.slice(-str.length, num - cut) + dots;
> }
> 
> ```

Well, we have both times set `dots` to `'...'`. Actually we need dots only if we change the input string, and we change the input string if it is longer than `num`. So if string is less or equal to `num` we can instantly return:

> **Code**
>
> ```auto
> function truncateString(str, num) {
> if (str.length <= num) return str;
> var cut = 0;
> if (num>4) {
> cut = 3;
> } else if (num <4) {
> } 
> return str.slice(-str.length, num - cut) + '...';
> }
> 
> ```

Now we have an empty `else`, let’s throw it away:

> **Code**
>
> ```auto
> function truncateString(str, num) {
> if (str.length <= num) return str;
> var cut = 0;
> if (num>4) {
> cut = 3;
> }
> return str.slice(-str.length, num - cut) + '...';
> }
> 
> ```

Then there is such thing as ternary operator, you can write `if...else` in one line: :

> **Code**
>
> ```auto
> function truncateString(str, num) {
> if (str.length <= num) return str;
> var cut = num > 4 ? 3 : 0;
> return str.slice(-str.length, num - cut) + '...';
> }
> 
> ```

And so on…  
Btw, `-str.length` is `0`

> **:nerd:**
>
> ```auto
> function truncateString(str, num) {
> return str.length <= num ? str : str.slice(0, num <= 3 ? num : num - 3) + '...';
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Flopet17](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/flopet17/32/34902_2.png) [@Flopet17](https://forum.freecodecamp.org/u/Flopet17)\
**Post date:** [December 8, 2016, 8:38pm UTC](https://forum.freecodecamp.org/t/turncate-a-string-feedback/63321/5 "2016-12-08T20:38:29Z")

</div>

@jenovs Love the way you cut it down step by step! 😀 Very nice!

---

<div class="post-metadata">

**Author:** ![drewfabrics](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/drewfabrics/32/35076_2.png) [@drewfabrics](https://forum.freecodecamp.org/u/drewfabrics)\
**Post date:** [January 31, 2017, 3:39am UTC](https://forum.freecodecamp.org/t/turncate-a-string-feedback/63321/6 "2017-01-31T03:39:59Z")

</div>

golden explanation sir 👊

---

<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 16, 2021, 1:08pm UTC](https://forum.freecodecamp.org/t/turncate-a-string-feedback/63321/7 "2021-01-16T13:08:22Z")

</div>

Closed on January 16, 2021, 1:08pm UTC


