Skip to content
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

Start Project
# JavaScript - II

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.
* [ x] Fork the project into your GitHub user account
* [x ] Clone the forked project into a directory on your machine
* [x ] You are now ready to build this project with your preferred IDE
* [x ] 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
Expand All @@ -17,19 +17,19 @@ This task focuses on getting practice with callback functions by giving you an a

* [ ] 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.
* [x ] 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.
* [x ] 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.
* [x ] Review the contents of the [array-methods.js](assignments/array-methods.js) file.

* [ ] Complete the problems provided to you
* [x ] 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.
* [ 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

Expand Down
29 changes: 28 additions & 1 deletion 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(runners){
fullName.push(runners.first_name + " " + runners.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 = [];
allCaps = runners.map(function(person){
return 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 = [];
largeShirts = runners.filter(runners => runners.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 = [];
ticketPriceTotal = runners.reduce((donation, runner) => {
return donation += runner.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
// The event director needs both the id and email of each runner for their running bibs. Combine both the id and emails into a new array called identify.
let identify = [];
runners.forEach(function(runners){
identify.push(runners.id + " " + runners.email);
});
console.log(identify);

// Problem 2
// The medium shirts won't be available for the event due to an ordering issue. Get a list of runners with medium 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 mediumShirts = [];
mediumShirts = runners.filter(runners => runners.shirt_size === 'M');
console.log(mediumShirts);

// Problem 3
// Problem 3
// The event director needs to have all the runner's company names converted to uppercase because the director BECAME DRUNK WITH POWER. Convert each middle name into all caps and log the result
let allCaps = [];
allCaps = runners.map(function(person){
return person.first_name.toUpperCase();
});
console.log(allCaps);
26 changes: 26 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,51 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];


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

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

function sumNums(x, y, cb) {
return cb(x+y);
// sumNums adds two numbers (x, y) and passes the result to the callback.
}
sumNums(1,2, 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(1,2, function(multiply){
console.log(multiply)
});

function contains(item, list, cb) {
for (let i = 0; i < list.length; i++){
//insert (let)
if(list[i] === item){
cb(true);
};
} cb(false);
//return 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.
}
contains('Pencil', items, function(contains){
console.log(contains)
});

/* STRETCH PROBLEM */

Expand Down
10 changes: 9 additions & 1 deletion assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

function whoareyou(description) {
const name = 'diamond';
const sound = ruff;
const type = dolphin;
}

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