Skip to content

Freddie thompson #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
35 changes: 31 additions & 4 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,55 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
// ==== 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(person){
fullName.push(person.first_name + " " + person.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 = [];
let allCaps = runners.map((person) => person.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
let largeShirts = [];
let largeShirts = runners.filter(person => person.shirt_size == "L");
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 = [];
let ticketPriceTotal = runners.reduce((tickTotal, person)=>{
return tickTotal += person.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.

// Problem 1
// we need users full names and email for a list
let fullNameEmail = [];
runners.forEach(function(person){
fullNameEmail.push(person.first_name + " " + person.last_name + " - Email: " + person.email);
});
console.log(fullNameEmail);


// Problem 2
//what is our average donation amount
let someArray = [];
runners.forEach(person => someArray.push(person.donation));
//console.log(someArray);
let total = someArray.reduce((tots, amount)=>{
return tots += amount;
});
let avgDonation = total/someArray.length;
console.log(avgDonation);


// Problem 3
// Problem 3
//how may medium size shirts do we need to order again?
let medShirts = runners.filter(person => person.shirt_size == "M");
let numOfShirts = medShirts.length;
console.log(`We need ${numOfShirts} medium shirts`);
50 changes: 48 additions & 2 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,78 @@ 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);
}

console.log(getLength(items, long => `length of array: ${long}`)); // wrote two ways for understanding
// console.log(
// getLength(items, function(long) {
// return 'length of array: ' + long;
// })
// );

function last(arr, cb) {
// last passes the last item of the array into the callback.
let lastSpot = arr.length - 1;
return cb(arr[lastSpot]);
}
console.log(last(items, last => `This is the last item: ${last}`));
// console.log(
// last(items, function(theLast) {
// return 'the last item is: ' + theLast;
// })
// );

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

console.log(sumNums(5, 6, total => `Your Sum is: ${total}`));

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

console.log(multiplyNums(5, 6, totalProd => `Your Product is: ${totalProd}`));

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.
const filterArray = list.filter(thing => {
return thing == item;
});
let testLength = filterArray.length;
if (testLength > 0) {
return cb(true);
} else {
return cb(false);
}
}

/* STRETCH PROBLEM */
console.log(contains('Gum', items, list => `the output was: ${list}`));
console.log(contains('Candy', items, list => `the output was: ${list}`));

/* STRETCH PROBLEM */
const dupArray = [1, 3, 4, 4, 6, 1, 8];
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.
const uniqueSet = new Set(array);
const uniqueArray = Array.from(uniqueSet);

return cb(uniqueArray);
}

console.log(
removeDuplicates(dupArray, function(arr) {
return arr;
})
);
console.log(dupArray);
18 changes: 15 additions & 3 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

let foo = 'bar';

/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
function printAFoo() {
console.log(foo);
}

printAFoo();

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

// ==== Challenge 2: Create a counter function ====
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
let countIt = 0;

return function() {
return ++countIt;
};
};
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
const newCounter = counter();
console.log(newCounter()); // 1
console.log(newCounter()); // 2

// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
const counterFactory = () => {
Expand Down