Skip to content
Merged
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
69 changes: 65 additions & 4 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,89 @@ 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);

runners.forEach(function(runners){
fullName.push(runners.first_name + " " + runners.last_name)
});
// runners.forEach(runners => {
// console.log((runners.first_name +' '+ runners.last_name));
// });
// fullName.push(names);

// function names(runners, cb) {
// cb(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 = [];
console.log(allCaps);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

use const until you can't keep that in mind


const firstNames = runners.map(function(run) {
allCaps.push(run.first_name.toUpperCase());
})


// console.log(caps);

// const firstNames = runners.map((run) => {
// allCaps.push(runners.first_name);
// })
// console.log(firstNames);

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

const shirts = runners.filter((shirts) => {
if (shirts.shirt_size === "L") {
return largeShirts.push(shirts);
}
});

// console.log(shirts);
// 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 price = runners.reduce((acc, item) => {
acc += item.donation;
// console.log(acc, item.donation);
return acc
}, 0);

ticketPriceTotal.push(price);

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
//Need to send a E-mail message blast to everyone

const email = runners.map((blast, message) => {
message = "Stay safe!";
return (`${blast.email} ${message}`);
});

console.log(email)

// Problem 2
// select people from company skinix from array
let skinix = [];

const company = runners.filter((company) => {
if (company.company_name === "Skinix") {
return skinix.push(company);
}
});

console.log(skinix);

// Problem 3
46 changes: 36 additions & 10 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// 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 @@ -24,27 +26,51 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

*/


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

function last(arr, cb) {
// last passes the last item of the array into the callback.
function last(arr, cb) {
cb(arr[3])
}
last(items, (lastItem) => {
console.log(lastItem);
});

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

function multiplyNums(x, y, cb) {
sumNums(2, 3, (add) => {
console.log(add);
});

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

// contains checks if an item is present inside of the given array/list.
//Pass true to the callback if it is, otherwise pass false.
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.
}
const checks = () => {
for (let i = 0; i < list.length; i++) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

how can you convert this into es6 instead of using for loops

if (list[i] === item) {
return true;
}
} return false;
};
cb(checks());
};


/* STRETCH PROBLEM */

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

function closureOuter() {
var x = "Hi";
function closureInner() {
console.log(x);
}
console.log('test', x);
return closureInner;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

good job on the closure

closureOuter();

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


// ==== Challenge 2: Create a counter function ====
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
let count = 0
const myFunction = () => {
count = ++count
return count
}
return myFunction
};

const increment = counter()
const c1 = increment()
const c2 = increment()
const c3 = increment()

console.log('increment:', c1, c2, c3);
// Return a function that when invoked increments and returns a counter variable.
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
Expand Down