Skip to content

Instantly share code, notes, and snippets.

@alexandrebvd
alexandrebvd / Bonfire: Factorialize a Number.txt
Last active August 29, 2015 14:25
4.Bonfire: Factorialize a Number
function factorialize(num) {
if (num === 0) {
return 1;
} else {
for (var i = num; i > 1; i--) {
num *= (i-1);
}
return num;
}
}
@alexandrebvd
alexandrebvd / Reverse a String.txt
Created July 24, 2015 02:55
3.Bonfire: Reverse a String
function reverseString(str) {
str = str.split('').reverse().join('');
return str;
}
reverseString('hello');
@alexandrebvd
alexandrebvd / Return Largest Numbers in Arrays
Created July 24, 2015 15:47
8.Bonfire: Return Largest Numbers in Arrays
function largestOfFour(arr) {
var largestOfFourArray = [];
console.log(typeof(largestOfFourArray));
for (var i in arr) {
var largestOfCurrentArray = arr[i][0];
for (var j in arr[i]) {
if (arr[i][j] > largestOfCurrentArray) {
largestOfCurrentArray = arr[i][j];
largestOfFourArray[i] = largestOfCurrentArray;
} else {
@alexandrebvd
alexandrebvd / Title Case a Sentence.txt
Created July 24, 2015 14:52
7.Bonfire: Title Case a Sentence
function titleCase(str) {
str = str.toLowerCase();
str = str.split(' ');
console.log(str);
for (var i in str) {
str[i] = str[i].charAt(0).toUpperCase() + str[i].substr(1);
console.log(str[i]);
}
str = str.join(' ');
return str;
@alexandrebvd
alexandrebvd / Find the Longest Word in a String.txt
Created July 24, 2015 13:57
6.Bonfire: Find the Longest Word in a String
function findLongestWord(str) {
str = str.split(' ');
var longestWord = '';
console.log(str);
for (var i in str) {
if (str[i].length >= longestWord.length) {
longestWord = str[i];
console.log(longestWord);
}
}
@alexandrebvd
alexandrebvd / Check for Palindromes.txt
Created July 24, 2015 13:26
5.Bonfire: Check for Palindromes
function palindrome(str) {
var rp = /[\s\W]*/; //remove punctuation
var checkPalindrome = str.toLowerCase().split(rp).reverse().join('');
console.log(checkPalindrome);
if (checkPalindrome === str.toLowerCase().split(rp).join('')) {
return true;
} else {
return false;
}
}
@alexandrebvd
alexandrebvd / Confirm the Ending
Created July 24, 2015 19:58
9.Bonfire: Confirm the Ending
function end(str, target) {
var stringLength = str.length;
var targetLength = target.length;
var diffLength = stringLength - targetLength;
if (str.substr(diffLength) === target) {
return true;
} else {
return false;
}
}
@alexandrebvd
alexandrebvd / Repeat a string repeat a string.txt
Last active August 29, 2015 14:25
10.Bonfire: Repeat a string repeat a string
function repeat(str, num) {
if (num <= 0) {
return '';
} else {
var rep = str;
var i = 1;
while (i < num) {
str = str + rep;
i++;
}
@alexandrebvd
alexandrebvd / Mutations
Created July 26, 2015 00:19
14.Bonfire: Mutations
function mutation(arr) {
newArray = [];
newArray[0] = arr[0].toLowerCase();
newArray[1] = arr[1].toLowerCase();
console.log(newArray);
var finalArray = [];
var numberOfLetters = newArray[1].split('').length;
for (var i = 0; i < numberOfLetters; i++) {
finalArray.push(newArray[0].indexOf(newArray[1][i]));
console.log(newArray[0].indexOf(newArray[1][i]));
@alexandrebvd
alexandrebvd / Falsey Bouncer
Created July 26, 2015 00:50
15.Bonfire: Falsey Bouncer
function bouncer(arr) {
//callback function for filter method
function RemoveFalse (value) {
if (value === '' || value === false || value === null || value === 0 || value === undefined) {
return false;
} else {
return true;
}
}
return arr.filter(RemoveFalse);