Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
note
37 changes: 30 additions & 7 deletions assignments/array-methods.js → array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,51 @@ 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 = [];
console.log(fullName);
runners.forEach(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 = [];
console.log(allCaps);
// they just want an arr of first names capitalized here?
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 = [];
console.log(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 = [];
// need an initial value, otherwise the initial value becomes the first object, and each donation is tacked on to it, which is odd.
let ticketPriceTotal = runners.reduce((total, person) => total + person.donation, 0);
console.log(ticketPriceTotal);

// check work w/ for loop
// let total = 0
// for (const person of runners) {
// total += person.donation;

// }

// console.log(total);
// console.log('done');

// ==== 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
// find average donation
let avg = runners.reduce((total, person) => total + person.donation, 0) / runners.length;
console.log(avg);

// Problem 2
// get all emails
let emails = runners.map(person => person.email);
console.log(emails);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nicely done stretch

// Problem 3
// how many ppl work at Quaxo
let quaxo = runners.filter(person => person.company_name == 'Quaxo').length;
console.log(quaxo);

// Problem 3
23 changes: 0 additions & 23 deletions assignments/stretch-function-conversion.js

This file was deleted.

36 changes: 36 additions & 0 deletions assignments/callbacks.js → callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,65 @@ 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)
}


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

// getLength(items, function(x) {
// console.log(x);
// })

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

// last(items, function(x) {
// console.log(x);
// })

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

// sumNums(5, 2, function(x) {
// console.log(x);
// })

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

// multiplyNums(5, 2, function(x) {
// console.log(x);
// })

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 list.includes(item) ? cb(true): cb(false)
}

// contains('book', items, function(x) {
// console.log(x);
// })
/* STRETCH PROBLEM */

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great job, please include all of the invocations.

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.

// indexOf will find the first index of a given item. if there
// is a duplicate, indexOf result wont match the current index of the loop
let uniqueItems = array.filter((item, index) => array.indexOf(item) == index)
return cb(uniqueItems)
}

removeDuplicates([1, 2, 2, 3], function(x) {
console.log(x);
})
39 changes: 36 additions & 3 deletions assignments/closure.js → closure.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

// IIFE
var myFunc = (function () {
var name = 'bob';
return function() {
console.log(name);
}
})();

myFunc();

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

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

Expand All @@ -17,4 +32,22 @@ const counterFactory = () => {
// 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.

let counter = 0;
return {
increment() {
counter += 1;
return counter;
},
decrement() {
counter -= 1;
return counter;
}
}

};

const advCounter = counterFactory();
console.log(advCounter.increment()); // 1
console.log(advCounter.increment()); // 2
console.log(advCounter.decrement()); // 1
File renamed without changes.
15 changes: 15 additions & 0 deletions stretch-function-conversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax
let myFunction = () => {};

let anotherFunction = param => param;
console.log(anotherFunction(5))

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

let subtract = (param1, param2) => param1 - param2
console.log(subtract(1,2));

exampleArray = [1,2,3,4];
const triple = exampleArray.map(num => num * 3);
console.log(triple);
6 changes: 6 additions & 0 deletions z.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let x = {
'hi': 5
}


console.log(x + 5 + 6);