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: 31 additions & 5 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,27 @@ 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 = runners.map((name, surName) => {
return {name: runners["first_name"], surName: runners["last_name"]};
let fullName = [];runners.forEach(function (info) {
fullName.push (`${info.first_name} ${info.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 = [];
runners.map(function(info) {
allCaps.push(info.first_name.toUpperCase());
})
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 = [];
runners.filter(function(info) {
if (info.shirt_size === "L")
return largeShirts.push(`${info.first_name} ${info.last_name} ordered a Large shirt`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice interpolation 🔥

});
console.log(largeShirts);

// ==== Challenge 4: Use .reduce() ====
Expand All @@ -78,8 +86,26 @@ 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 Need a party invite list for participants who donated over 200
let partyList = [];
runners.map(function(info) {
if (info.donation > 200)
return partyList.push(`${info.first_name} ${info.last_name} should be added to the guest list`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here it would be good practice to save runners.map(...) in a variable and reassign partyList like so: partyList = guestListRunners. Using .push() inside of a .map() isn't the best method 👍

});
console.log(partyList);

// Problem 2
// Problem 2 Email list of people who ordered small shirts
let smallShirts = [];
runners.filter(function(info) {
if (info.shirt_size === "S")
return smallShirts.push(`${info.first_name} ${info.email}`);
});
console.log(smallShirts);

// Problem 3
// Problem 3 number of people from Skinix company
let companySkinix = [];
runners.map(function(info) {
if (info.company_name === "Skinix")
return companySkinix.push(` ${info.company_name}: ${info.first_name} ${info.last_name}`);
});
console.log(companySkinix);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good stuff!! 🔥

55 changes: 28 additions & 27 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,41 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

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

// Potential Solution:

// Higher order function using "cb" as the call back
function firstItem(arr, cb) {
return cb(arr[0]);
function getInfo(arr, cb) {
return cb(arr);
// return cb(arr[0]);
}

// Function invocation
firstItem(items, function(first) {
console.log(first)
});
function firstItem(info) {
return info[0];
};

*/
console.log(getInfo(items, firstItem));


function getLength(arr, cb) {
function getLength(info) {
// getLength passes the length of the array into the callback.
return cb(arr.length);
return info.length;
}
getLength(items, (length) => {
console.log(length);
})
console.log(getInfo(items, getLength));
// getLength(items, (length) => {
// console.log(length);
// })

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


function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
Expand All @@ -57,22 +60,20 @@ multiplyNums(2, 3, (callBack) => {
console.log(callBack);
})

function contains(item, list, cb) {
// 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.
}

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

function contains (item, list, cb) {
if (item === list[i]) {
return cb(list[i]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here you have not defined what i is so it is giving you an error. The situation that we could potentially have defined i was if we used for loops. Instead, we could check the list for the item using .includes():

if (list.includes(item)) {
  return true;
}
else {
  return false;
}

}
}

// contains('Gum', items, (callBack) => {
// console.log(callBack);
// });
contains('Gum', items, (callBack) => {
console.log(callBack);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Interesting way of doing this. 👍

});

/* STRETCH PROBLEM */

Expand Down
2 changes: 1 addition & 1 deletion assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<script src="array-methods.js"></script>
<script src="callbacks.js"></script>
<script src="closure.js"></script>
<script src="stretch-function-conversion.js"></script>
<!-- <script src="stretch-function-conversion.js"></script> -->
</head>

<body>
Expand Down