Skip to content

Instantly share code, notes, and snippets.

@alexandrebvd
alexandrebvd / Where art thou
Created July 27, 2015 03:44
16.Bonfire: Where art thou
function where(collection, source) {
var arr = [];
var findKeys = Object.keys(source);
for (var i = 0; i < collection.length; i++) {
var currentObj = collection[i];
for (var j = 0; j < findKeys.length; j++) {
var key = findKeys[j];
if (currentObj.hasOwnProperty(key) && currentObj[key] === source[key]) {
arr.push(currentObj);
@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);
@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 / Slasher Flick
Created July 25, 2015 23:28
13.Bonfire: Slasher Flick
function slasher(arr, howMany) {
if (howMany >= arr.length) {
return [];
} else if (howMany === 0) {
return arr;
} else {
arr.splice (0, howMany);
return arr;
}
}
@alexandrebvd
alexandrebvd / Chunky Monkey
Created July 25, 2015 23:18
12.Bonfire: Chunky Monkey
function chunk(arr, size) {
var count = 0;
var newArray = [];
var finalArray = [];
while (count < arr.length) {
newArray = arr.slice(count, count+size);
finalArray.push(newArray);
count += size;
}
return finalArray;
@alexandrebvd
alexandrebvd / Truncate a string
Created July 25, 2015 21:56
11.Bonfire: Truncate a string
function truncate(str, num) {
if (str.length > num) {
str = str.slice(0, num-3) + '...';
}
return str;
}
truncate('A-tisket a-tasket A green and yellow basket', 11);
@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 / 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 / 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;