Skip to content

Instantly share code, notes, and snippets.

// 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;
}
// 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: 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: 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: 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: Falsy Bouncer
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-falsy-bouncer?solution=function%20bouncer(arr)%20%7B%0A%20%20%2F%2F%20Don%27t%20show%20a%20false%20ID%20to%20this%20bouncer.%0A%20%20return%20arr.filter(function(elem)%7B%0A%20%20%20%20return%20(elem)%3B%0A%20%20%7D)%3B%0A%7D%0A%0Abouncer(%5B7%2C%20%22ate%22%2C%20%22%22%2C%20false%2C%209%5D)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function bouncer(arr) {
// Don't show a false ID to this bouncer.
// Cool way to do it as 0, null, false, "", NaN, undefined etc just return false on their own!
return arr.filter(function(elem){ return (elem) }); }
// Bonfire: Diff Two Arrays
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-diff-two-arrays?solution=function%20diff(arr1%2C%20arr2)%20%7B%0A%20%20var%20newArr%20%3D%20arr1.concat(arr2)%3B%0A%20%20return%20newArr.filter(function(elem%2C%20index)%7B%0A%20%20%20%20%20return%20newArr.lastIndexOf(elem)%20%3D%3D%3D%20index%20%26%26%20newArr.indexOf(elem)%20%3D%3D%3D%20index%3B%0A%20%20%7D)%3B%0A%7D
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function diff(arr1, arr2) {
var newArr = arr1.concat(arr2); // Maybe this could be incorporated inside the filter function? I don't know how!
return newArr.filter(function(elem, index){
return newArr.lastIndexOf(elem) === index && newArr.indexOf(elem) === index;
});
// Bonfire: Roman Numeral Converter
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-roman-numeral-converter?solution=%2F%2F%20Flow%20for%20conversion%3A%20http%3A%2F%2Fwww.rapidtables.com%2Fconvert%2Fnumber%2Fhow-number-to-roman-numerals.htm%20%0Avar%20vals%20%3D%20%7B1%20%3A%20%27I%27%2C%204%3A%20%27IV%27%2C%205%3A%20%27V%27%2C%209%3A%20%27IX%27%2C%2010%3A%20%27X%27%2C%2040%3A%20%27XL%27%2C%2050%3A%20%27L%27%2C%2090%3A%20%27XC%27%2C%20100%3A%20%27C%27%2C%20400%3A%20%27CD%27%2C%20500%3A%20%27D%27%2C%20900%3A%20%27CM%27%2C%201000%3A%20%27M%27%7D%3B%0Afunction%20convert(n)%7B%0A%09var%20roman%20%3D%20%22%22%3B%0A%09while(n%3E0)%7B%0A%09%09%2F%2F%20find%20highest%20decimal%0A%09%09var%20v%20%3D%20findHighest(n)%3B%0A%09%09%2F%2F%20write%20roman%20numeral%0A%09%09roman%20%2B%3D%20vals%5Bv%5D%3B%0A%09%09%2F%2F%20subtract%20highest%20decimal%20from%20total%20number%0A%09%09n%20-%3D%20v%3B%0A%09%7D%0A%09return%20roman%3B%0A%7D%0A%0Afunction%20findHighest(n)%7B%0A%09var%20decimals%20
// Bonfire: Search and Replace
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-search-and-replace?solution=function%20myReplace(str%2C%20before%2C%20after)%20%7B%0A%09return%20str.replace(new%20RegExp(before)%2C%20(before.charCodeAt(0)%3E96%20%3F%20after%5B0%5D.toLowerCase()%20%3A%20after%5B0%5D.toUpperCase())%20%2B%20after.slice(1))%3B%0A%7D%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
// Comments:
// Ahhh, one liners make me happy :)
function myReplace(str, before, after) {
return str.replace(new RegExp(before), (before.charCodeAt(0)>96 ? after[0].toLowerCase() : after[0].toUpperCase()) + after.slice(1));
}
// Bonfire: DNA Pairing
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-dna-pairing?solution=function%20pair(str)%20%7B%0A%20%20return%20str.split(%27%27).map(function(e)%7B%0A%20%20%20%20switch(e)%7B%0A%20%20%20%20%20%20%20case%20%22C%22%3A%20return%20%5B%22C%22%2C%20%22G%22%5D%3B%0A%20%20%20%20%20%20%20case%20%22G%22%3A%20return%20%5B%22G%22%2C%20%22C%22%5D%3B%0A%20%20%20%20%20%20%20case%20%22A%22%3A%20return%20%5B%22A%22%20%2C%22T%22%5D%3B%0A%20%20%20%20%20%20%20case%20%22T%22%3A%20return%20%5B%22T%22%2C%20%22A%22%5D%3B%0A%20%20%20%20%7D%0A%20%20%7D)%3B%20%20%0A%7D%0A%0Apair(%22GCG%22)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
// Comments:
// Once i'd figured out what this was asking it wasn't so bad. Enabled to remove some lines by using .map to replace the str
function pair(str) {
return str.split('').map(function(e){
switch(e){
case "C": return ["C", "G"];