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
67 changes: 62 additions & 5 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,86 @@ 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 = [];
let fullName = runners.forEach(function(element, index) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forEach returns undefined, which is why when you console.log fullName, it comes returns an array of undefined items. How would you mitigate this?

element["first_name"] + " " + element["last_name"];
});
console.log(fullName);



// ==== 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 = [];
let allCaps = runners.map(function(x) {
x.first_name.toUpperCase();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As is, this exercise is not logging out to the console. Why do you think that is?

});
console.log(allCaps);



// ==== 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 = [];
let largeShirts = runners.filter(function(x) {
if(x.shirt_size === "L";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As is, this exercise is not logging out to the console. Why do you think that is?

});
console.log(largeShirts);



// ==== 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 = [];
let ticketPriceTotal = runners.reduce((reducer, runner) => {
return reducer += runner.donation;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for +=, + works here because the reducer method is already aggregating the results of the return.

}, 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 solve 3 unique problems using one or many of the array methods listed above.

// Problem 1
// We need an email list of all Runners to send out annual reminders for our marathon event. Collect all runner's emails.
let emailList = [];

runners.forEach(function(participant) {
emailList.push(participant["donation"]);
});

console.log(emailList);

// Problem 2
// This year, we are splitting the runners into two teams to see which teams time is the fastest. Place even id numbered runners in teamBlue and odd id numbered runners into teamRed.

let teamBlue = [];
let teamRed = [];

runners.filter(function(participant) {
if(participant.id % 2 === 0) {
teamBlue.push(participant);
} else {
teamRed.push(participant);
}
})

console.log(teamBlue);
console.log(teamRed);

// Problem 3
// This year we are making acknowledgements to businesses who donated an oustanding amount to the marathon. If donators donated more than 200, collect their information into the mvpDonator array.

let superNova= [];
let theFlash = [];
let babyCheetah = [];

runners.filter(function(participant) {
if(participant.donation >= 200) {
superNova.push(participant);
} else if(participant.donation >= 100) {
theFlash.push(participant);
} else {
babyCheetah.push(participant)
}
})

// Problem 3
console.log(superNova);
console.log(theFlash);
console.log(babyCheetah);
62 changes: 60 additions & 2 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,88 @@
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

// Made PR

function firstItem(arr, cb) {
// firstItem passes the first item of the given array to the callback function.
return cb(arr);
}

function firstIndex(items) {
return (items[0]);
}

firstItem(items, firstIndex);

//

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't return this value, it will log out as undefined.

}

function arrLength(items) {
return items.length;
}

getLength(items, arrLength);

//

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lastIndex is a function, and you are not using the argument items given to last. What do you think is going on, and how can you fix it?

}

function lastIndex(items) {
return items[items.length - 1];
}

last(items, lastIndex);

//

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's something you need to do here to get the sum to print or return to the console when you run the file. What needs to happen?

}

function sum (a, b) {
return a + b;
}

sumNums(2, 4, sum);

//

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

function multiply(a, b) {
return a * b;
}

multiplyNums(5, 5, multiply);

//

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.
cb(item, list);
}

function listChecker(item, list) {
for(let i = 0; i < list.length; i++) {
if(item === list[i]) {
return true;
}
}
}

contains('Pencil', items, listChecker);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're currently checking if an item exists in the array and returning true if it does, but you're not returning false if the item does not exist.


/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down
23 changes: 23 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

function person() {
var name = "Wonjae";
function anotherPerson() {
console.log("My name is " + name + " too!");
}
anotherPerson();
}

console.log(person());


// ==== Challenge 2: Create a counter function ====
const counter = () => {
let count = 0;
return() => (++count);

// Return a function that when invoked increments and returns a counter variable.

};

const outsideCounter = counter();
console.log(outsideCounter());
console.log(outsideCounter());
console.log(outsideCounter());




// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
Expand Down
10 changes: 9 additions & 1 deletion assignments/function-conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,26 @@
// return param;
// };

let anotherFunction (param) => return param

// let add = function (param1, param2) {
// return param1 + param2;
// };
// add(1,2);

let add = (param1, param2) => param1 + param2;

// let subtract = function (param1, param2) {
// return param1 - param2;
// };
// subtract(1,2);

let subtract = (param1, param2) => param1 - param2;

// exampleArray = [1,2,3,4];
// const triple = exampleArray.map(function (num) {
// return num * 3;
// });
// console.log(triple);
// console.log(triple);

const triple = exampleArray.map(number => number * 3);