Skip to content

Instantly share code, notes, and snippets.

View bflannery's full-sized avatar
💭
When in doubt, log it out

Brian bflannery

💭
When in doubt, log it out
View GitHub Profile
@bflannery
bflannery / disEmvowelTrolls.js
Created January 20, 2017 11:48
Remove vowels from the string
// Remove vowels from the string
function disemvowel(str) {
return str.replace(/[aeiou]/gi, '');
}
console.log(disemvowel("This website is for losers LOL!"));
@bflannery
bflannery / rotateLeft.js
Created January 19, 2017 17:03
Rotate the array (first arg) left n (second arg) amount of times
// Rotate the array (first arg) left n (second arg) amount of times
function arrRotate(arr, n) {
let i = 0;
let result = arr.map(function() {
return arr[ (i++ + n) % arr.length ];
});
@bflannery
bflannery / compareValuesOfTwoArrays.js
Created January 15, 2017 16:54
Compare the values of 2 arrays
//Compare the values of 2 arrays.
//If Alice[i] > Bob[i], then Alice is awarded 1 point.
//If Alice[i] < Bob[i] , then Bob is awarded 1 point.
//If Alice[i] = Bob[i], then neither person receives a point.
function compArr(alice , bob){
alicePoints = 0;
bobPoints= 0;
for(i=0; i < 3; i++){
@bflannery
bflannery / sumArr.js
Created January 15, 2017 15:45
Given an array of X integers, can you find the sum of its elements?
//Given an array of X integers, can you find the sum of its elements?
function sumArr(arr) {
let sum = arr.reduce((a,b) => {
return a+b;
}, 0);
return sum;
}
console.log(sumArr([1,2,3,4,10,11]));
console.log(sumArr([2,6,9,15,25,42]));
@bflannery
bflannery / fizzBuzz.js
Created January 2, 2017 17:33
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz"
// Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz
function fizzBuzz(num) {
console.log(num);
for (var i = 0; i <= 100; i++) {
if (i % 5 === 0 && i % 3 === 0 ) {
console.log('fizzbuzz');
} else if (i % 5 === 0) {
console.log('buzz');
} else if (i % 3 === 0) {
@bflannery
bflannery / whereDoIBelong.js
Created December 27, 2016 21:13
Where Do I Belong
// Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
function getIndexToIns(arr, num) {
arr.sort((a,b)=>{
return a - b;
});
for(var i in arr) {
if(arr[i] >= num)
return parseInt(i);
}
@bflannery
bflannery / seekAndDestroy.js
Created December 27, 2016 14:48
Remove all elements from the initial array that are of the same value as these arguments.
// Remove all elements from the initial array that are of the same value as these arguments.
function destroyer(arr) {
let argArr = arr.slice.call(arguments);
argArr.splice(0,1);
return arr.filter((element) => {
return argArr.indexOf(element) === -1;
});
@bflannery
bflannery / falsyBouncer.js
Created December 23, 2016 19:53
Remove all falsy values from an array
// Remove all falsy values from an array.
function bouncer(arr) {
return arr.filter(Boolean);
}
console.log(bouncer([7, "ate", "", false, 9]));
console.log(bouncer([1, null, NaN, 2, undefined]));
console.log(bouncer(["a", "b", "c"]));
console.log(bouncer([false, null, 0, NaN, undefined, ""]));
// Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
function mutation(arr) {
// console.log(arr[1].toLowerCase().split(''))
return arr[1].toLowerCase()
.split('')
.every((letter) => {
// console.log(arr[0].toLowerCase().indexOf(letter));
return arr[0].toLowerCase()
.indexOf(letter) != -1;
// Return the remaining elements of an array after chopping off n elements from the head.
// The head means the beginning of the array, or the zeroth index.
// (non-mutative)
function slasher(arr, howMany) {
return arr.slice(howMany);
}