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

4th
Jan 2016
Kyle Kinzalow
@KyleKinz
Jan 04 2016 20:55
hint
CamperBot
@camperbot
Jan 04 2016 20:55

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]

Kyle Kinzalow
@KyleKinz
Jan 04 2016 20:55
hint
CamperBot
@camperbot
Jan 04 2016 20:55

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]

Kyle Kinzalow
@KyleKinz
Jan 04 2016 20:56
hint
CamperBot
@camperbot
Jan 04 2016 20:56

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]

Kyle Kinzalow
@KyleKinz
Jan 04 2016 20:56
hint
CamperBot
@camperbot
Jan 04 2016 20:56

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]

Kyle Kinzalow
@KyleKinz
Jan 04 2016 20:56
hint
CamperBot
@camperbot
Jan 04 2016 20:56

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]

Kyle Kinzalow
@KyleKinz
Jan 04 2016 20:59
hint
CamperBot
@camperbot
Jan 04 2016 20:59

hint [6/7]

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

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

Kyle Kinzalow
@KyleKinz
Jan 04 2016 20:59
hint
CamperBot
@camperbot
Jan 04 2016 20:59

hint [7/7]

Code Solution:

function truncate(str, num) {
  if (str.length <= num)
    return str;

  if (num <= 3)
    return str.substr(0,num) + "...";

  return str.substr(0,num-3) + "...";
}

Code Explanation:

First we need an if-statement to test if the length of the full string passed in as the first argument already fits within the size limit passed in as the second argument. If so we can just return the string that was passed in.

if (str.length <= num)
  return str;

Then we need to check if the num fits into our special case, where the desired length is less than or equal to three. In this case we will return the string trimmed to our target length, followed by "...". We don't need to worry about the "..." being counted against the length of our output string in this case.

if (num <= 3)
  return str.substr(0,num) + "...";

Finally, we write the return for what happens when neither of the previous if-statements have been true. This call to substr() gives us a string that is 3 less than the target length specified by num, which is done so that we have room to add on the "..." and fit within that target.

return str.substr(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 @ltegman for your help with Bonfire: Truncate a String

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]

Kyle Kinzalow
@KyleKinz
Jan 04 2016 21:20
Bonfire: Chunky Monkey
CamperBot
@camperbot
Jan 04 2016 21:20

:fire:Bonfire: Chunky Monkey :link:

function chunk(arr, size) {
  // Break it up.
  return arr;
}

chunk(['a', 'b', 'c', 'd'], 2);

Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a multidimensional array.

more info:  bf details | bf links | hint

Kyle Kinzalow
@KyleKinz
Jan 04 2016 21:20
hint
CamperBot
@camperbot
Jan 04 2016 21:20
These hints depend on people like you! Please add to this :point_right: Bonfire's Wiki Hints Page