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
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\assignments\\array-methods.js"
}
]
}
89 changes: 79 additions & 10 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// 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 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.

// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.
// 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 = [{"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},
Expand Down Expand Up @@ -54,30 +57,96 @@ 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 = [];
//fullName = runners.map(runner=>`${runner.first_name} ${runner.last_name}`);
runners.forEach(runner=>fullName.push(`${runner.first_name} ${runner.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 = [];
let allCaps = runners.map(runner=>runner.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 = [];
// 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 = runners.filter(runner=>runner.shirt_size==="L");
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 = [];
// 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 = runners.reduce((total,cur)=>total + cur.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.
// 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

//theres a drawing for a raffle where all the people who donated at least 100 get entered for a chance to win
// a dinner party for their office. Need a array to keep track of all the qualified entrants.

let qualifyDonation = runners.filter(runner=>runner.donation>=100);
console.log(qualifyDonation);

// Problem 2
// the fundraiser needs to display a list of all their sponsors in a pamphlet for the event.
// Get an array of all the company names and sort them alphabetically.
let allCoNames = (runners.map(runner=>runner.company_name)).sort();
console.log(allCoNames);


// Problem 3
// organizers need to figure out how many shirts of each size to order and for who.
// Get a list of the names of the people ordering each shirt and display how many of them there are.

let xsShirts = runners.filter(runner=>runner.shirt_size==="XS");
if(xsShirts!==[])
{
console.log(`There are ${xsShirts.length} runners ordering size XS shirts`);
console.log(xsShirts);
}
let sShirts = runners.filter(runner=>runner.shirt_size==="S");
if(sShirts!==[])
{
console.log(`There are ${sShirts.length} runners ordering size S shirts`);
console.log(sShirts);
}
let mShirts = runners.filter(runner=>runner.shirt_size==="M");
if(mShirts!==[])
{
console.log(`There are ${mShirts.length} runners ordering size M shirts`);
console.log(mShirts);
}
let lShirts = runners.filter(runner=>runner.shirt_size==="L");
if(lShirts!==[])
{
console.log(`There are ${lShirts.length} runners ordering size L shirts`);
console.log(lShirts);
}
let xlShirts = runners.filter(runner=>runner.shirt_size==="XL");
if(xlShirts!==[])
{
console.log(`There are ${xlShirts.length} runners ordering size XL shirts`);
console.log(xlShirts);
}
let xlTwoShirts = runners.filter(runner=>runner.shirt_size==="XL2");
if(xlTwoShirts!==[])
{
console.log(`There are ${xlTwoShirts.length} runners ordering size XL2 shirts`);
console.log(xlTwoShirts);
}
let xlThreehirts = runners.filter(runner=>runner.shirt_size==="XL3");
if(xlThreehirts!==[])
{
console.log(`There are ${xlThreehirts.length} runners ordering size XL3 shirts`);
console.log(xlThreehirts);
}

// Problem 3
43 changes: 41 additions & 2 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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'];
const items = ['Pencil','Pencil', 'Notebook', 'yo-yo', 'Gum'];

/*

Expand All @@ -25,31 +27,68 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
*/


cb(list.forEach(function(item, list){
if(item===list)
return true;
else return false;
}))


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

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

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

last(items, function(last) {
console.log(last);
});

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x+y);
}
sumNums(5,6,function(sum){
console.log(sum);
});

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

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));
}

contains('Pencil', items, function(cont){
console.log(cont);
});

/* 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.
let newArray =[];
array.forEach(item=>{if(!newArray.includes(item)) newArray.push(item)});
return cb(newArray);
}

removeDuplicates(items,function(remove){
console.log(remove);
});
25 changes: 22 additions & 3 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!
const multiply = x => (y => x*y);

const mult5 = multiply(5);

console.log(mult5(3));

/* 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;
return () => ++count;
};
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
const newCounter = counter();
console.log(newCounter()); // 1
console.log(newCounter()); // 2

// ==== Challenge 3: 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: ()=> ++count,
decrement: ()=> --count
}
};

const newCntFactory = counterFactory();
console.log(newCntFactory.increment());
console.log(newCntFactory.increment());
console.log(newCntFactory.increment());
console.log(newCntFactory.decrement());
console.log(newCntFactory.increment());
console.log(newCntFactory.decrement());