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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ We have learned that closures allow us to access values in scope that have alrea

* [ ] Arrow Function Syntax - You will see more and more arrow functions as you progress deeper into JavaScript. Use the [function-conversion.js](assignments/function-conversion.js) file as a helper challenge to showcase some of the differences between ES5 and ES6 syntax

* [ ] Look up what an IIFE is in JavaScript and experiment with them
* [ ] Look up what an IIFE is in JavaScript and experiment with them

* [ ] Important Links:
https://docs.google.com/spreadsheets/d/17fRw7oxPobXS9zJT8HVCV_tFVJ1jGGstf0RUp5RmYU8/edit#gid=0

JS III
https://youtu.be/qNLeZlnAslo

41 changes: 35 additions & 6 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,57 @@ 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 = [];
const loopFullName = (item, index) =>{
fullName.push(`${item.first_name} ${item.last_name}`);
return fullName;
}
runners.forEach(loopFullName);
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 = [];
runners.map((item) => {
allCaps.push(item.first_name.toUpperCase() + " " + item.last_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 = [];
const pullLargeShirts = (item) => {
if (item.shirt_size == "L"){
largeShirts.push(item.first_name);
}
// largeShirts.push(item);
return largeShirts;
}

runners.filter(pullLargeShirts);
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 = [];
console.log(ticketPriceTotal);
const eachTicketPrice = (item) => {
ticketPriceTotal.push(item.donation);
// console.log(item.donation);
return ticketPriceTotal;
}

const runnersDonation = runners.forEach(eachTicketPrice);
// console.log(ticketPriceTotal);
const total = ticketPriceTotal.reduce((total, amount) => total + amount);
console.log(`The total donation amount is \$${total}`);
// response.reduce(calcTotalDonation);
// 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 solve 3 unique problems using one or many of the array methods listed above.

// Problem 1

// Problem 2

// Problem 3
// Problem 1/2/3
console.log(`* Problems I could solve \n
** Get records of all users but a specific key/value or combined such as Fullname \n
** Pull specific records from one object to another, such as passing multiple users under the same subscription \n
** Combine two objects and return a single object, more like a SQL join of two columns and tables. \n
** Search and return page/objects it is referring to.`)
70 changes: 70 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,100 @@
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

//MAIN ARRAY CALLBACK
function arrayItems(arr, type){
switch (type){
case "first":
return arr[0];
break;
case "last":
return arr[arr.length - 1];
break;
case "length":
return arr.length;
break;
}
}

//MAIN ARITHMETIC FUNCTIONS
function doMath(x, y, operation){
switch (operation){
case "sum":
return x + y;
break;
case "multiply":
return x * y;
break;
}
}

//REDUNDANT
function checkFirst(arr){
return arr[0];
}

function arrayLength(arr){
return arr.length - 1;
}

//CHALLENGE
function firstItem(arr, cb) {
// firstItem passes the first item of the given array to the callback function.
return cb(arr, "first");
}

firstItem(items, arrayItems);

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

getLength(items, arrayItems);

function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr, "last");
}

last(items, arrayItems);

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

sumNums(3, 4, doMath);

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

multiplyNums(7, 10, doMath);

function itemInArray(item, arr){
if (arr.indexOf(item) == -1){
return false;
}
else{
return true;
}
}

//using === TEST pending
function itemInArrayTwo(item, arr){
return arr.indexOf(item) === -1;
}

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, itemInArray);
}

contains("yo-yo", items, itemInArray);
contains("nope", items, itemInArray);

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down
43 changes: 38 additions & 5 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!
const closuresDown = (name) => {
let firstName = "Lambda";

const closureDump = () => {
let subject = name;
return `${firstName} has taught me ${subject}, I know you do not believe it!`;
}

return closureDump();
}

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

/* 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 = () => {
// 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.
// // 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 count = 0;
const increment = () => {
return ++count;
}

const decrement = () => {
return --count;
}
return {increment, decrement};
};

const manageCounter = counterFactory();
console.log(manageCounter.increment());
console.log(manageCounter.increment());
console.log(manageCounter.increment());
console.log(manageCounter.decrement());
29 changes: 25 additions & 4 deletions assignments/function-conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,43 @@

// let myFunction = function () {};

const myFunction = () =>{

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

const anotherFunction = (param) => {
return param;
}
console.log(anotherFunction("Hello"));
// let add = function (param1, param2) {
// return param1 + param2;
// };
// add(1,2);

const add = (param1, param2) => {
return param1 + param2;
}
console.log(add(1,2));



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

// exampleArray = [1,2,3,4];
const subtract = (param1, param2) => {
return param1 - param2;
}
console.log(subtract(1,2));

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

const triple = () =>{
return exampleArray.map(num => num * 3);
}
console.log(triple());