Skip to content

Josh humphrey-JavaScript-II #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
40 changes: 39 additions & 1 deletion assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,66 @@ 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 = [];
runners.map(runner => {
firstNamesAllCaps.push(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 = [];
runnersLargeSizeShirt = 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) => {
return total + 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
//Return companies alongside donations
let company_donation = [];

runners.map(company => {
company_donation.push(`${company.company_name} donated $${company.donation}`);
})

console.log(company_donation);




// Problem 2
//Add all runners to email list without their knowledge

let addToMailer = [];
runners.forEach(runner => {
addToMailer.push(runner.email);
})
console.log(addToMailer);

// Problem 3
//Return an array of the companies with the highest donations

let highRollers = [];

// Problem 3
runners.map(runner => {
if(runner.donation > 200) {
highRollers.push(runner.company_name);
}
})
console.log(highRollers);
26 changes: 25 additions & 1 deletion assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,52 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
console.log(test2); // "this Pencil is worth a million dollars!"
*/


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

getLength(items, (length) => {
console.log(length);
})


function last(arr, cb) {
// last passes the last item of the array into the callback.
cb(arr.pop())
}

last(items,(lastOne) => {
console.log(lastOne);
})

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
cb(x + y);
}
sumNums(2, 4, (sum) => {
console.log(sum);
})

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

multiplyNums(5,9,(product) => {
console.log(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.
cb(list.indexOf(item) > -1 ? true : false);
}

contains('Pencil', items, (bool) =>{
console.log(bool);
})

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down
80 changes: 78 additions & 2 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,52 @@
// 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.
function planet() {
let citizens = 0;
console.log(`I have ${citizens} inhabitants`);

function continent() {
citizens = 1000;
console.log(`Not true, all ${citizens} of us live here!`)

function country() {
citizens = 1500;
console.log(`With us it makes ${citizens}!`);
}
country();
}
continent();
}

planet();

/* 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 = () => {

// 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`.
// 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.
};
// const counterMaker = () => {
// let count = 0;


// return function counter() {
// count++;
// return count;
// }

// //counter
// }
// const newCounter = counterMaker();
// console.log(newCounter());
// console.log(newCounter());
// console.log(newCounter());

// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
Expand All @@ -25,9 +57,53 @@ const counterMaker = () => {
// It should have a `limit` parameter. Any counters we make with `counterMaker`
// will refuse to go over the limit, and start back at 1.

const counterMaker = () => {
let count = 0;
const limit = 5;


return function counter() {

count == limit ? count = 1 : count++;
return count;
}

//counter
}
const newCounter = counterMaker();
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());

// ==== 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 count = 0;
return {
increment: function() {
count++;
return count;
},
decrement: function() {
count--;
return count;
}
}
};

const newFactory = counterFactory();

console.log(newFactory.increment());
console.log(newFactory.decrement());