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
7 changes: 6 additions & 1 deletion assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,26 @@ 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(function(item){fullNames.push(`${item.first_name} ${item.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(x => x.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(z => z.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;
let donations = runners.map(x =>x.donation)
let ticketPriceTotal = donations.reduce((acc,x)=> acc + x)

console.log(ticketPriceTotal);

// ==== Challenge 5: Be Creative ====
Expand Down
41 changes: 39 additions & 2 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
// 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.


const makeSomething = () => {
let something = null;
function putSomething (){
something = "thing";
console.log(something);
}
putSomething()
}
makeSomething()
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */


Expand All @@ -16,18 +23,48 @@ const counterMaker = () => {
// 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.
let count = 0;
console.log(count)
return function() {
if (count < 10){
return ++count;
}else {
return "time is Up!!!"
}


}
};
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// 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.

// ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
let count2 = 0;
const counterFactory = () => {

const newTime = {
increase: function(){
return ++count2;
},
decrease: function(){
return --count2;
}

}
console.log(count2);
return newTime;


// 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.
};
counterFactory().increase()
counterFactory().increase()
counterFactory().increase()