Skip to content

Instantly share code, notes, and snippets.

@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 / 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);
@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 / Seek and Destroy
Created July 27, 2015 04:29
17.Bonfire: Seek and Destroy
function destroyer(arr) {
var args = Array.prototype.slice.apply(arguments).slice(1);
for (var i in arr) {
for (var j in args) {
if (arr[i] === args[j]) {
arr.splice(i, 1);
}
}
}
return arr;
@alexandrebvd
alexandrebvd / Where do I belong
Created July 27, 2015 15:51
18.Bonfire: Where do I belong
function where(arr, num) {
newArr = arr.sort();
finalArr = [];
for (var i = 0; i < newArr.length; i++) {
if (num > newArr[i] && num < newArr[i+1]) {
finalArr.push(newArr[i]);
finalArr.push(num);
} else {
finalArr.push(newArr[i]);
}
@alexandrebvd
alexandrebvd / Sum All Numbers in a Range
Created July 27, 2015 19:17
19.Bonfire: Sum All Numbers in a Range
/*function sumAll(arr) {
function getMaxOfArray(numArray) {
return Math.max.apply(null, numArray);
}
function getMinOfArray(numArray) {
return Math.min.apply(null, numArray);
}
var max = getMaxOfArray(arr);
var min = getMinOfArray(arr);
var n = max - min +1; //quantity of numbers to be added.
@alexandrebvd
alexandrebvd / Diff Two Arrays
Created August 2, 2015 20:46
20.Bonfire: Diff Two Arrays
function diff(arr1, arr2) {
function getDiff(arr1, arr2) {
var newArr = [];
for (var i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) === -1) {
newArr.push(arr2[i]);
}
}
return newArr;
@alexandrebvd
alexandrebvd / Roman Numeral Converter
Created August 2, 2015 21:44
21.Bonfire: Roman Numeral Converter
function convert(num) {
//I V X L C D M
var roman = '';
var counter = num;
while (counter !== 0) {
console.log(counter);
if (counter >= 1000) {
for (var i = 0; i < Math.floor(counter/1000); i++) {
@alexandrebvd
alexandrebvd / Search and Replace
Created August 5, 2015 13:46
22.Bonfire: Search and Replace
function replace(str, before, after) {
var newStr = str.replace(before, after);
if (before[0] === before[0].toUpperCase()) {
var afterArray = after.split('');
afterArray.splice(0, 1, after[0].toUpperCase());
var newAfter = afterArray.join('');
console.log(newAfter);
newStr = str.replace(before, newAfter);