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
36 changes: 34 additions & 2 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,60 @@ 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( word => fullNames.push(`${word.first_name} ${word.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.map( word => firstNamesAllCaps.push(word.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 = [];
console.log(runnersLargeSizeShirt);
runnersLargeSizeShirt = runners.filter( word => word.shirt_size == "L");
console.log(JSON.stringify(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((count,curentV) => count += curentV.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
// all the runners add 50 more to there donation
let newdonation = runners.map( ele => ele.donation+50)
console.log(newdonation);



// Problem 2
//see how many XL shirt size is there
XL_size = 0;
runners.forEach(ele => {
if (ele.shirt_size == 'XL'){
XL_size++;
}
} )
console.log(XL_size);


// Problem 3
// Problem 3
// take all the names with donation more than 200
let firstname = [];
let heighdonation = runners.filter( elem => elem.donation >= 200);
heighdonation.forEach(elem => firstname.push(elem.first_name));
console.log(firstname)
56 changes: 56 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,85 @@ 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 length(arrlength) {
return console.log(arrlength);
}
getLength(items,length);




function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr[arr.length-1]);
}
function lastitem(last_item){
return console.log(last_item);
}
last(items,lastitem);





function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x + y);
}
function adding(sum){
console.log(sum);
}
sumNums(3,4,adding);



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





function contains(item, list, cb) {
let newword = list.filter(word => item == word);
// contains checks if an item is present inside of the given array/list.
if (newword.length >= 1){
return cb(true);
}else{
return cb(false);
}
// Pass true to the callback if it is, otherwise pass false.
}
function check(bol){
console.log(bol);
}
myword = "yo-yo";
contains(myword, items, check);





/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
let uniqueArray = array.filter((elem, index) => array.indexOf(elem) >= index)
// 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(uniqueArray);
}

function newarray(newarra){
console.log(newarra);
}
removeDuplicates(items,newarray)
46 changes: 46 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
// 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 mixingColors() {
c1 = "blue";
c2 = "yellow";
function green() {
console.log(`mixing ${c1} with ${c2} will give us the color Green`);
c3 = "green";
function cyan() {
console.log(`lights mix ${c3} and ${c1} the result is a cyan color`);
}
cyan();
}
green();
}
mixingColors();





/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
Expand All @@ -16,7 +33,32 @@ 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;
return function counter() {
if (count >= 10) {
count = 0
}
return ++count;
}
};

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


// console.log(myCounter()); // 1
// console.log(myCounter()); // 2
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
Expand All @@ -25,6 +67,10 @@ const counterMaker = () => {
// 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 ====
const counterFactory = () => {
// Return an object that has two methods called `increment` and `decrement`.
Expand Down