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
123 changes: 102 additions & 21 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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.
// // 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.
Comment thread
CookieMonsta89 marked this conversation as resolved.
Resolved

// 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":"cseiler0@wired.com","shirt_size":"2XL","company_name":"Divanoodle","donation":75},
{"id":2,"first_name":"Whitaker","last_name":"Ierland","email":"wierland1@angelfire.com","shirt_size":"2XL","company_name":"Wordtune","donation":148},
Expand Down Expand Up @@ -53,31 +53,112 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
{"id":49,"first_name":"Bel","last_name":"Alway","email":"balway1c@ow.ly","shirt_size":"S","company_name":"Voolia","donation":107},
{"id":50,"first_name":"Shell","last_name":"Baine","email":"sbaine1d@intel.com","shirt_size":"M","company_name":"Gabtype","donation":171}];

// ==== Challenge 1: Use .forEach() ====
// // ==== 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(runner){
fullName.push(runner.first_name + ' ' + runner.last_name);
})
console.log(`Challenge 1 Output:\n ${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);
// // // ==== 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 = runners.map(function(runner){
return runner.first_name.toUpperCase();
})

// ==== 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);
console.log(`\nChallenge 2 Output:\n${allCaps}`);

// ==== 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 = [];
console.log(ticketPriceTotal);
// // // ==== 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 = runners.filter(function(runner){
if(runner.shirt_size == 'L'){
return runner;
}
});
let values = JSON.stringify(largeShirts);

// ==== 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
console.log(`\nChallenge 3 Output:\n${values}`);

// // // ==== 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 = runners.reduce(function(acc,val){
return acc + val.donation;
},0)


console.log(`\nChallenge 4 Output:\n${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 - We've been hacked! It was someone from an @example.com domain address, can you find it? Return array with the persons info where there are any @example.com addresses.

console.log('\nChallenge 5 - Problem 1 Output:')
const addresses = runners.filter(function(runner){
let domain = runner.email.split('@');
if(domain.includes('example.com')){
return true;
}
});

addresses.forEach(function(item){
console.log(item);
})

// // // Problem 2 - We need more sponsers. Put together a list of all the runners who donated 200 dollars or more. Then add up their total donations, log the list and total. We're going to try and throw them an appreciation party in the most public way possible. Just want to see if we can afford it.

const donors = runners.filter(function(donor){
if(donor.donation >= 200){
return true;
}
});
const total = donors.reduce(function(acc, val){
return acc + val.donation;
},0);

console.log(`\nChallenge 5 - Problem 2 Output:`)

donors.forEach(function(donor){
console.log(donor);});

console.log(`\nTotal Donations :$${total}`);

// // // Problem 3 - In preparation for the race, we want to try and oranize the runners at the starting line according to who passed the benchmark run or not. For each runner include an entry called 'benchmark' and state whether they passed or failed. Return the array and how many people passed the benchmark.

function includeBenchmark(){
let value = Number(Math.random() * 1).toFixed();
if(value == 1){
return 'Passed';
}
else{
return 'Failed';
}
}

const benchmarkers = runners.map(function(runner){

return Object.defineProperty(runner, 'benchmark',{
value: includeBenchmark(),
writable: false,
enumerable: true
})
});


function passers(arr = benchmarkers){
let totalPassed = 0;
arr.forEach(function(runner){
if(runner.benchmark == 'Passed'){
totalPassed++;
}
})
return totalPassed;
}

let newarray = JSON.stringify(benchmarkers);
console.log(`Challenge 5 - Problem 3 Output:\n ${newarray} \nThe total number of runners who passed is ${passers()}`);

// Problem 2

// Problem 3
44 changes: 43 additions & 1 deletion assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,74 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

*/


function getLength(arr, cb) {
Comment thread
CookieMonsta89 marked this conversation as resolved.
Resolved
// getLength passes the length of the array into the callback.
let alength = arr.length;
return cb(alength);
}
getLength(items, function(output){
console.log(`getLength() output:\n${output}`);
})

function last(arr, cb) {
// last passes the last item of the array into the callback.
let lastItem = arr.pop();
return cb(lastItem);
}
last(items, function(output){
console.log(`\nlast() output:\n${output}`);
})

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);
}
sumNums(4,5,function(output){
console.log(`\nsumNums() output:\n${output}`);
})

function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
let product = x * y;
return cb(product);
}
multiplyNums(6,7,function(output){
console.log(`\nmultiplyNums() output:\n${output}`);
})

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.
if(list.includes(item)){
return cb(true);
}
else{
return cb(false);
}
}
contains('yo-yo',items,function(output){
console.log(`\ncontains() output:\n${output}`);
})

/* STRETCH PROBLEM */

const items2 = ['Pencil', 'Notebook', 'yo-yo', 'Gum', 'yo-yo'];

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

removeDuplicates(items2, function(output){
console.log(`\nStretch output:\n${output}`);
})

39 changes: 35 additions & 4 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!
let array = [1,2,3,4,5];
Comment thread
CookieMonsta89 marked this conversation as resolved.
Resolved
function closureLevelOne(){
console.log(`Challenge 1 Output:\n-closureLevelOne() access: ${array}`);
closureLevelTwo();
function closureLevelTwo(){
let total = array.reduce(function(acc, val){
return acc + val;
})
console.log(`\n-closureLevelTwo access: Total = ${total}`);
closureLevelThree();
function closureLevelThree(){
console.log(`\n-closureLevelThree access: Element 3 is ${array[3]}`);
}
}
}
closureLevelOne();


// ==== Challenge 2: Create a counter function ====
const counter = () => {
const counter = (x) => {
// Return a function that when invoked increments and returns a counter variable.
x++;
return x;
};
// Example usage: const newCounter = counter();
// newCounter(); // 1
console.log(`\nChallenge 2 Output:\n${counter(1)}`);
//Example usage: const newCounter = counter();
//newCounter() // 1
// newCounter(); // 2

/* 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 = () => {
const counterFactory = (x) => {
// 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 counter = x;
function increment(counter){
counter++;
return counter;
};

function decrement(){
counter--;
return counter;
};
//console.log(counter);
};

24 changes: 12 additions & 12 deletions assignments/stretch-function-conversion.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax

// let myFunction = function () {};
let myFunction = () => {};

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

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

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

// exampleArray = [1,2,3,4];
// const triple = exampleArray.map(function (num) {
Expand Down