Skip to content

Instantly share code, notes, and snippets.

Avatar

Nicki Hastings nickihastings

View GitHub Profile
@nickihastings
nickihastings / inventory-update.js
Created Apr 10, 2018
Compare and update the inventory stored in a 2D array against a second 2D array of a fresh delivery. Update the current existing inventory item quantities (in arr1). If an item cannot be found, add the new item and quantity into the inventory array. The returned inventory array should be in alphabetical order by item.
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]){
@nickihastings
nickihastings / exact-change.js
Created Apr 9, 2018
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument. cid is a 2D array listing available currency. Return the string "Insufficient Funds" if cash-in-drawer is less than the change due. Return the…
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);
@nickihastings
nickihastings / symmetric-difference.js
Last active Apr 6, 2018
Create a function that takes two or more arrays and returns an array of the symmetric difference (△ or ⊕) of the provided arrays. Given two sets (for example set A = {1, 2, 3} and set B = {2, 3, 4}), the mathematical term "symmetric difference" of two sets is the set of elements which are in either of the two sets, but not in both (A △ B = C = {…
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){
@nickihastings
nickihastings / record-collection.js
Created Apr 4, 2018
You are given a JSON object representing a part of your musical album collection. Each album has several properties and a unique id number as its key. Not all albums have complete information. Write a function which takes an album's id (like 2548), a property prop (like "artist" or "tracks"), and a value (like "Addicted to Love") to modify the d…
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"
]
@nickihastings
nickihastings / validate-us-tel-numbers.js
Created Apr 4, 2018
Return true if the passed string is a valid US phone number. The user may fill out the form field any way they choose as long as it is a valid US number. In the comments are examples of valid formats for US numbers (refer to the tests below for other variants):
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);
}
@nickihastings
nickihastings / arguments-optional.js
Created Apr 3, 2018
Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum. For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function. Calling this returned function with a single argument will then return the sum: var sumTwoAnd = ad…
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;
}
@nickihastings
nickihastings / everything-be-true.js
Created Apr 2, 2018
Check if the predicate (second argument) is truthy on all elements of a collection (first argument). Remember, you can access object properties through either dot notation or [] notation.
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 ++;
}
@nickihastings
nickihastings / binary-agents.js
Created Apr 2, 2018
Return an English translated sentence of the passed binary string. The binary string will be space separated.
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.
}
@nickihastings
nickihastings / steamroller.js
Created Mar 31, 2018
Flatten a nested array. You must account for varying levels of nesting.
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
@nickihastings
nickihastings / drop-it.js
Last active Mar 30, 2018
Drop the elements of an array (first argument), starting from the front, until the predicate (second argument) returns true. The second argument, func, is a function you'll use to test the first elements of the array to decide if you should drop it or not. Return the rest of the array, otherwise return an empty array.
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){
You can’t perform that action at this time.