Skip to content
31 changes: 31 additions & 0 deletions REVIEW.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
JavaScript - II

## MVP

- [✅] Callbacks
- [✅] Array Methods
- [✅] Closures

## Stretch

- [✅] Cool extra stuff

## What Looks Great

- MVP complete! Nice work
- Frequent and descriptive commits
- Code is well formatted and easy to read
- Great file structure

## What Could Be Improved

- Need more commits. You should have one commit for every one change.
- Commits need more description. Check out this guide (https://chris.beams.io/posts/git-commit/)

## What Could Be Added

-

## Rating (1-3)

✅✅✅
66 changes: 56 additions & 10 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// 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},
const getRunners = () => [{"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},
{"id":3,"first_name":"Julieta","last_name":"McCloid","email":"jmccloid2@yahoo.com","shirt_size":"S","company_name":"Riffpedia","donation":171},
{"id":4,"first_name":"Martynne","last_name":"Paye","email":"mpaye3@sciencedaily.com","shirt_size":"XL","company_name":"Wordware","donation":288},
Expand Down Expand Up @@ -55,29 +55,75 @@ 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 = [];
console.log(fullName);
function challenge1(list) {
let fullName = [];
list.forEach(runner => fullName.push(`${runner.first_name} ${runner.last_name}`))
return fullName
}
console.log('Challenge 1: \n', challenge1(getRunners()), '\n\n');

// ==== 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);
firstNameToUpper = x => x.first_name.toUpperCase()
function challenge2(list){
return list.map(firstNameToUpper);
}

console.log('Challenge 2: \n', challenge2(getRunners()), '\n\n');

// ==== 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);
function challenge3(list) {
return list.filter(runner => runner.shirt_size === 'L')
}

console.log('Challenge 3: \n', challenge3(getRunners()), '\n\n');


// ==== 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);

const tallyDonations = (acc, cur) => acc + cur.donation
function challenge4(list){
return [list.reduce(tallyDonations, 0)]
}


console.log('Challenge 4: \n', challenge4(getRunners()));

// ==== 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

// helper
const genRandAge = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)

function challenge5a() {
return getRunners().map( runner => ({ ...runner, age: genRandAge(1, 65) }) )
}
console.log('Challenge 5a: \n', challenge5a())


// Problem 2

// Problem 3
// helpers
const runnersWithAge = challenge5a()
const isOldEnoughToRun = x => x.age > 16

function challenge5b(listOfRunners) {
return listOfRunners.filter(isOldEnoughToRun)
}
console.log('Challenge5b: \n', challenge5b(runnersWithAge))


// Problem 3

// helper
const generousDonors = x => x.donation > 100

function challenge5c(runners) {
return runners.filter(generousDonors)
}
console.log('Challenge 5c', challenge5c(runnersWithAge))

68 changes: 49 additions & 19 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Create a higher order function and invoke the callback function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems.

const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
const lookInBackback = () => ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

/*

Expand All @@ -24,32 +24,62 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

*/

const identity = x => x

function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
}
// getLength passes the length of the array into the callback.
const getLength = (cb, arr) => cb(arr.length)
console.log('getLegth: [Pencil, Notebook, yo-yo, Gum]', getLength(identity, lookInBackback()))

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

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
}
// last passes the last item of the array into the callback.
const last = (cb, arr) => cb(arr.slice(-1)[0])
console.log('last [Pencil, Notebook, yo-yo, Gum]: ', last(identity, lookInBackback()))

function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
}
// sumNums adds two numbers (x, y) and passes the result to the callback.
const sumNums = (cb, x, y,) => cb(x + y)
console.log('sumNums [1,1]: ', sumNums(identity, 1, 1))

// multiplyNums multiplies two numbers and passes the result to the callback.
const multiplyNums = (cb, x, y) => cb(x * y)
console.log('mulitplyNums [10,10]: ', multiplyNums(identity, 10,10))

// contains checks if an item is present inside of the given array/list.
const contains = (cb, item, list) => list.includes(item) ? cb(true) : cb(false)
console.log('contains [Notebok]: ', contains(identity, 'Notebook', lookInBackback()))
console.log('contains [glock]: ', contains(identity, 'glock', lookInBackback()))

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.
}

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
// removeDuplicates removes all duplicate values from the given array.
// removeDuplicates removes all duplicate values from the given array.
function removeDuplicates(cb, arr) {
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
const uniqueVals = arr.reduce(
(acc,cur) => !(acc.includes(cur)) ? acc.concat(cur) : acc
, [])

return cb(uniqueVals)
}
console.log('removeDuplicates: [* x1000]', removeDuplicates(identity, Array(1000).fill('*')))

/* Bonus Stretch (because I'm a code addict) */

const removeDuplicatesEasyPeasyLemonSqueezy = (cb, arr) => cb(new Set(arr))
// BAM! ES6 BABY!

const lilPumpsMostPopularSongInItsEntirety = repeat(" Gucci Gang ", 100) // (at least...)

console.log('removeDuplicates: [Gucci Gang x100]', removeDuplicatesEasyPeasyLemonSqueezy(identity, lilPumpsMostPopularSongInItsEntirety))



function repeat(str, numTimes) {
const acc = [];
return (function again(repetitions) {
"use strict" // enables TCO
if (repetitions === numTimes) return acc
acc.push(str)
return again(repetitions + 1)
})(0)
}
36 changes: 26 additions & 10 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

const add = a => b => a + b
console.log('Challenge 1: ', add(2)(2))

/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */


// ==== Challenge 2: Create a counter function ====
// Return a function that when invoked increments and returns a counter variable.
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
};
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
var count = 0
return () => ++count
}

const counter1 = counter()
console.log('Challenge2: ', counter1(), counter1(), counter1())


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

const counter2 = counterFactory()()
console.log('Challenge 3:')

const increment = counter => counter.increment
for (let i = 5; i > 0; i--) {
console.log('incrementing... closed over count is now', counter2.increment())
}

console.log('decrementing... closed over count is now', counter2.decrement())