Skip to content

Instantly share code, notes, and snippets.

View nickihastings's full-sized avatar
🏠
Working from home

Nicki Hastings nickihastings

🏠
Working from home
View GitHub Profile
@nickihastings
nickihastings / everything-be-true.js
Created April 2, 2018 17:32
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.
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 / arguments-optional.js
Created April 3, 2018 06:54
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…
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 / validate-us-tel-numbers.js
Created April 4, 2018 18:48
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):
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 / record-collection.js
Created April 4, 2018 20:40
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…
// Setup
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
@nickihastings
nickihastings / symmetric-difference.js
Last active November 10, 2022 20:17
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 = {…
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 / exact-change.js
Last active November 27, 2021 15:20
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…
/* UPDATED NOVEMBER 2021 to work with the latest FreeCodeCamp tests
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.
The checkCashRegister() function should always return an object with a status key and a change key.
Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return
the exact change.
Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.
Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order,
as the value of the change key.
@nickihastings
nickihastings / inventory-update.js
Created April 10, 2018 17:52
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.
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 / no-repeats-please.js
Created April 15, 2018 14:55
Return the number of total permutations of the provided string that don't have repeated consecutive letters. Assume that all characters in the provided string are each unique. For example, aab should return 2 because it has 6 total permutations (aab, aab, aba, aba, baa, baa), but only 2 of them (aba and aba) don't have the same letter (in this c…
function permAlone(str) {
//check if the string provided is only one character
if(str.length === 1){
return 1;
}
//function to create the permutations
//for each value in the string, add the permutations of the rest
function getPermutations(str){
var output = [];
@nickihastings
nickihastings / make-a-person.js
Created April 16, 2018 18:29
Fill in the object constructor with the following methods below: getFirstName() getLastName() getFullName() setFirstName(first) setLastName(last) setFullName(firstAndLast) Run the tests to see the expected output for each method. The methods that take an argument must accept only one argument and it has to be a string. These methods must be the …
var Person = function(firstAndLast) {
var arr = firstAndLast.split(' ');
var firstName = arr[0];
var lastName = arr[1];
// Complete the method below and implement the others similarly
this.getFullName = function() {
return firstName + ' ' + lastName;
};
this.getFirstName = function() {
@nickihastings
nickihastings / map-the-debris.js
Created April 17, 2018 07:03
Return a new array that transforms the element's average altitude into their orbital periods. The array will contain objects in the format {name: 'name', avgAlt: avgAlt}. You can read about orbital periods on wikipedia. The values should be rounded to the nearest whole number. The body being orbited is Earth. The radius of the earth is 6367.4447…
function orbitalPeriod(arr) {
var GM = 398600.4418;
var earthRadius = 6367.4447;
//create a function to calculate orbital period
function calcPeriod(avgAlt){
var orbitalRadius = earthRadius + avgAlt;
//calculation is 2 multiplied by pi multiplied by the square root of the
//orbital radius to the power of 3 divided by the GM
var period = Math.round(2*Math.PI*(Math.sqrt(Math.pow(orbitalRadius,3)/GM)));
return period;