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
// Setup | |
var collection = { | |
"2548": { | |
"album": "Slippery When Wet", | |
"artist": "Bon Jovi", | |
"tracks": [ | |
"Let It Rock", | |
"You Give Love a Bad Name" | |
] |
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 telephoneCheck(str) { | |
// Good luck! | |
var regex = /(^1(?=(\s\d{3}\s?-?\d{3}\s?-?\d{4}$)|(\s?\(\d{3}\)\s?-?\d{3}\s?-?\d{4}$)))|(^\((?=\d{3}\)\s?-?\d{3}\s?-?\d{4}$))|(^\d{3}(?=\s?-?\d{3}\s?\-?\d{4}$))/; | |
return regex.test(str); | |
} | |
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 addTogether() { | |
//create a function that test whether arguments are numbers | |
//if not a number return undefined, if it is return the number | |
function checkArgs(val){ | |
if(typeof val !== 'number'){ | |
return undefined; | |
} | |
return val; | |
} |
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 truthCheck(collection, pre) { | |
// Is everyone being true? | |
//set a variable to count the truthies | |
var truthy = 0; | |
//loop over the array and test for true | |
//if it's true add one to truth | |
for(var i = 0; i<collection.length; i++){ | |
if(collection[i][pre]){ | |
truthy ++; | |
} |
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. | |
} |
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 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 |