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
69 changes: 62 additions & 7 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,86 @@ const runners = [
{ id: 50, first_name: "Shell", last_name: "Baine", email: "sbaine1d@intel.com", shirt_size: "M", company_name: "Gabtype", donation: 171 },
];




// ==== 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(people){
return fullNames.push(`${people.first_name} ${people.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 = [];
console.log(firstNamesAllCaps);
runners.map(function(people) {
return firstNamesAllCaps.push(`${people.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.

// ==== 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(function(lrgShirtRunners) {

return lrgShirtRunners.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;
console.log(ticketPriceTotal);
ticketPriceTotal = runners.reduce(function(accumulator, item){
// console.log(`I am the ${accumulator}`);
// console.log(`that is being added to the ${item.donation}`);
return accumulator + item.donation;
},0);
console.log(ticketPriceTotal); //7043

// ==== 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 -- print the names and emails of everyone running the race to the console.
let runnerInfo = [];
runners.forEach(function(information) {
return runnerInfo.push(`${information.first_name} ${information.last_name} has the email ${information.email}`);

}
);

console.log(runnerInfo);

// Problem 2 - who didn't get great donations? name the runners whose donations were less than $20.
// let donorRunners = []
let cheapDonors = [];
cheapDonors = runners.filter(function(donors) {

return donors.donation < 20;

});

console.log(cheapDonors);


// Problem 2

// Problem 3
// console.log
// Problem 3 - filter out the companies whose name start with J and print the name of the runner and their company
38 changes: 36 additions & 2 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,58 @@ 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 theLength = arr => {
return arr.length;
};
console.log(getLength(items,theLength)); //4

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

function theLast(arr) {
return (arr[arr.length - 1]);
}

//could also use .pop()
console.log(last(items,theLast)); //Gum

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x,y);
}
const sum = (x,y) => {
return x + y;
}
console.log(sumNums(2,5,sum)); //7

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) {
const multiply = (x,y) => {
return x * y;
}

console.log(multiplyNums(9,3,multiply)); //27


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);
// Pass true to the callback if it is, otherwise pass false.
}
function itContains(e,arr)
{
return arr.includes(e);
}

console.log(contains("yo-yo",items,itContains));


/* STRETCH PROBLEM */

Expand Down
71 changes: 66 additions & 5 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,90 @@
// The outer scope can be a parent function, or the top level of the script.



function myHandle(twitter) {
const firstName = 'Sooshe';
const lastName = 'Bot';
console.log(`My ${twitter} is ${firstName}${lastName}!`);
}
myHandle('Twitter handle');

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

const counterMaker = () => {
let count = 0;
return function() {
return ++count;
}
};

const counter = counterMaker();
console.log(counter()); //1
console.log(counter()); //2
console.log(counter()); //3


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

//could not figure this one out

const CounterMaker = () => {
let count = 0;
return function() {
if (count >= 1) {
console.log("You have reached the upper limit. Resetting to 1")
count = 0;
}
return ++count;
};

};

const test = CounterMaker();
console.log(test());
console.log(test());
console.log(test());
console.log(test());
console.log(test());
console.log(test());


// ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
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.
let count = 0;
// Return an object that has two methods called `increment` and `decrement`.
return {
// `increment` should increment a counter variable in closure scope and return it.
increment: function increment() {
return ++count;
},
// `decrement` should decrement the counter variable and return it.
decrement: function decrement() {
return --count;
},
}
};


const counterTest = counterFactory();
console.log(counterTest.increment()); //1
console.log(counterTest.increment()); //2
console.log(counterTest.increment()); //3
console.log(counterTest.decrement()); //2
console.log(counterTest.decrement()); //1
console.log(counterTest.decrement()); //0