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
40 changes: 38 additions & 2 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,59 @@ const runners = [
// ==== 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 and populate a new array called `fullNames`. This array will contain just strings.
let fullNames = [];

runners.forEach(function(first){

fullNames.push(first.first_name + ` ` + first.last_name)

});


console.log(fullNames);

// ==== Challenge 2: Use .map() ====
// The event director needs to have all the runners' first names in uppercase because the director BECAME DRUNK WITH POWER. Populate an array called `firstNamesAllCaps`. This array will contain just strings.
let firstNamesAllCaps = [];

let caps = runners.map(letsCap);

function letsCap(upper) {

return upper.first_name.charAt(0).toUpperCase() + upper.first_name.slice(1);

}

firstNamesAllCaps.push(caps);

console.log(firstNamesAllCaps);

// ==== Challenge 3: Use .filter() ====
// The large shirts won't be available for the event due to an ordering issue. We need a filtered version of the runners array, containing only those runners with large sized shirts so they can choose a different size. This will be an array of objects.
let runnersLargeSizeShirt = [];

let sizeLarge = runners.filter(function (runners) {
return runners.shirt_size === "L";
});

runnersLargeSizeShirt.push(sizeLarge);

console.log(runnersLargeSizeShirt);

// ==== Challenge 4: Use .reduce() ====
// The donations need to be tallied up and reported for tax purposes. Add up all the donations and save the total into a ticketPriceTotal variable.
let ticketPriceTotal = 0;
console.log(ticketPriceTotal);
let ticketPriceTotal = [];

const calc = (acc, donate) => {
return acc + donate;
}

runners.forEach(function(object){
ticketPriceTotal.push(object.donation)
});

ticketTotal = ticketPriceTotal.reduce(calc);

console.log(ticketTotal);
// ==== 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.

Expand Down
60 changes: 56 additions & 4 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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


/*

// GIVEN THIS PROBLEM:
Expand Down Expand Up @@ -38,28 +39,79 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
console.log(test2); // "this Pencil is worth a million dollars!"
*/

// getLength solution

l = () => {
return items.length;
}

function getLength(items, callback) {

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

function last(arr, cb) {
console.log(getLength(items, l));

// last time in array(hardcode) solution

a = () => {
return items[3];
}

function last(items, cb) {

return cb(items, a);
// last passes the last item of the array into the callback.
}

function sumNums(x, y, cb) {
console.log(last(items, a));

// sumNums solution

sum = (x,y) => {
return x + y
}

sumNums = (x, y, cb) => {

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

console.log(sumNums(1,2,sum));

// Multiply Solution

multiply = (x, y) => {
return x * y
}

function multiplyNums(x, y, cb) {

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

console.log(multiplyNums(1,2, multiply))

// Checks solution

let check = () =>{
if (items.includes("Gum" === true)){
return true;
} else {
return false;
}
}

function contains(item, list, cb) {

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

console.log(contains("Gum",items, check))
/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down
14 changes: 14 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

function car(fast) {
console.log(`My favorite car is a ${fast}`);

function hp(alot){
console.log(`This car has ${alot} horsepower.`)
}
hp('755');
}
car('Corvette Stringray ZR1');

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


// ==== Challenge 2: Implement a "counter maker" function ====
const counterMaker = () => {
const count = [];
const counter = () => {
counter.push(count);
}
// IMPLEMENTATION OF counterMaker:
// 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`!
// 2- Declare a function `counter`. It should increment and return `count`.
Expand Down