Skip to content
58 changes: 40 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@

# JavaScript - II
With some basic JavaScript principles we can now expand our skills out even further by exploring array methods like: `.forEach()`, `.map()`, `.reduce()`, and `.filter()`. We can also look at how closures have a large impact on how we write JavaScript.

## Assignment Description
With some basic JavaScript principles in hand, we can now expand our skills out even further by exploring callback functions, array methods, and closure. Finish each task in order as the concepts build on one another.

## Task 1: Set Up The Project With Git

* [ ] Fork the project into your GitHub user account
* [ ] Clone the forked project into a directory on your machine
* [ ] You are now ready to build this project with your preferred IDE
* [ ] To test your `console.log()` statements, open up the index.html file found in the assignments folder and use the developer tools to view the console.


## Task 2: Callbacks

This task focuses on getting practice with 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 callback functions.

* [ ] Write out each function using the `ES5` `function` keyword syntax.

* [ ] Solve the problems listed. Save the stretch problems until you have completed Tasks 1-4.

## 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.

* [ ] Complete the problems provided to you

* [ ] 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

* Fork/Clone this repository.
* Complete all the exercises as described inside each assignment file.
* Use `console.log()` statements to check to see if your code does what it is supposed to do.
* To test your `console` statements you can run `node /assignments/<fileName>` and see what prints in your terminal. You can also use an online tool like `JSBin`, `REPL.it`, `JSFiddle`, or even your `Chrome developer console`.
* Once you finish the exercises in each file, commit your code, and push it to your fork.
We have learned that closures allow us to access values in scope that have already been invoked.

### Function Conversion
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.
**Hint: Utilize debugger statements in your code in combination with your developer tools to easily identify closure values.**

### 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 fund rasising 5k fun run event.
* [ ] Review the contents of the [closure.js](assignments/closure.js) file.
* [ ] Solve the problems listed. Save the stretch problems until you have completed Tasks 1-4.
* [ ] Once you have completed this task please submit a pull request against the original fork.

* Read the instructions found within the file carefully to finish the challenges.
* The last challenge is to come up with 3 problems to solve and then build a solution for them using any of the array methods listed above.
* Share one of your favorite problem/solutions in your team meeting.
* Complete each challenge presented before moving on to closure.
## Stretch Goals

### Closures
The [closure.js](assignments/closure.js) assignment showcases how variables can be used outside of functions to store state using closure.
* [ ] Arrow Function Syntax - [Check out this awesome guide for ES6 arrow syntax](https://medium.freecodecamp.org/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26). You will see more and more arrow functions as you progress deeper into JavaScript. Use the [stretch-function-conversion.js](assignments/function-conversion.js) file as a helper challenge to showcase some of the differences between ES5 and ES6 syntax.

* Complete each challenge.
* [ ] Look up what an IIFE is in JavaScript and experiment with them
49 changes: 45 additions & 4 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// :)
// 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 @@ -55,29 +56,69 @@ 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.
console.log('----------ARRAY METHODS----------')
let fullName = [];
console.log(fullName);
runners.forEach(function (element) {
fullName.push(element.first_name + " " + element.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 = [];
runners.map(function (runners) {

allCaps.push(runners.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 = [];
const orderedLarge = runners.filter(element => {
if (element.shirt_size === 'L') {
largeShirts.push(element)
}
})
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 = [];
const red = runners.reduce(function (acc, currentValue) {
return {donation: acc.donation + currentValue.donation};
})
ticketPriceTotal.push(red);
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.
// 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
console.log ("Challenge 5.1")
console.log ('Last Name & Email')
let namesAndEmails = [];
runners.forEach (function (element) {
namesAndEmails.push(`Last: ${element.last_name}, Email: ${element.email};`)
});
console.log(namesAndEmails);

// Problem 2

// Problem 3
console.log('Challenge 5.2')
console.log ('Send shame on you letters to people who donated less than $100')
let shame = [];
const sift = runners.filter (runner => {
if (runner.donation < 100) {
shame.push(runner);
}
})
console.log(shame);
// Problem 3
console.log('Challenge 5.3')
console.log ('Companies to send thank you cards to')
let newArr = [];
runners.forEach (function (element) {
newArr.push(element.company_name)
})
console.log(newArr)
79 changes: 79 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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'];

/*

//Given this problem:

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

// Potential Solution:
function firstItem(arr, cb) {
return cb(arr[0]);
}

firstItem(items, function(first) {
console.log(first)
});

*/

console.log('----------CALLBACKS----------')

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

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



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

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

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 (sum) {
console.log (sum);
})

function multiplyNums(x, y, cb) {
return cb(x*y)
// multiplyNums multiplies two numbers and passes the result to the callback.
}
multiplyNums (2,3, function (mul){
console.log (mul);
})

function contains(item, list, cb) {
cb(list.includes(item))

// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
}

contains ('Gum', items, function (stuff) {
console.log (stuff);
});

/* 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.
}
32 changes: 30 additions & 2 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

console.log('----------CLOSURE----------')
function isThisClosure () {
const closure = 'closure'
console.log(closure)
thisIsClosure();
function thisIsClosure () {
console.log(`Yea, this is ${closure}.`)
}
}
isThisClosure();

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

// const newCounter = counter();
// 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 = () => {
let num = 0;
const obj



}

// 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.
Expand Down
19 changes: 19 additions & 0 deletions assignments/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>

<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>JS II</title>

<script src="array-methods.js"></script>
<script src="callbacks.js"></script>
<script src="closure.js"></script>
<script src="stretch-function-conversion.js"></script>
</head>

<body>
<h1>JS II - Check your work in the console!</h1>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
// };
// add(1,2);

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

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