This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Where do I belong | |
// Author: @elliotfriend | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-where-do-i-belong?solution=function%20where(arr%2C%20num)%20%7B%0A%20%20%2F%2F%20Find%20my%20place%20in%20this%20sorted%20array.%0A%20%20var%20newArray%20%3D%20arr.concat(num)%3B%0A%20%20return%20newArray.sort(function(a%2C%20b)%20%7B%0A%20%20%20%20return%20a%20-%20b%3B%0A%20%20%7D).indexOf(num)%3B%0A%7D%0A%0Awhere(%5B40%2C%2060%5D%2C%2050)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function where(arr, num) { | |
// Find my place in this sorted array. | |
var newArray = arr.concat(num); | |
return newArray.sort(function(a, b) { | |
return a - b; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Seek and Destroy | |
// Author: @elliotfriend | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-seek-and-destroy?solution=function%20destroyer(arr)%20%7B%0A%20%20%2F%2F%20Remove%20all%20the%20values%0A%20%20var%20args%20%3D%20%5B%5D.slice.call(arguments)%3B%20args.shift()%3B%0A%20%20var%20destroyedArray%20%3D%20%5B%5D%3B%0A%20%20arr.filter(function(val)%20%7B%0A%20%20%20%20if%20(args.indexOf(val)%20%3D%3D%3D%20-1)%20%7B%0A%20%20%20%20%20%20destroyedArray.push(val)%3B%0A%20%20%20%20%7D%0A%20%20%7D)%3B%0A%0A%20%20return%20destroyedArray%3B%0A%7D%0A%0Adestroyer(%5B1%2C%202%2C%203%2C%201%2C%202%2C%203%5D%2C%202%2C%203)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function destroyer(arr) { | |
// Remove all the values | |
var args = [].slice.call(arguments); args.shift(); | |
var destroyedArray = []; | |
arr.filter(function(val) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Falsy Bouncer | |
// Author: @elliotfriend | |
// 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(val)%20%7B%0A%20%20%20%20return%20Boolean(val)%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. | |
return arr.filter(function(val) { | |
return Boolean(val); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Mutations | |
// Author: @elliotfriend | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-mutations?solution=function%20mutation(arr)%20%7B%0A%20%20var%20indexLocation%20%3D%200%3B%0A%20%20var%20targetArray%20%3D%20arr%5B0%5D.toLowerCase().split(%22%22)%3B%0A%20%20var%20goalArray%20%3D%20arr%5B1%5D.toLowerCase().split(%22%22)%3B%0A%20%20while%20(goalArray.length%20%3E%200)%20%7B%0A%20%20%20%20indexLocation%20%3D%20targetArray.indexOf(goalArray%5B0%5D)%3B%0A%20%20%20%20if%20(indexLocation%20!%3D%3D%20-1)%20%7B%0A%20%20%20%20%20%20goalArray.shift()%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%20return%20true%3B%0A%7D%0A%0Amutation(%5B%22hello%22%2C%20%22mhey%22%5D)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function mutation(arr) { | |
var indexLocation = 0; | |
var targetArray = arr[0].toLowerCase().split(""); | |
var goalArray = arr[1].toLowerCase().split(""); | |
while (goalArray.length > 0) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Slasher Flick | |
// Author: @elliotfriend | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-slasher-flick?solution=function%20slasher(arr%2C%20howMany)%20%7B%0A%20%20%2F%2F%20it%20doesn%27t%20always%20pay%20to%20be%20first%0A%20%20return%20arr.splice(howMany)%3B%0A%7D%0A%0Aslasher(%5B1%2C%202%2C%203%5D%2C%202)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function slasher(arr, howMany) { | |
// it doesn't always pay to be first | |
return arr.splice(howMany); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Chunky Monkey | |
// Author: @elliotfriend | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-chunky-monkey?solution=function%20chunk(arr%2C%20size)%20%7B%0A%20%20%2F%2F%20Break%20it%20up.%0A%20%20var%20multiArray%20%3D%20%5B%5D%3B%0A%20%20var%20tempArray%20%3D%20%5B%5D%3B%0A%20%20while%20(arr.length%20%3E%200)%20%7B%0A%20%20%20%20for%20(var%20i%20%3D%200%3B%20i%20%3C%20size%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20if%20(arr.length%20%3E%200)%20%7B%0A%20%20%20%20%20%20%20%20tempArray.push(arr%5B0%5D)%3B%0A%20%20%20%20%20%20%20%20arr.shift()%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20multiArray.push(tempArray)%3B%0A%20%20%20%20tempArray%20%3D%20%5B%5D%3B%0A%20%20%7D%0A%20%20return%20multiArray%3B%0A%7D%0A%0A%2F%2Fchunk(%5B%22a%22%2C%20%22b%22%2C%20%22c%22%2C%20%22d%22%5D%2C%202)%3B%0Achunk(%5B0%2C%201%2C%202%2C%203%2C%204%2C%205%5D%2C%204)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function chunk(arr, size) { | |
// Break it up. | |
var multiArray = []; | |
var |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Truncate a string | |
// Author: @elliotfriend | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-truncate-a-string?solution=function%20truncate(str%2C%20num)%20%7B%0A%20%20%2F%2F%20Clear%20out%20that%20junk%20in%20your%20trunk%0A%20%20var%20maxLength%20%3D%20num%3B%0A%20%20if%20(num%20%3E%203)%20%7B%20maxLength%20%3D%20num%20-%203%3B%20%7D%0A%20%20if%20(num%20%3E%3D%20str.length)%20%7B%0A%20%20%20%20return%20str%3B%0A%20%20%7D%20else%20%7B%0A%20%20%20%20return%20str.slice(0%2C%20maxLength)%20%2B%20%22...%22%3B%0A%20%20%7D%0A%7D%0A%0Atruncate(%22A-tisket%20a-tasket%20A%20green%20and%20yellow%20basket%22%2C%2011)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function truncate(str, num) { | |
// Clear out that junk in your trunk | |
var maxLength = num; | |
if (num > 3) { maxLength = num - 3; } | |
if (num >= str.length) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Repeat a string repeat a string | |
// Author: @elliotfriend | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-repeat-a-string-repeat-a-string?solution=function%20repeat(str%2C%20num)%20%7B%0A%20%20%2F%2F%20repeat%20after%20me%0A%20%20if%20(num%20%3C%200)%20%7B%0A%20%20%20%20return%20%22%22%3B%0A%20%20%7D%20else%20%7B%0A%20%20%20%20return%20str.repeat(num)%3B%0A%20%20%7D%0A%7D%0A%0Arepeat(%22abc%22%2C%203)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function repeat(str, num) { | |
// repeat after me | |
if (num < 0) { | |
return ""; | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Confirm the Ending | |
// Author: @elliotfriend | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-confirm-the-ending | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function end(str, target) { | |
// "Never give up and good luck will find you." | |
// -- Falcor | |
if (str.substr(target.length * -1) === target) { | |
return true; |