View inventory-update.js
function updateInventory(arr1, arr2) { | |
// All inventory must be accounted for or you're fired! | |
//check the items in arr2 against those in arr1 | |
//if the item is found update the value and set found to true. | |
//if the item is not found add it to arr1 | |
for(var i = 0; i < arr2.length; i++){ //loop over array 2 | |
var found = false; | |
for(var j = 0; j<arr1.length; j++){ //loop over array 1 | |
if(arr2[i][1] === arr1[j][1]){ |
View exact-change.js
function checkCashRegister(price, cash, cid) { | |
//change all decimals to whole numbers for ease | |
//First work out how much change is needed | |
var change = (cash * 100) - (price * 100); | |
//Next work out how much cash there is in the cash register using | |
//reduce to add all the values and turn to whole numbers | |
var totalCash = cid.reduce(function(accum, current){ | |
accum = accum + (current[1]*100); |
View symmetric-difference.js
function sym(args) { | |
//Put the arguments into an array | |
var arrArgs = Array.prototype.slice.call(arguments); | |
//Function to compare two arrays by reducing each one in | |
//comparison with the other. Check if the item in first array | |
//is also in the second array, if it isn't push it to the accumulator. | |
//then repeat the other way around. | |
function compareArrays(arr1, arr2){ |
View record-collection.js
// Setup | |
var collection = { | |
"2548": { | |
"album": "Slippery When Wet", | |
"artist": "Bon Jovi", | |
"tracks": [ | |
"Let It Rock", | |
"You Give Love a Bad Name" | |
] |
View validate-us-tel-numbers.js
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); | |
} |
View arguments-optional.js
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; | |
} |
View everything-be-true.js
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 ++; | |
} |
View binary-agents.js
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. | |
} |
View steamroller.js
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 |
View drop-it.js
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){ |