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
29 changes: 19 additions & 10 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// A local community center is holding a fund raising 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.
// 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 @@ -54,30 +54,39 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
{"id":50,"first_name":"Shell","last_name":"Baine","email":"[email protected]","shirt_size":"M","company_name":"Gabtype","donation":171}];

// ==== 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.
// 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(x => fullName.push(x.first_name +" "+ x.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);
let allCaps = runners.map(x => x.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 = [];
let largeShirts = runners.filter(x => x.shirt_size === "L");
console.log(largeShirts);

// ==== Challenge 4: Use .reduce() ====
// ==== Challenge 4: Use .reduce() ====0
// 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 = [];
let ticketPriceTotal = runners.reduce((a, b) => a + b.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

// Each participant gets a shirt with thier company logo paired with the logo for the fun run
let logoShirt = [];
runners.forEach(x => logoShirt.push(x.company_name +" x "+"5k fun run"));
console.log(logoShirt);
// Problem 2

// Problem 3
// If a participant donated more then 150 they get a raffle ticket
let raffleTicket = runners.filter(x => x.donation > 150);
console.log(raffleTicket);
// Problem 3
// database needs all email capitalized
let emailCaps = runners.map(x => x.email.toUpperCase());
console.log(emailCaps)
45 changes: 36 additions & 9 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
// 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 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'];
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum','Gum'];

/*
/*

//Given this problem:

//Given this problem:

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

// Potential Solution:

// Higher order function using "cb" as the call back
function firstItem(arr, cb) {
return cb(arr[0]);
}

// Function invocation
firstItem(items, function(first) {
console.log(first)
});
Expand All @@ -27,29 +24,59 @@ 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);
}

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


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

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

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

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.
items.forEach(element => {
return cb(element === item);
});
}
contains('Gum',items, check =>
console.log(check));

/* 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.
const duplicateFree = array.filter((x,i,arr) => {
return !(i === arr.indexOf(x))
});
cb(duplicateFree);
}

removeDuplicates(items,(array) => {
console.log(array)
})
46 changes: 38 additions & 8 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!
function closureTest(){
let test = "Does it work???"
itWorks();
function itWorks(){
console.log(test)
};
};


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

closureTest();

// ==== Challenge 2: Create a counter function ====
const counter = () => {
const newCounter = counter => {
// Return a function that when invoked increments and returns a counter variable.
increment();
function increment(){
count+=1;
console.log(count)
}
};
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
let count = 0;
newCounter();// 2
newCounter();
/* 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 newCount = 0;

return {
increment: () => {
newCount++;
console.log(`the ${newCount}`);
},
decrement: () => {
newCount--;
console.log(newCount);
}
}
};

let counter = counterFactory();
counter.increment();
counter.increment();


// 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.
};
15 changes: 15 additions & 0 deletions assignments/function-conversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax

let myFunction = () => {};

let anotherFunction = (param) => { return param; };

let add = (param1, param2) => {return(param1 + param2); };
add(1,2);

let subtract = (param1, param2) => { return (param1 - param2); };
subtract(1,2);

exampleArray = [1,2,3,4];
const triple = exampleArray.map((num) => { return num * 3;});
console.log(triple);
3 changes: 2 additions & 1 deletion assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
<script src="array-methods.js"></script>
<script src="callbacks.js"></script>
<script src="closure.js"></script>
<script src="function-conversion.js"></script>
</head>

<body>
<h1>JS II - Check your work in the console!</h1>
</body>
</html>
</html>