This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dropElements(arr, func) { | |
// Drop them elements. | |
//shift() removes the element if it returns false | |
//so loop over the array, test against the function | |
//if it returns false remove the element, otherwise | |
//return the array. | |
while(arr.length > 0){ | |
var check = func(arr[0]); | |
if(!check){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findElement(arr, func) { | |
var num = 0; | |
//filter the array using the provided function, this will keep anything that returns true in an array; | |
num = arr.filter(func); | |
//return the first element in the new array. | |
return num[0]; | |
} | |
findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function smallestCommons(arr) { | |
//sort the array ascending, so that a is always the smaller number | |
arr.sort(function(a,b){ | |
return a-b; | |
}); | |
//first find the GCD of the pair of numbers using the Euclidean Algorithm. | |
//return the GCD | |
function gcd(a, b){ | |
var x = a; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sumPrimes(num) { | |
var ints = [2]; //array to hold prime numbers | |
//function to test if a number is a prime | |
function isPrime(test){ | |
for(var i = 2; i < test; i++){ | |
if(test % i == 0){ //if there's no remainder it's not a prime | |
return false; //not a prime number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sumFibs(num) { | |
//set up variables a counter to hold the next Fibonacci number and an | |
//array to hold all the Fibonacci numbers below num. | |
var count = 0; | |
var fib = [1, 1]; | |
//loop through and keep adding Fibonacci numbers to the array until the num is reached | |
for(var i = 0; i<fib.length; i++){ | |
count = fib[i] + fib[i+1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function spinalCase(str) { | |
// "It's such a fine line between stupid, and clever." | |
// --David St. Hubbins | |
function processStr(match, offset, string){ | |
//if the offset is not the first letter and there isn't already a hyphen, | |
//add a hyphen then change the letter to lowercase | |
if(offset !== 0 && string[offset-1] !== '-'){ | |
return '-' + match.toLowerCase(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function convertHTML(str) { | |
// :) | |
//create an object to store the html conversions | |
var htmlEntities = { | |
'&' : '&', | |
'<' : '<', | |
'>' : '>', | |
'"' : '"', | |
"'" : ''' | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function uniteUnique(arr) { | |
var args = []; | |
//loop over the arguments provided and then loop over each array | |
//within the arguments, check if the value is already in the new | |
//array, if it's not, add it in. | |
for(var i = 0; i < arguments.length; i++){ | |
for(var j = 0; j<arguments[i].length; j++){ | |
if(args.indexOf(arguments[i][j]) == -1){ | |
args.push(arguments[i][j]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function booWho(bool) { | |
// What is the new fad diet for ghost developers? The Boolean. | |
//check if the value 'bool' is strictly true or false, this means the value is a Boolean Primitive and should return true. | |
//Everything else should return false. | |
if(bool === true || bool === false){ | |
return true; | |
} | |
else { | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fearNotLetter(str) { | |
//in unicode A=65 and Z=90, a=97 and z=122 | |
//get the starting letter's unicode, and then add one to check the next letter | |
var start = str.charCodeAt(0) + 1; | |
var missing = ''; //store missing letters | |
//loop over the string, get the letters unicode value and compare it to what the next letter's code should be: | |
for(var i = 1; i < str.length; i++){ | |
//if the codes don't match append the letter to the variable | |
if(str.charCodeAt(i) !== start){ |