Skip to content

Instantly share code, notes, and snippets.

// Bonfire: Sum All Primes
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-sum-all-primes#
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function sumPrimes(num) {
var primes = [];
for(var i=2; i<=num; i++){
if(isPrime(i)) primes.push(i);
}
// Bonfire: Sum All Odd Fibonacci Numbers
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-sum-all-odd-fibonacci-numbers#
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function sumFibs(num) {
return fib(num).reduce(function(a,b){
return b % 2 ? a + b : a;
});
}
// Bonfire: Spinal Tap Case
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-spinal-tap-case
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
return str.replace(/([a-z][A-Z])|\s|_/g, function(match){
return match.length === 2 ? match[0] + "-" + match[1] : "-";
// Bonfire: Sorted Union
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-sorted-union?solution=function%20unite()%20%7B%0A%20%20%2F%2FImportant%3A%20You%20should%20not%20slice%20on%20arguments%20because%20it%20prevents%20optimizations%20in%20JavaScript%20engines%20(V8%20for%20example).%20Instead%2C%20try%20constructing%20a%20new%20array%20by%20iterating%20through%20the%20arguments%20object%0A%20%20args%20%3D%20new%20Array(arguments.length)%3B%0A%20%20for(var%20i%3D0%3B%20i%3Carguments.length%3B%20i%2B%2B)%7B%0A%20%20%20%20args%5Bi%5D%20%3D%20arguments%5Bi%5D%3B%0A%20%20%7D%0A%20%20args%20%3D%20args.reduce(function(a%2Cb)%7B%0A%20%20%20%20return%20a.concat(b)%3B%0A%20%20%7D)%3B%0A%20%20return%20args.filter(function(a%2Ci)%7B%0A%20%20%20%20return%20args.indexOf(a)%20%3D%3D%20i%3B%0A%20%20%7D)%3B%0A%7D%0A%0A%0A%0A%0Aunite(%5B1%2C%203%2C%202%5D%2C%20%5B5%2C%202%2C%201%2C%204%5D%2C%20%5B2%2C%201%5D)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function unite()
// Bonfire: Missing letters
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-missing-letters?solution=function%20fearNotLetter(str)%20%7B%0A%20str%20%3D%20str.split(%27%27).map(function(c%2C%20i)%7B%0A%20%20%20%20%20%20return%20(c.charCodeAt()%20!%3D%20str.charCodeAt()%20%2B%20i)%20%3F%20String.fromCharCode(str.charCodeAt()%2Bi)%20%3A%20%27undefined%27%3B%0A%20%7D)%3B%0A%20return%20str.lastIndexOf(%27undefined%27)%20%3F%20str%5Bstr.lastIndexOf(%27undefined%27)%2B1%5D%20%3A%20%27undefined%27%3B%0A%20%09%0A%7D
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function fearNotLetter(str) {
str = str.split('').map(function(c, i){
return (c.charCodeAt() != str.charCodeAt() + i) ? String.fromCharCode(str.charCodeAt()+i) : 'undefined';
});
return str.lastIndexOf('undefined') ? str[str.lastIndexOf('undefined')+1] : 'undefined';
// 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"];
// 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: 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: 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: 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) }); }