Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

104 changes: 100 additions & 4 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// A local community center is holding a fund rasising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.
// A local community center is holding a fund rasising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities.
//Each business has assigned a representative to attend the event along with a small donation.

// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.

Expand Down Expand Up @@ -53,31 +54,126 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
{"id":49,"first_name":"Bel","last_name":"Alway","email":"[email protected]","shirt_size":"S","company_name":"Voolia","donation":107},
{"id":50,"first_name":"Shell","last_name":"Baine","email":"[email protected]","shirt_size":"M","company_name":"Gabtype","donation":171}];


// ==== Challenge 1: Use .forEach() ====
// The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names into a new array called fullName.
let fullName = [];
runners.forEach( function(runner){
fullName.push(runner.first_name+ ' ' + runner.last_name);
});
console.log(fullName);

// ==== Challenge 2: Use .map() ====
// The event director needs to have all the runner's first names converted to uppercase because the director BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result
let allCaps = [];
allCaps = runners.map(function(runner){
return runner.first_name.toUpperCase();
});
console.log(allCaps);

// ==== Challenge 3: Use .filter() ====
// The large shirts won't be available for the event due to an ordering issue. Get a list of runners with large sized shirts so they can choose a different size. Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result
// The large shirts won't be available for the event due to an ordering issue. Get a list of runners with large sized shirts so they can choose a different size.
// Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result
let largeShirts = [];
runners.filter(function(runner){
if(runner.shirt_size === 'L'){
largeShirts.push(runner);
}
});
console.log(largeShirts);

// ==== Challenge 4: Use .reduce() ====
// The donations need to be tallied up and reported for tax purposes. Add up all the donations into a ticketPriceTotal array and log the result
let ticketPriceTotal = [];
ticketPriceTotal = runners.reduce(function(total, runner){
return total + runner.donation;
},0);
console.log(ticketPriceTotal);


// ==== Challenge 5: Be Creative ====
// Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential problems you could solve given the data set and the 5k fun run theme. Try to create and then solve 3 unique problems using one or many of the array methods listed above.
// Now that you have used .forEach(), .map(), .filter(), and .reduce().
// I want you to think of potential problems you could solve given the data set and the 5k fun run theme.
// Try to create and then solve 3 unique problems using one or many of the array methods listed above.

// Problem 1
// What fields do we have availble for the runners? Create an array with the keys that represent each object.
// This will help us to see clearly what kind of data we have available for further examination
let keySample = [];
keySample = Object.keys(runners[0]);
console.log(keySample);

// Now that you have that cleared out, let's focus on donations.
// Create an array that tells us how many donations we had in the following ranges:
// 0 - 50
// 51 - 100
// 101 - 150
// > 150
// Log results
let donationRanges = [0, 0, 0, 0];

// ES5 format:
// for(let i = 0; i < runners.length; i++){
// if(runners[i].donation <= 50){
// donationRanges[0] += 1;
// }
// else if (runners[i].donation <= 100){
// donationRanges[1] += 1;
// }
// else if (runners[i].donation <= 150){
// donationRanges[2] += 1;
// }
// else {
// donationRanges[3] += 1;
// }
// }

// ES6 format
runners.forEach(function(runner){
switch (true) {
case (runner.donation <= 50):
donationRanges[0] += 1;
break;
case (runner.donation <= 100):
donationRanges[1] += 1;
break;
case (runner.donation <= 150):
donationRanges[2] += 1;
break;
case (runner.donation > 151):
donationRanges[3] += 1;
break;

default: console.log('Invalid entry');
}
});

console.log('Number of donations in the range of 0 - $50: ' + donationRanges[0]);
console.log('Number of donations in the range of $51 - $100: ' + donationRanges[1]);
console.log('Number of donations in the range of $101 - $150: ' + donationRanges[2]);
console.log('Number of donations greater than $150: ' + donationRanges[3]);

// Problem 2
// We are very grateful and want to send thank you cards to the representatives that assisted to the run.
// Create an array with all emails to send the cards.

let emailsArray = [];
emailsArray = runners.map(runner => runner.email);

console.log('emails '+ emailsArray)

// Problem 3
// The team in charge of communicating to the runners that chose size L on their shirts that there was
// and ordering issue with their shirts, will be sending an automated email with current updates about their order.
// Use the largeShirts array of object and create an array containing only the emails
// of the partipants whose order had issues.

let emailAffectedRunners = [];
emailAffectedRunners = largeShirts.map(runner => runner.email);
console.log(emailAffectedRunners);






// Problem 3
76 changes: 75 additions & 1 deletion assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Create a callback function and invoke the function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems.
// Create a callback function and invoke the function to test your work.
//You have been provided an example of a problem and a solution to see how
//this works with our items array. Study both the problem and the solution
//to figure out the rest of the problems.

const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

Expand All @@ -24,29 +27,100 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
return cb(arr.length);
}

function giveLength(length){
console.log('Array length: ' + length);
}

getLength(items, giveLength);


function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr[arr.length - 1])
}

function getLast(lastItem){
console.log('Last item of the array: ' + lastItem);
}

last(items, getLast);

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x, y);
}

sumNums(2, 3, function(num1, num2){
//Number() used here to avoid string concatenation when using +
console.log('Sum of two numbers: '+ Number(num1 + num2));
});

function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
return cb(x, y);
}

multiplyNums(2, 3, function (num1, num2){
console.log('Multiplication of two numbers: '+ num1 * num2);
})

function contains(item, list, cb) {
// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
return cb(item, list);
}

function containItem(unit, collection){
let found = 0;
for(let i = 0; i< collection.length; i++){
if(unit === collection[i])
{ found++ }
}

if (found > 0)
{ console.log(true);}
else
{ console.log(false);}
}

contains("Gum", items, containItem);

/* STRETCH PROBLEM */

// my example
const arrayContainsDuplicate = ['A', 'B', 'A', 'C', 'B'];

function removeDuplicates(array, cb) {
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
return cb(array);
}

function findDuplicate(arrayOfItems){
//start at zero
//if same position, skip and go to next array position
// compare: if same, remove element with splice(position, 1)
//if not, go to next position
let arrayCopy = arrayOfItems;
for(let i = 0; i < arrayOfItems.length; i ++){ //outer loop
for(let j = 0; j < arrayOfItems.length; j++){ //inner loop
if(i === j){
/*skip same position*/}
else{
if (arrayOfItems[i] === arrayOfItems[j]){
arrayCopy.splice(arrayOfItems[i], 1);
}
}
}//inner loop
}//outer loop
console.log(arrayCopy);
}

removeDuplicates(arrayContainsDuplicate, findDuplicate);



56 changes: 55 additions & 1 deletion assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,74 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

const globalVariable = -1;

const grandParent = () => {
console.log('Inside grandParent');
const grandParentVariable = 0;
console.log('globalVariable from inside of grandParent: '+globalVariable);
// functionParent();
functionParent = () => {
console.log("Inside parent");
const parentVariable = 1;
console.log('grandParentVariable from inside parent '+grandParentVariable);
// functionChild();
functionChild = () => {
console.log("Inside child");
const childVariable = 2;
console.log('childVariable: '+childVariable);
console.log('parentVariable: '+parentVariable);
console.log('grandParentVariable: '+grandParentVariable);
console.log('globalVariable: '+globalVariable);
}
}

};
grandParent();
functionParent();
functionChild();



// ==== Challenge 2: Create a counter function ====
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
let count = 0
return () => {
count++
console.log(count)
}
};

newCounter = counter();
newCounter();
newCounter();

// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2

/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */

// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
const counterFactory = () => {
const counterFactory = (counterVariable) => {
// Return an object that has two methods called `increment` and `decrement`.
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.
console.log('inside counterFactory');
console.log('counterVariable '+counterVariable);

const counterFactoryObject = {
increment: function(){
return console.log('counterVariable increment '+ ++counterVariable);
},
decrement: function(){
return console.log('counterVariable decrement '+ --counterVariable);
}
}

return (counterFactoryObject.increment(), counterFactoryObject.decrement());
};

counterFactory(9);

19 changes: 15 additions & 4 deletions assignments/stretch-function-conversion.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax

// let myFunction = function () {};
let myFunction = () => {}

// let anotherFunction = function (param) {
// return param;
// };

let anotherFunction = param => param;

// let add = function (param1, param2) {
// return param1 + param2;
// };
// add(1,2);

let add = (param1, param2) => param1 + param2;
add(1,2);

// let subtract = function (param1, param2) {
// return param1 - param2;
// };
// subtract(1,2);

// exampleArray = [1,2,3,4];
let subtract = (param1, param2) => param1 - param2;
subtract(1,2);

exampleArray = [1,2,3,4];
// const triple = exampleArray.map(function (num) {
// return num * 3;
// });
// console.log(triple);

const triple = exampleArray.map(num => num *3)

console.log(triple);