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

22nd
Aug 2015
Damon Anderson
@CoderNurse
Aug 22 2015 02:08
hint
CamperBot
@camperbot
Aug 22 2015 02:08

hint [1/5]

## :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]

Damon Anderson
@CoderNurse
Aug 22 2015 02:09
hint
CamperBot
@camperbot
Aug 22 2015 02:09

hint [2/5]

# 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.

Note that the three dots at the end add to the string length.

Tests final code needs to pass

  1. expect(truncate('A-tisket a-tasket A green and yellow basket', 11)).to.eqls('A-tisket...');
  2. expect(truncate('Peter Piper picked a peck of pickled peppers', 14)).to.eqls('Peter Piper...');
  3. assert(truncate('A-tisket a-tasket A green and yellow basket', 'A-tisket a-tasket A green and yellow basket'.length) === 'A-tisket a-tasket A green and yellow basket', 'should not truncate if string is = length');
  4. assert.strictEqual(truncate('A-tisket a-tasket A green and yellow basket', 'A-tisket a-tasket A green and yellow basket'.length + 2), 'A-tisket a-tasket A green and yellow basket', 'should not truncate if string is < length');

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

Brian Wieland
@bwieland86
Aug 22 2015 02:10
hint
CamperBot
@camperbot
Aug 22 2015 02:10

hint [3/5]

Hint

Truncate the string to a length equal to the second parameter passed to the function. Remember the added ... is included in the final string length.

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

Damon Anderson
@CoderNurse
Aug 22 2015 02:10
hint
CamperBot
@camperbot
Aug 22 2015 02:10

hint [4/5]

My final code

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

truncate('A-tisket a-tasket A green and yellow basket', 11);

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

Brian Wieland
@bwieland86
Aug 22 2015 02:10
how do you tell it which item to give a hint on?
Damon Anderson
@CoderNurse
Aug 22 2015 02:14
@bwieland86 I'm just getting the hang of this too. First, type "bf" plus "the name of the bonfire"
then your following "hint" commands will show hints for that bonfire
so for instance, I type bf truncate a string
but not from this room
do it from the help room
Damon Anderson
@CoderNurse
Aug 22 2015 03:07
hint
CamperBot
@camperbot
Aug 22 2015 03:07

hint [5/5]

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) + '...';

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