Skip to content

Instantly share code, notes, and snippets.

Created December 11, 2015 06:28

Revisions

  1. @invalid-email-address Anonymous created this gist Dec 11, 2015.
    15 changes: 15 additions & 0 deletions bonfire-truncate-a-string.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    // Bonfire: Truncate a string
    // Author: @kkas
    // Challenge: http://www.freecodecamp.com/challenges/bonfire-truncate-a-string
    // Learn to Code at Free Code Camp (www.freecodecamp.com)

    function truncate(str, num) {
    // Clear out that junk in your trunk
    var len = num > 3 ? num - 3 : num;

    return str.length > num ? str.slice(0, len) + "..." : str;
    }

    //truncate("A-tisket a-tasket A green and yellow basket", 11);
    //truncate("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length);
    truncate("A-", 1);