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
43 changes: 42 additions & 1 deletion assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,69 @@ 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(runner => {
let fullname = `${runner.first_name} ${runner.last_name}`
fullNames.push(fullname)
})
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 = [];
runners.map(runner => {
const firstname = runner.first_name.toUpperCase()
firstNamesAllCaps.push(firstname)
})
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 = [];
runners.filter(runner => runner.shirt_size === 'L')
.map(runner => runnersLargeSizeShirt.push(runner))
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
.map(runners => runners.donation)
.reduce((totalDonations, donation) => {
ticketPriceTotal = totalDonations + donation
return ticketPriceTotal
}, 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

// Identify top doners
let topDoners = [];
runners.filter(runner => runner.donation >= 250)
.map(runner => topDoners.push(runner))
console.log(topDoners);

// Problem 2

// Problem 3
// Get the average
let average = 0;
let donors = runners
.map(runners => runners.donation)
.reduce((totalDonations, donation) => {
ticketPriceTotal = totalDonations + donation
average = ticketPriceTotal
return ticketPriceTotal
}, 0)
average = donors / runners.length
console.log(average);

// Problem 3

// Sort Last names alphabetically
let lastNames = [];
runners.map(runner => {
lastNames.push(runner.last_name)
})
console.log(lastNames.sort());
50 changes: 50 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,79 @@ 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 logLength(items) {
console.log(items)
}

getLength(items, logLength)

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

function logLast() {
console.log(items.slice(-1));
}

last(items, logLast)

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

function logAdd(x, y) {
console.log(x + y);
} // callback

sumNums(3,4, logAdd)

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

function logMultiply(x, y) {
console.log(x * y);
} // callback

multiplyNums(6, 6, logMultiply)

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

function logContains(item, list) {
console.log(list.includes(item))
}

contains("Pencil", items, logContains)

/* STRETCH PROBLEM */

let numArray = [1,4,7,4,5,8,9,3,1]
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)
}

function logDups(array) {
console.log(array.reduce((unique, items) => {
return unique.includes(items) ? unique : [...unique, items]
}, []))
}

removeDuplicates(numArray, logDups)
console.log(numArray)
55 changes: 53 additions & 2 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
// 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.
function sayHello(name) {
const greeting = "Hello";
const hello = () => {
return `${greeting} ${name}`
}
return hello;
}

let greet = sayHello('Dre')

console.log(greet())


/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
Expand All @@ -12,22 +23,62 @@
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`!
let count = 0;
// 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!
function counter() {
return console.log(++count)
}
// 3- Return the `counter` function.
return counter
};
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
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.
const counterMakerAdv = (limit) => {
let count = 0;
function counter() {
if (count < limit) {
return console.log(++count)
} else {
count = 1
console.log(count)
}
}
return counter
};

const counterWithLimit = counterMakerAdv(5)
counterWithLimit()
counterWithLimit()
counterWithLimit()
counterWithLimit()
counterWithLimit()
counterWithLimit()

// ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
const counterFactory = () => {
let count = 0;
// 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.

return {
increment: function() {
return console.log(++count)
},
decrement: function() {
return console.log(--count)
}
}
};

const counterWithMethods = counterFactory()
counterWithMethods.increment()
counterWithMethods.increment()