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
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,47 @@ With some basic JavaScript principles in hand, we can now expand our skills out

**Follow these steps to set up and work on your project:**

* [ ] Create a forked copy of this project.
* [ ] Add your project manager as collaborator on Github.
* [ ] Clone your OWN version of the repository (Not Lambda's by mistake!).
* [ ] Create a new branch: git checkout -b `<firstName-lastName>`.
* [ ] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
* [ ] Push commits: git push origin `<firstName-lastName>`.
* [x ] Create a forked copy of this project.
* [x ] Add your project manager as collaborator on Github.
* [ x] Clone your OWN version of the repository (Not Lambda's by mistake!).
* [x ] Create a new branch: git checkout -b `<firstName-lastName>`.
* [ x] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
* [x ] Push commits: git push origin `<firstName-lastName>`.

**Follow these steps for completing your project.**

* [ ] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo). **Please don't merge your own pull request**
* [ ] Add your project manager as a reviewer on the pull-request
* [ ] Your project manager will count the project as complete by merging the branch back into master.
* [x ] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo). **Please don't merge your own pull request**
* [x ] Add your project manager as a reviewer on the pull-request
* [x ] Your project manager will count the project as complete by merging the branch back into master.

## Task 2: Higher Order Functions and Callbacks

This task focuses on getting practice with higher order functions and callback functions by giving you an array of values and instructions on what to do with that array.

* [ ] Review the contents of the [callbacks.js](assignments/callbacks.js) file. Notice you are given an array at the top of the page. Use that array to aid you with your functions.
* [ x] Review the contents of the [callbacks.js](assignments/callbacks.js) file. Notice you are given an array at the top of the page. Use that array to aid you with your functions.

* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.
* [x ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.

## Task 3: Array Methods

Use `.forEach()`, `.map()`, `.filter()`, and `.reduce()` to loop over an array with 50 objects in it. The [array-methods.js](assignments/array-methods.js) file contains several challenges built around a fundraising 5K fun run event.

* [ ] Review the contents of the [array-methods.js](assignments/array-methods.js) file.
* [x ] Review the contents of the [array-methods.js](assignments/array-methods.js) file.

* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.
* [ x] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.

* [ ] Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it.
* [x ] Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it.

## Task 4: Closures

We have learned that closures allow us to access values in scope that have already been invoked (lexical scope).

**Hint: Utilize debugger statements in your code in combination with your developer tools to easily identify closure values.**

* [ ] Review the contents of the [closure.js](assignments/closure.js) file.
* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.
* [x ] Review the contents of the [closure.js](assignments/closure.js) file.
* [x ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.

## Stretch Goals

* [ ] Go back through the stretch problems that you skipped over and complete as many as you can.
* [ ] Look up what an IIFE is in JavaScript and experiment with them
* [x ] Go back through the stretch problems that you skipped over and complete as many as you can.
* [ x] Look up what an IIFE is in JavaScript and experiment with them
120 changes: 110 additions & 10 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 raising 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 raising 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 @@ -55,29 +56,128 @@ 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);

// let fullName = [];
// for (let i = 0; i < runners.length; i++) {
// fullName.push(runners[i].first_name + " " + runners[i].last_name)
// }
// console.log(fullName)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like seeing that you got both syntax's here.



let fullName2 = []
runners.forEach(runner => fullName2.push(`${runner.first_name} ${runner.last_name}`))
console.log(fullName2);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Great job using Template string literals here to render the name information from the runner object. This could also be done using string concatenation as well.



let fullName4 = []
runners.forEach(addName)

function addName(word){
fullName4.push(`${word.first_name} ${word.last_name}`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice abstracting the callback into its own function rather than typing it in the forEach callback.

}

console.log(fullName4)



// ==== 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);

// let allCaps = [];
let allCaps = runners.map(allCapMode)

function allCapMode (words) {
return words.first_name.toUpperCase()
// allCaps.push(`${wubba.first_name}`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Glad to see it looks like you have a fairly solid grasp on callbacks now!

}



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 = [];
// 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(shirts).map(names)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice use of Method chaining here, using dual abstracted callback functions stored in the global scope as their arguments!

function shirts(words){
if (words.shirt_size === "L") {
return true
}
else {
return false
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not use the .includes() method here? It returns a true if the argument passed to it is included in the array, or srting it was called on, and false if it does not.


function names(xxx) {
return xxx.first_name + " - " + xxx.shirt_size
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Naming convention! what is the xxx supposed to be a place holder for? is it not each item in the array we are mapping through?

Because of method chaining we. in this case, are iterating through the freshly filtered array of runners with a shirt size of large.

So this xxx parameter is a place holder for a runner object who already meets the conditions of being a large shirt size wearer

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(sums, 0)

function sums(x, y) {
return x + y.donation
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

In reducers, you are better off sticking to keeping the actual callback inside of your reduce and in 1 unit.


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

//use map to list donation amounts

let newDonations = runners.map(tally)

function tally (jolly) {
return jolly.donation
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Naming convention.


console.log(newDonations)

// Problem 2

// Problem 3
// use map to scrape the array to sell people's information - our customer needs first name, last name, and email addresses


let cashflow = []
runners.forEach(dollars)

function dollars (cash) {
cashflow.push(`To: ${cash.email} <br><br> Hello, Mr or Ms ${cash.first_name} ${cash.last_name}, we have an exciting offer for you today`)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

the breaks are HTML tags not JS .

console.log(cashflow)

// Problem 3
// Our research shows only people who work at companies that donated over $150 fall for this.
// Filter them out, then give us an organized list of the their names, emails, and how much they gave.
// We don't need their shirt size

let suckers = runners.filter(haha).map(dollaBillsRain)

function haha(lol) {
if (lol.donation > 150)
return lol.first_name + lol.last_name + lol.email + lol.donation
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is happening here with these names?!?!


function dollaBillsRain (payday) {
return (`${payday.first_name} ${payday.last_name} - ${payday.email} - $${payday.donation}`)
}

console.log(suckers)



64 changes: 61 additions & 3 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Create a higher order function and invoke the callback 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 higher order function and invoke the callback 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 @@ -20,36 +21,93 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
// Function invocation
firstItem(items, function(first) {
console.log(first)


});

*/


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

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

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



function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
let z = x * y;
cb(z)
}
console.log(multiplyNums(5, 10, ert))
//function ert below shows 50 properly in quokka, but console log above is showing undefined
function ert (g){
console.log(g)
}

function contains(item, list, cb) {
function contains(items, 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(items).toString());
};
console.log(contains('Pencil', items));

// Previous solution I was working on ------

// item.map(sums)
// function sums(x){}
// if (x.item === x.list){
// cb(true)
// }

// else {
// cb(false)
// }
// }

/* STRETCH PROBLEM */

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.

//----Previous attempt-----------
// let newArray = items.filter(reduction)

// function reduction(xxx) {
// if (xxx.value !== xxx.value) {
// return true
// }
// console.log(newArray);


// function test() {
// if (newArray.value == array.value) {
// return true
// }
// }

// }
// }
}


let notDupe = items.filter((item, index) => {
return items.indexOf(item) >= index;
});

console.log(notDupe);
21 changes: 21 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@
// Write a simple closure of your own creation. Keep it simple!




{
let a = 2
}

console.log(a)
//nothing

{
let b = 2;
return b
}

console.log(b);
//b = 2



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


Expand All @@ -13,6 +32,8 @@ const counter = () => {
// newCounter(); // 1
// newCounter(); // 2



// ==== 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`.
Expand Down