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
36 changes: 33 additions & 3 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,58 @@ 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((i) => {
fullNames.push({firstName : i.first_name, last_name : i.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 = [];
firstNamesAllCaps = runners.map((i) =>{
return i.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((i) =>{
if(i.shirt_size == "L")
return i;
});
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;
runners.reduce((acumulator, runner) => {
ticketPriceTotal += runner.donation;
});
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

let evenRunners = []; //checks if the id remainder is 0 a.k.a even and pushes it into evenrunners
runners.forEach((i) => {
if(i.id % 2 == 0){
evenRunners.push(i);
}
});
console.log(evenRunners);
// Problem 2

// Problem 3
let oddRunners = []; //checks if the id remainder is not 0 aka odd and pushes it
runners.map((i) =>{
if(i.id % 2 != 0)
oddRunners.push(i);
});
console.log(oddRunners);
// Problem 3
let bigSpenders = []; //filters the big donation spenders
runners.filter((i) =>{
if(i.donation >= 50)
bigSpenders.push(i);
});
console.log(bigSpenders);
17 changes: 11 additions & 6 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,34 @@ 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);
}

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

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

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

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

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
var unique = array.reduce((a,b) =>{
if(a.indexOf(b) < 0) a.push(b);
return a;
},[]);

return cb(unique);
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
Expand Down
66 changes: 60 additions & 6 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,85 @@
// 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.
var stuff = ["yes", "no", "maybe"];

function addName(string){
stuff.push(string);
}

/* 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.
};
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
// 3- Return the `counter` function

function counterMaker(){
let count = 0;
const counter = function(){
count = count + 1;
return count;
}
return counter;
}
const myCounter = counterMaker();
console.log(myCounter()); // 1
console.log(myCounter()); // 2



// ==== Challenge 3: Make `counterMaker` more sophisticated ====
// It should have a `limit` parameter. Any counters we make with `counterMaker`
// will refuse to go over the limit, and start back at 1.

function counterMakerLimit(limit){
let counts = 0;
const counterLimit = function(){
if(counts == limit){
return counts = 1;
}
counts = counts + 1;
return counts;
}
return counterLimit;
}
var myCounterLimit = counterMakerLimit(5);
console.log(myCounterLimit());
console.log(myCounterLimit());
console.log(myCounterLimit());
console.log(myCounterLimit());
console.log(myCounterLimit());
console.log(myCounterLimit());
console.log(myCounterLimit());


// ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
const counterFactory = () => {
const mathy = {
counting: 0,
increment: function() {
return mathy.counting += 1;
},
decrement: function() {
return mathy.counting -= 1;
}
}
return mathy;
// 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.
};


var factory = counterFactory();

factory.increment();
factory.increment();
console.log(factory.counting);
factory.decrement();
console.log(factory.counting);