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
77 changes: 73 additions & 4 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,97 @@ 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.forEach(function firstAndLastName(element) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd recommend naming "element" what it is so the code is more semantic. In this case, each element is a "runner." Similarly, your variable "i" could be called "names" so that the logic is a bit easier to follow. Typically, "i" refers to an index.

let i = element.first_name + " " + element.last_name;
fullName.push(i);
});


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 (runners){
allCaps.push(`${runners.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(runner){
if (`${runner.shirt_size}` === 'L'){
largeShirts.push(runner);
}
})

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 = [];
console.log(ticketPriceTotal);

runners.forEach(function(runner){
ticketPriceTotal.push(runner.donation);
})

console.log(ticketPriceTotal.reduce(function (pv, cv, ci, arr){

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice work!

return pv + cv;
}));

// ==== 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 - In a new array filter the runners and their ids to help identify them as they cross the finish line.

let idAndName = [];

runners.map(function(runner){
idAndName.push(`Runner number ${runner.id} is ${runner.first_name} ${runner.last_name}`);
})

console.log(idAndName);

// Problem 2 - Make arrays that are based on the shirt sizes for the runners. m, s, 2xl, l, xl, xs, 3xl,

let xtraSmall = [];
let small = [];
let medium = [];
let large = [];
let xtraLarge = [];
let twoXlarge = [];

runners.filter(function(runner){
if(`${runner.shirt_size}` === 'XS'){
xtraSmall.push(`XtraSmall: ${runner.first_name} ${runner.last_name}`)
} else if (`${runner.shirt_size}` === 'S'){
small.push(`Small: ${runner.first_name} ${runner.last_name}`)
} else if (`${runner.shirt_size}` === 'M'){
medium.push(`Medium: ${runner.first_name} ${runner.last_name}`)
} else if (`${runner.shirt_size}` === 'L'){
large.push(`Large: ${runner.first_name} ${runner.last_name}`)
} else if (`${runner.shirt_size}` === 'XL'){
xtraLarge.push(`XtraLarge: ${runner.first_name} ${runner.last_name}`)
} else if (`${runner.shirt_size}` === '2XL'){
twoXlarge.push(`2XtraLarge: ${runner.first_name} ${runner.last_name}`)
}
})

console.log(xtraSmall, small, medium, large, xtraLarge, twoXlarge);


// Problem 3 - Sort the runners in alphabetical order.

let sorted = [];

runners.map(function(runner){
sorted.push(`${runner.first_name} ${runner.last_name}`);

// Problem 2
})

// Problem 3
console.log(sorted.sort());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice work on these three problems!

24 changes: 24 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,49 @@ 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);
}

getLength(items, function(first){

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The naming of the argument "first" is a bit confusing here.

console.log(first);
});

function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr.slice(-1).pop());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice! You can also use arr[arr.length-1]

}

last(items, function(first) {
console.log(first)
});

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

sumNums(6, 48, function(add) {
console.log(add)
});

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

multiplyNums(6, 3, function(multiply) {
console.log(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.
return cb (list.includes(item));
}

contains( 'rock', items, console.log);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice work on all these callbacks!


/* STRETCH PROBLEM */

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

function dateOfBirth(month, day, year){

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a great start. I noticed your console.log is a bit off and you're not using myBirth, though. Here's how it looks with those fixed:

function dateOfBirth(month, day, year){
  const myBirth = "The date of my birth is:";

  function myBirthday (){
    return `${myBirth} ${month} ${day}, ${year}`;
  }

  return myBirthday();
}

console.log(dateOfBirth("May", "15", '1993'));

const myBirth = "The date of my birth is:";

function myBirthday (){
return `${month} ${day}, ${year}`;
}

return myBirthday();
}

console.log("May", "15", '1993');

// ==== Challenge 2: Create a counter function ====
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
let count = 0;
return () => (++count);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Very sleek ES6!

};
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
const newCounter = counter();
console.log(newCounter()); // 1
console.log(newCounter()); // 2

/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */

Expand Down
2 changes: 1 addition & 1 deletion assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>JS II</title>

<!--Small change-->
<script src="array-methods.js"></script>
<script src="callbacks.js"></script>
<script src="closure.js"></script>
Expand Down