Skip to content

Instantly share code, notes, and snippets.

// Bonfire: Truncate a string
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-truncate-a-string?solution=function%20truncate(str%2C%20num)%20%7B%0A%20%20%2F%2F%20Totally%20unclear!%0A%20%20%20%20return%20str.length%20%3E%20num%20%3F%20num%20%3C%3D%203%20%3F%20str.slice(0%2C%20num)%20%2B%20%22...%22%20%3A%20str.slice(0%2C%20num%20-3)%20%2B%20%22...%22%20%3A%20str%3B%0A%7D%0A%0Atruncate(%22Absolutely%20Longer%22%2C%202)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function truncate(str, num) {
// Totally unclear!
return str.length > num ? num <= 3 ? str.slice(0, num) + "..." : str.slice(0, num -3) + "..." : str;
}
// Bonfire: Title Case a Sentence
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-title-case-a-sentence?solution=function%20titleCase(str)%20%7B%0A%20%20return%20str.toLowerCase().split(%27%20%27).map(function(word)%7B%0A%20%20%20%20return%20word.charAt(0).toUpperCase()%20%2B%20word.substring(1%2C%20word.length)%3B%0A%20%20%7D).join(%27%20%27)%3B%0A%7D%0A%0AtitleCase(%22I%27m%20a%20little%20tea%20pot%22)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function titleCase(str) {
return str.toLowerCase().split(' ').map(function(word){
return word.charAt(0).toUpperCase() + word.substring(1, word.length);
}).join(' ');
}
// Bonfire: Find the Longest Word in a String
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-find-the-longest-word-in-a-string?solution=function%20findLongestWord(str)%20%7B%0A%20%20return%20str.split(%27%20%27).sort(function(a%2Cb)%7B%0A%20%20%20%20return%20a.length%20%3C%20b.length%3B%0A%20%20%7D)%5B0%5D.length%3B%0A%7D%0A%0AfindLongestWord(%22The%20quick%20brown%20fox%20jumped%20over%20the%20lazy%20dog%22)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function findLongestWord(str) {
return str.split(' ').sort(function(a,b){
return a.length < b.length;
})[0].length;
}
// Bonfire: Check for Palindromes
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-check-for-palindromes?solution=function%20palindrome(str)%20%7B%0A%20%20%2F%2F%20Good%20luck!%0A%20%20str%20%3D%20stripandlower(str)%3B%0A%20%20return%20str%20%3D%3D%20reverse(str)%3B%0A%7D%0A%0Afunction%20reverse(str)%7B%0A%20%20return%20str.split(%27%27).reverse().join(%27%27)%3B%0A%7D%0A%0Afunction%20stripandlower(str)%7B%0A%20%20return%20str.replace(%2F%5B%5EA-Za-z0-9%5D%2Fg%2C%20%27%27).toLowerCase()%3B%0A%7D%0A%0A%0Apalindrome(%22eye%22)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function palindrome(str) {
// Good luck!
str = stripandlower(str);
return str == reverse(str);
}
// Bonfire: Factorialize a Number
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-factorialize-a-number?solution=%2F%2F%20Recursive%20Factorialize%20example%0Afunction%20factorialize(num)%20%7B%0A%20%20if(num%20%3D%3D%201%20%7C%7C%20num%20%3D%3D%3D%200)%7B%0A%20%20%20%20return%201%3B%0A%20%20%7D%0A%20%20var%20result%20%3D%20factorialize(num-1)%20*%20num%3B%0A%20%20return%20result%3B%0A%7D%0A%0Afactorialize(5)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
// Recursive Factorialize example
function factorialize(num) {
if(num == 1 || num === 0){
return 1;
}