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
52 changes: 49 additions & 3 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,76 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
{"id":50,"first_name":"Shell","last_name":"Baine","email":"[email protected]","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 into a new array called fullName.
// 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(firstName, lastName) {
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.

You only need to pass one thing into the function because you are already doing something forEach item you pass

fullName.push(`${firstName.first_name} ${lastName.last_name}`);
});

console.log(fullName);

// ==== Challenge 2: Use .map() ====
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.

use .map (refer to the MDN) documentation for array methods for some examples. Same for .filter

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

const mapped = function(firstname) {
return firstname.first_name.toUpperCase();
}
console.log(mapped);

allCaps = runners.map(mapped);

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

function filterer(size) {
if(size.shirt_size === "L") {
return size;
}
}

largeShirts = runners.filter(filterer);

// ==== 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 = [];

function reducer(accum, cur) {
return accum + cur.donation;
}

ticketPriceTotal = runners.reduce(reducer,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
let largeDoners = []

function donationFilter(num) {
if(num.donation > 100) {
return num;
}
}

largeDoners = runners.filter(donationFilter);
// Problem 2

// Problem 3
totalLargeDoners = []

totalLargeDoners = largeDoners.reduce(reducer, 0);

// Problem 3
let emails = []

runners.forEach(function(email) {
emails.push(email.email);
});

console.log(emails);
38 changes: 35 additions & 3 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

/*
/*

//Given this problem:

//Given this problem:

function firstItem(arr, cb) {
// firstItem passes the first item of the given array to the callback function.
}
Expand All @@ -24,25 +24,57 @@ 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(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]);
}

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

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

const add = function(x,y) {
console.log(x + y);
}

sumNums(1,1,add);

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

const mult = function(x,y) {
console.log(x * y);
}

multiplyNums(2,2,mult);

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 contains.includes(items);
}

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

Everything looks good except for this contains function. Use some console logs here to check your work along the way.

console.log(items);
});

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


/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down
24 changes: 21 additions & 3 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!
function speak(word) {
const slang = word;
console.log(`${slang} my man`);

function firstName(name) {
const nickname = name;
console.log(`${slang} ${nickname}`)
}
firstName('habatchi');
}

speak('Yo');


// ==== Challenge 2: Create a counter function ====
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.

You're almost there for challenge 2, throw some console.logs in there to check your work as you work through your function.

const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
};
const counter = (function(count) {
return function() {
count += 1;
return count;
}
}(0));

console.log(counter());
console.log(counter());
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
Expand Down
4 changes: 2 additions & 2 deletions assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<script src="closure.js"></script>
<script src="stretch-function-conversion.js"></script>
</head>

<!-- This is a test -->
<body>
<h1>JS II - Check your work in the console!</h1>
</body>
</html>
</html>