Skip to content

Instantly share code, notes, and snippets.

View EmaaLJN's full-sized avatar

Emanuel Luján EmaaLJN

  • Argentina
View GitHub Profile
// Bonfire: Where do I belong
// Author: @emaaljn
// Challenge: http://www.freecodecamp.com/challenges/bonfire-where-do-i-belong?solution=function%20where(arr%2C%20num)%20%7B%0A%20%0A%20%20%2F%2F%20sort%20array.%0A%20%20arr.sort(function(a%2C%20b)%7B%0A%20%20%20%20return%20a%20-%20b%3B%0A%20%20%7D)%3B%0A%20%20%0A%20%20var%20counter%20%3D%200%3B%0A%20%20for(var%20i%20%3D%200%3B%20i%20%3C%20arr.length%3B%20i%2B%2B)%7B%0A%20%20%20%20if(arr%5Bi%5D%20%3C%20num)%7B%0A%20%20%20%20%20%20counter%20%2B%3D%201%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%20%0A%20%20return%20counter%3B%0A%7D%0A%0Awhere(%5B10%2C%2020%2C%2030%2C%2040%2C%2050%5D%2C%2030)%3B
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function where(arr, num) {
// sort array.
arr.sort(function(a, b){
return a - b;
// Bonfire: Seek and Destroy
// Author: @emaaljn
// Challenge: http://www.freecodecamp.com/challenges/bonfire-seek-and-destroy?solution=function%20destroyer(arr)%20%7B%0A%20%20%0A%20%20var%20args%20%3D%20Array.prototype.slice.call(arguments)%3B%0A%20%20%0A%20%20args.splice(0%2C%201)%3B%0A%20%20%0A%20%20return%20arr.filter(function(val)%7B%0A%0A%20%20%20%20%20%20%20%20for(var%20i%20%3D%200%3B%20i%20%3C%20args.length%3B%20i%2B%2B)%7B%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if(args%5Bi%5D%20!%3D%3D%20val)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%7D)%3B%0A%20%20%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) {
var args = Array.prototy
// Bonfire: Falsy Bouncer
// Author: @emaaljn
// Challenge: http://www.freecodecamp.com/challenges/bonfire-falsy-bouncer?solution=function%20bouncer(arr)%20%7B%0A%0A%20%20return%20arr.filter(function(value)%7B%0A%20%20%20%20%20%20return%20Boolean(value)%3B%0A%20%20%7D)%3B%0A%7D%0A%0Abouncer(%5Bfalse%2C%20null%2C%200%2C%20NaN%2C%20undefined%2C%20%22%22%5D)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function bouncer(arr) {
return arr.filter(function(value){
return Boolean(value);
});
// Bonfire: Mutations
// Author: @emaaljn
// Challenge: http://www.freecodecamp.com/challenges/bonfire-mutations?solution=function%20mutation(arr)%20%7B%0A%20%20%2F%2F%20return%20bool%0A%20%20return%20arr.reduce(function(haystack%2C%20needle)%7B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20var%20state%20%3D%20true%3B%0A%20%20%20%20%20%20var%20hayStaInCase%20%3D%20haystack.toLowerCase()%3B%0A%20%20%20%20%20%20var%20needleInCase%20%3D%20needle.toLowerCase()%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20for(var%20i%20%3D%200%3B%20i%20%3C%20needle.length%3B%20i%2B%2B)%7B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20if(hayStaInCase.indexOf(needleInCase%5Bi%5D)%20!%3D%3D%20-1)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%20%20%20%20%20%20%20%20%7Delse%7B%0A%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20state%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%
// Bonfire: Slasher Flick
// Author: @emaaljn
// Challenge: http://www.freecodecamp.com/challenges/bonfire-slasher-flick?solution=function%20slasher(arr%2C%20howMany)%20%7B%0A%20%20%0A%20%20if(howMany%20!%3D%3D%200)%0A%20%20%20%20return%20arr.splice(howMany%2C%20howMany)%3B%0A%20%20%0A%20%20return%20arr%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) {
if(howMany !== 0)
return arr.splice(howMany, howMany);
// Bonfire: Chunky Monkey
// Author: @emaaljn
// Challenge: http://www.freecodecamp.com/challenges/bonfire-chunky-monkey?solution=function%20chunk(arr%2C%20size)%20%7B%0A%20%20%0A%20%20var%20arrayContainer%20%3D%20%5B%5D%3B%0A%20%20%0A%20%20var%20divider%20%3D%20Math.ceil(arr.length%20%2F%20size)%3B%0A%20%20%0A%20%20for(var%20i%20%3D%200%3B%20i%20%3C%20divider%3B%20i%2B%2B)%7B%0A%20%20%20%20%0A%20%20%20%20var%20arrayCContent%20%3D%20%5B%5D%3B%0A%20%20%20%20%0A%20%20%20%20for(var%20x%20%3D%200%3B%20x%20%3C%20size%3B%20x%2B%2B)%7B%0A%20%20%20%20%20%20if(i%20%3E%200)%7B%0A%20%20%20%20%20%20%20%20if(arr%5B(x%20%2B%20size)%5D%20!%3D%3D%20undefined)%0A%20%20%20%20%20%20%20%20%20%20arrayCContent.push(arr%5Bx%20%2B%20size%20*%20i%5D)%3B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%7Delse%7B%0A%20%20%20%20%20%20%20%20arrayCContent.push(arr%5Bx%5D)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20arrayContainer.push(arrayCContent)%3B%0A%20%20%7D%0A%20%0A%20%20return%20arrayContainer%3B%0A%7D%0A%0Achunk(%5B
// Bonfire: Factorialize a Number
// Author: @emaaljn
// Challenge: http://www.freecodecamp.com/challenges/bonfire-factorialize-a-number?solution=function%20factorialize(num)%20%7B%0A%20%20%0A%20%20%2F%2F%20!0%20%3D%201%0A%20%20if(num%20%3D%3D%3D%200)%7B%0A%20%20%20%20%0A%20%20%20%20return%201%3B%0A%20%20%7D%0A%20%20else%7B%0A%20%20%20%20%0A%20%20%20%20return%20num%20*%20factorialize(num%20-%201)%3B%0A%20%20%7D%0A%7D%0A%0Afactorialize(5)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function factorialize(num) {
// !0 = 1
if(num === 0){
// Bonfire: Check for Palindromes
// Author: @emaaljn
// Challenge: http://www.freecodecamp.com/challenges/bonfire-check-for-palindromes?solution=function%20palindrome(str)%20%7B%0A%20%20%0A%20%20var%20delimiter%20%3D%20%2F(%5Ba-zA-Z0-9%5D)%2Fgi%3B%0A%20%20%0A%20%20var%20firstPhrase%20%3D%20str.toLowerCase()%0A%20%20%20%20.match(delimiter)%3B%0A%20%20%0A%20%20%0A%20%20%2F%2F%20I%20can%27t%20use%20Array.reverse()%20because%20it%20changes%20the%20value%20by%20reference.%0A%20%20var%20secondPhrase%20%3D%20%5B%5D%3B%0A%20%20%0A%20%20for(var%20reverse%20%3D%20firstPhrase.length%20-%201%20%3B%20reverse%20%3E%3D%200%3B%20reverse%20--)%7B%0A%20%20%20%20%20%20secondPhrase.push(firstPhrase%5Breverse%5D)%3B%0A%20%20%7D%0A%20%20%0A%20%20%2F%2F%20due%20to%20both%20of%20them%20share%20the%20same%20length%0A%20%20for(var%20i%20%3D%200%3B%20i%20%3C%20firstPhrase.length%3B%20i%2B%2B)%7B%0A%20%20%20%20if(firstPhrase%5Bi%5D%20!%3D%3D%20secondPhrase%5Bi%5D)%0A%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%20%20%0A%20%20%7D%0
// Bonfire: Find the Longest Word in a String
// Author: @emaaljn
// Challenge: http://www.freecodecamp.com/challenges/bonfire-find-the-longest-word-in-a-string?solution=function%20findLongestWord(str)%20%7B%0A%20%20%0A%20%20var%20phrases%20%3D%20str.split(%22%20%22)%3B%0A%0A%20%20var%20phraseLength%20%3D%20phrases.map(function(val)%7B%0A%20%20%20%20return%20val.length%3B%0A%20%20%7D)%3B%0A%0A%20%20return%20phraseLength.reduce(function(prev%2C%20current)%7B%0A%20%20%20%20%20%20if(prev%20%3E%20current)%0A%20%20%20%20%20%20%20%20return%20prev%3B%0A%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20return%20current%3B%0A%20%20%7D%2C%200)%3B%0A%20%20%0A%7D%0A%0AfindLongestWord(%22May%20the%20force%20be%20with%20you%22)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function findLongestWord(str) {
var phrases = str.split(" ");
var phraseLength = phrases.map(function(val){
// Bonfire: Title Case a Sentence
// Author: @emaaljn
// Challenge: http://www.freecodecamp.com/challenges/bonfire-title-case-a-sentence?solution=function%20titleCase(str)%20%7B%0A%20%20var%20phrases%20%3D%20str.toLowerCase().split(%27%20%27)%3B%20%20%0A%20%20%0A%20%20%0A%20%20var%20phrasesParse%20%3D%20phrases.map(function(val)%7B%0A%20%20%20%20%20%20var%20newRw%20%3D%20%5B%5D%3B%0A%20%20%20%20%20%20for(var%20index%20%3D%200%3B%20index%20%3C%20val.length%3B%20index%2B%2B)%7B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20newRw.push(val%5Bindex%5D)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20return%20newRw%3B%0A%20%20%7D)%3B%0A%20%20%0A%20%20var%20textUcFirst%20%3D%20%27%27%3B%0A%20%20for(index%20%3D%200%3B%20index%20%3C%20phrasesParse.length%3B%20index%2B%2B)%7B%0A%20%20%20%20%20%20var%20ucFirst%20%3D%20phrasesParse%5Bindex%5D.shift().toUpperCase()%3B%0A%20%20%20%20%0A%20%20%20%20%20%20phrasesParse%5Bindex%5D.unshift(ucFirst)%3B%0A%20%20%20%20%20%20textUcFirst%20%2B%3D%20phra