Skip to content

Emil beckwith #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
6 changes: 5 additions & 1 deletion assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.

const runners = [
const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"[email protected]","shirt_size":"2XL","company_name":"Divanoodle","donation":75},
{ id: 1, first_name: "Charmain", last_name: "Seiler", email: "[email protected]", shirt_size: "2XL", company_name: "Divanoodle", donation: 75 },
{ id: 2, first_name: "Whitaker", last_name: "Ierland", email: "[email protected]", shirt_size: "2XL", company_name: "Wordtune", donation: 148 },
{ id: 3, first_name: "Julieta", last_name: "McCloid", email: "[email protected]", shirt_size: "S", company_name: "Riffpedia", donation: 171 },
Expand Down Expand Up @@ -58,21 +58,25 @@ 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(runner => fullNames.push(`${runner.first_name} ${runner.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 = [];
firstNamesAllCaps = runners.map(runner => runner.first_name.toUpperCase());
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 = [];
largeSizeShirt = runners.filter(runner => runner.shirt_size === 'L');
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;
ticketPriceTotal = runners.reduce((total, runner) => runner.donation + total, 0);
console.log(ticketPriceTotal);

// ==== Challenge 5: Be Creative ====
Expand Down
22 changes: 22 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,36 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

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.
for (let i = 0; i < arr.length; i++) {
if (i === arr.length - 1) {
return cb(arr[i]);
}
}
}

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

return cb(sum);
}

function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
let product = x * y;

return cb(product);
}

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.
return cb(list.includes(item));
}

/* STRETCH PROBLEM */
Expand All @@ -66,4 +79,13 @@ 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.
let duplicateFree = [];
array.forEach(item => {
if (!duplicateFree.includes(item)) {
duplicateFree.push(item);
}
}
)

return cb(duplicateFree);
}
32 changes: 30 additions & 2 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
// Keep it simple! Remember a closure is just a function
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.
const add = (function () {
let counter = 0;
return function () {counter += 1; return counter};
})();

console.log(add());

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

Expand All @@ -16,18 +21,41 @@ const counterMaker = () => {
// NOTE: This `counter` function, being nested inside `counterMaker`,
// "closes over" the `count` variable. It can "see" it in the parent scope!
// 3- Return the `counter` function.
let i = 0;
return function () {i += 1; return i};
};
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2

const myCounter = counterMaker();
console.log(myCounter()); // 1
console.log(myCounter()); // 2
// ==== Challenge 3: Make `counterMaker` more sophisticated ====
// It should have a `limit` parameter. Any counters we make with `counterMaker`
// will refuse to go over the limit, and start back at 1.

var a = counterMaker();
for (i = 0; i < 10; ++i)
{
console.log(a);
a = (a % 10) + 1;
}
// ==== Challenge 4: 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`.
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.
let i = 0;
return {
"increment" : function (i) {
return i+=1;
},
"decrement" : function (i) {
return i-=1;
}

};
};

const newCounterFactory = counterFactory;
console.log(newCounterFactory(this.increment));
console.log(newCounterFactory(this.decrement));