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 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 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 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 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 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 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 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 steamrollArray(arr) { | |
// I'm a steamroller, baby | |
var result = []; //store the resulting flat array | |
//function to recursively flatten arrays inside of arrays. | |
function flatten(arr){ | |
//for every item in the array check if it is an array itself | |
for(var i = 0; i < arr.length; i++){ | |
if(Array.isArray(arr[i]) ){ | |
//if it is an array call the flatten function and concatinate the results |
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 binaryAgent(str) { | |
var arr = str.split(' '); | |
//var words = []; | |
for(var i =0; i<arr.length; i++){ | |
arr[i] = parseInt(arr[i], 2); | |
arr[i] = String.fromCharCode(arr[i]); | |
//can also write the above as: | |
//words.push(String.fromCharCode(parseInt(arr[i], 2))); | |
//parses first then converts to letter then pushes to array. | |
} |