Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
51d8cea
created a HOF and callback to return the array items length
CJLucido Sep 17, 2019
456d016
added callbacks for addition, multiplication, and show last HOFs in c…
CJLucido Sep 17, 2019
1355f11
added callback for contains function in callback.js, .includes will w…
CJLucido Sep 17, 2019
fbf86a1
added closure to closure.js, have to call the function with the outer…
CJLucido Sep 17, 2019
1a9523a
made a forEach for runners array so that i could push each of its ite…
CJLucido Sep 17, 2019
61058a9
added uppercase first names to array called firstNamesAllCaps, almost…
CJLucido Sep 17, 2019
f26d3d4
completed challenge 3 of array-methods.js, had a hiccup using filter …
CJLucido Sep 17, 2019
b5509a9
removed for each from filter challenge, it was creating copies of the…
CJLucido Sep 17, 2019
5ddf8a9
fixed challenge 4 of array-methods by removing curly brackets from ar…
CJLucido Sep 17, 2019
7821fa3
fixed challenge 2 of array-methods to use .map instead of forEach
CJLucido Sep 17, 2019
0df60a5
made a new problem that required a filter but this time I just reassi…
CJLucido Sep 17, 2019
8a060ee
added a tax problem that could be solved with map to array-methods
CJLucido Sep 17, 2019
de69046
added last problem to array-methods, was overthinking finding the tot…
CJLucido Sep 17, 2019
84fcc69
made test array for callback.js stretch, solved stretch using a filte…
CJLucido Sep 17, 2019
6158adf
sprint challenge 2 from closure.js solved with help from B.Teague, I …
CJLucido Sep 17, 2019
5a32e62
added limiter to count at count =4
CJLucido Sep 17, 2019
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: 40 additions & 3 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,65 @@ 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 = [];
// runners.forEach(function(item){
// firstNamesAllCaps.push(item.first_name.toUpperCase())
// })

firstNamesAllCaps = runners.map((item) => item.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.push(runners.filter(item => item.shirt_size == "L")); //this wont work without the item => in the filter b/c you haven't given the filter anything to reference to create or look for item.shirt_size


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;

// ticketPriceTotal = runners.reduce(function(acc, currentVal) {return acc += currentVal.donation}, 0);
// console.log(ticketPriceTotal);

ticketPriceTotal = runners.reduce((acc, currentVal) => acc + currentVal.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
// Problem 1: We want to give everyone who donated over 200 dollars a gift basket, get a list of those generous people

let givingPeople = [];

givingPeople = runners.filter(item => item.donation > 200);

console.log(givingPeople);


// Problem 2: actually we want the tax to know exactly how much tax is coming from each donation, make an array that shows how much each person is paying to a 17% tax

let taxMan = [];

taxMan = runners.map((item) => item.donation * .17);

console.log(taxMan);

// Problem 3: We want to know how many people wore 2XL, reduce them to a number!

// Problem 2
let totalMiddleWeights = 0;
let middleWeightsByName = [];
middleWeightsByName = runners.filter((item) => item.shirt_size == "2XL");
console.log(middleWeightsByName);
totalMiddleWeights = middleWeightsByName.length;

// Problem 3
console.log(totalMiddleWeights);
69 changes: 69 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,98 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

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

const arrLength = function(arr){
return arr.length;
}


//////////////MY ATTEMPT at inline did not work////
// function getLength(arr, arr => arr.length);
// const arrLength = arr => arr.length;


console.log(getLength(items, arrLength));




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

const arrLast = function(arr){
return arr[arr.length -1];
}

console.log(last(items, arrLast));




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

const addTwo = function(x,y){
return x + y;
}

console.log(sumNums(1, 2, addTwo));





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

const productTwo = function(x, y){
return x * y;
}

console.log(multiplyNums(3, 4, productTwo));



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

const runThroughList = function(item, list){
return list.includes(item);


}

console.log(contains('Pencil', items, runThroughList));
console.log(contains('Eraser', items, runThroughList));


/* STRETCH PROBLEM */

let testArray = [1, 1, 2, 4, 5, 6, 6, 7, 8]

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

const removeSamsies = function(list){
return list.filter(function(item, index){
return list.indexOf(item) >= index;
})
}

console.log(removeDuplicates(testArray, removeSamsies));
31 changes: 31 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,50 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

function spaceship(shipName){
const position1 = "Navigator";
const position2 = "Cook";

function lifepod(podNum){
console.log(`${podNum} didn't have enough space for more than one person so that person is both the ${position1} and ${position2}.`)
};

lifepod("Pod5");
}
spaceship("crazy horse");


/* 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 = () => {
let count = 0;

return function counter(){ //don't forget the damn RETURN
count += 1;
if (count >= 4){
return count = count - 3;
}else{
return count;}
};



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

const newCounter = counterMaker();
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());

// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
Expand Down