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
41 changes: 36 additions & 5 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,60 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c

// ==== 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 = [];

/* version one */

/* const fullName = runners.map((person) => {
return `${person.first_name} ${person.last_name}`;
});
*/

const fullName = []

runners.forEach(person => fullName.push(`${person.first_name} ${person.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 = [];

const allCaps = runners.map(person => `${person.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 = [];

const largeShirts = runners.filter(person => person.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 = [];
const ticketPriceTotal = runners.reduce((acc, currentPerson) => {
return acc += currentPerson.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
//Show each name of the student and how much they donated in USD notation.
const studentDonation = runners.map(person => `${person.first_name} ${person.donation}.00 USD`);

console.log(studentDonation);
// Problem 2
//There was an error with sending out some emails to students, we need info about all students with an email from the provider yale.edu. Quick! We need to get this fixxed.
const studentEmailFromYale = runners.filter(person => person.email.includes('yale.edu'));

console.log(studentEmailFromYale);
// Problem 3
// We need a list of all students with their name and their donations next to it as an Array containing each of them in an object.

const DonationsAsJSON = runners.map((person) => {
return { name: `${person.first_name}`, donation: `${person.donation}`};
});

// Problem 3
console.log(DonationsAsJSON);
139 changes: 136 additions & 3 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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

/*

/*
//Given this problem:

function firstItem(arr, cb) {
Expand All @@ -20,36 +20,169 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
// Function invocation
firstItem(items, function(first) {
console.log(first)
});
}); */



*/

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

/* version one */

/*
function length(arr) {
console.log(arr.length);
}

getLength(items, length); */

/* version two */

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

getLength(items, console.log);

/* ---------------------------------------------------------------- */

/* function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr[arr]);
}
*/
/* version one */

/* function lastItem(arr) {
console.log(arr[arr.length -1]);
}

last(items, lastItem); */

/* version two */

/* last(items, function lastItem(arr) {
console.log(arr[arr.length -1]);
}); */


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

last(items, console.log);


/* ---------------------------------------------------------------- */

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

/* There are some problems with this version!
function sum(x,y){
return cb(x + y);
}

sumNums(2, 3, sum);
*/

/* version two */

/* sumNums(2, 3, function(x, y) {
console.log(x + y)
});
*/

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

sumNums(2, 3, console.log);

/* ---------------------------------------------------------------- */

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

/* There are some problems with this version!
function multiply(x, y){
console.log(x * y);
}

multiplyNums(2, 3, multiply(x, y));
*/

/* version two */

/* multiplyNums(2, 3, function(x, y) {
console.log(x * y)
}); */

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

multiplyNums(2, 3, console.log);

/* ---------------------------------------------------------------- */
/*
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(item, list)
} */

/* version one */

/* function containCheck(item, list) {
console.log(list.includes(item));
}

contains("Pencil", items, containCheck); */

/* version two */

/* contains("Pencil", items, function(item, list) {
console.log(list.includes(item));
});
*/

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, console.log);


/* 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.
return cb(array.filter((value, index, self) => self.indexOf(value) === index))
}

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

removeDuplicates(itemsCheated, console.log);



37 changes: 34 additions & 3 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

const cat = 'kittie';
function returnCat () {
let inside_variable = 'so much private';
let cat_variable = cat;
return cat_variable;
}
console.log(returnCat());
/* console.log(inside_variable); */ //is not defined bercause the variable in within the function scope

/* 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 function() {
return ++count;
}
};

const newCounter = counter();
console.log(newCounter()); // 1
console.log(newCounter()); // 2

// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2

// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
const counterFactory = () => {

// doesn't work so far

/* const counterFactory = (param) => {
// 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 countObj = { counter: 0,
increment: function() {return this.counter +1},
decrement: function() {return this.counter -1},
};
return function(param) {
return param === 'increment' ? countObj.increment() : countObj.decrement();
}
};

const newCounterFactory = counterFactory();
console.log(newCounterFactory('increment')); // 1
console.log(newCounterFactory('increment')); // 2
console.log(newCounterFactory('decrement')); // 1 */
7 changes: 7 additions & 0 deletions assignments/iife-example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// IIFE stands for Immediately Invoked Function Expression
//It does not get invoked, only referenced thus you don't need the () after the reference
//you can reference to this function alone by its name
let greet = (() => 'Hello, my name is Sascha!')();

console.log(greet);