These are chat archives for camperbot/bonfire-truncate-a-string

19th
Sep 2015
lauratalaat
@lauratalaat
Sep 19 2015 11:21
hint
CamperBot
@camperbot
Sep 19 2015 11:21

hint [1/7]

## :construction: After this are possible spoiler hints.

Make sure you've tried to hard to solve it yourself before proceeding. :construction:

type hint for next hint :pencil: [Contribute at the FCC Wiki]

lauratalaat
@lauratalaat
Sep 19 2015 11:21
hint
CamperBot
@camperbot
Sep 19 2015 11:21

hint [2/7]

# Explanation:

We need to reduce the length of the string or truncate it if it is longer than the given maximum lengths specified and add ... to the end. If it is not that long then we keep it as is.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

lauratalaat
@lauratalaat
Sep 19 2015 11:21
hint
CamperBot
@camperbot
Sep 19 2015 11:21

hint [3/7]

Hint: 1

Strings are immutable in JavaScript so we will need a new variable to store the truncated string.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

lauratalaat
@lauratalaat
Sep 19 2015 11:21
hint
CamperBot
@camperbot
Sep 19 2015 11:21

hint [4/7]

Hint: 2

You will need to use slice and specify where to start and where to stop.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

lauratalaat
@lauratalaat
Sep 19 2015 11:21
hint
CamperBot
@camperbot
Sep 19 2015 11:21

hint [5/7]

Hint: 3

Do not forget that when we truncate the word, we also must count the length added by ...

type hint for next hint :pencil: [Contribute at the FCC Wiki]

lauratalaat
@lauratalaat
Sep 19 2015 11:21
hint
CamperBot
@camperbot
Sep 19 2015 11:21

hint [6/7]

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

type hint for next hint :pencil: [Contribute at the FCC Wiki]

lauratalaat
@lauratalaat
Sep 19 2015 11:21
hint
CamperBot
@camperbot
Sep 19 2015 11:21

hint [7/7]

Code Solution:

function truncate(str, num) {
  var truncd = '';
  if (str.length > num) {
    truncd = str.slice(0, num - 3) + '...';
    return truncd;
  }

  return str;
}

Code Explanation:

First we need a conditional if statement to test if the length of the full string passed in as the first argument is greater than the maximum passed in as the second argument.

if (str.length > num)

Then we need to slice the full string at the length required (second argument). The trick is to minus the length of the ... that needs to be added to get the final truncated string.

str = str.slice(0, num-3)

Finally, just add the ... to the end of the sliced substring ready to be returned

str = str.slice(0, num-3) + '...';

Credits:

If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 @richyvk

NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Ali-Haider5
@Ali-Haider5
Sep 19 2015 13:46
hint
CamperBot
@camperbot
Sep 19 2015 13:46
These hints depend on people like you! Please add to this :point_right: Bonfire's Wiki Hints Page