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
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"workbench.colorCustomizations": {
"activityBar.background": "#352D0A",
"titleBar.activeBackground": "#4A400F",
"titleBar.activeForeground": "#FCFAF2"
}
}
29 changes: 28 additions & 1 deletion assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,48 @@ 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 = [];
console.log(fullName);

runners.forEach(function(element){
fullName.push(element.first_name);
fullName.push(element.last_name);

});

console.log(fullName);

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.

Excellent job, Larry. Here is what that same function would look like using es6 arrow syntax, btw (although both ways are totally valid!!):

Suggested change
const fullName = [];
runners.forEach(m => fullName.push(`${m.first_name} ${m.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(currentValue){
return allCaps.push(currentValue.first_name.toUpperCase());

});


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.

Great work here!
Here's the arrow function so you have it:

Suggested change
let allCaps = runners.map(person => person.first_name.toUpperCase());
console.log(allCaps);

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((currentValue) => {
return largeShirts.push(currentValue.shirt_size === "L");
});

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.

Great job here!

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

runners.reduce(function(accumilator, currentValue){

return ticketPriceTotal.push(accumilator + currentValue.donation);
}, 0); // starting point - starts from 0


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.

Good job. FYI the accumulator is a 'foo bar' thing (i.e. you can name it whatever you wish).

console.log(ticketPriceTotal);

// ==== Challenge 5: Be Creative ====
Expand Down
49 changes: 49 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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


/*

//Given this problem:
Expand All @@ -24,19 +25,46 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
return cb(arr);
Comment thread
LarrySimiyu marked this conversation as resolved.
}
getLength(items,arrLength);
function arrLength(arr) {
console.log(arr.length);
}


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

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.

So this function would be your callback function. You seem to be adding your cb into your test functions. Let's talk about this.

cb(arr);
}
last(items, lastFunc);
function lastFunc(arr) {
console.log(arr[arr.length - 1]);
}


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

}
sumNums(2,7,total);
function total(x,y) {
console.log(x+y);

}

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



function contains(item, list, cb) {
// contains checks if an item is present inside of the given array/list.
Expand All @@ -50,3 +78,24 @@ function removeDuplicates(array, cb) {
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
}


// function higherOrderFunction(param1,param2,callback) {
// // console.log(`${greeting}, this is my random number: ${random}`);
// callback(param1,param2);
// }

// higherOrderFunction(3,1,product);

// // Callback function definition
// function add(a,b) {
// console.log(a+b);
// }

// function subtract(a,b) {
// console.log(a-b);
// }

// function product(a,b) {
// console.log(a*b);

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 want to go over callbacks with you.

// }
27 changes: 27 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

function anyFunc(name) {
console.log(name);
let newName = name;

function printName() {
debugger;
console.log(newName);
}
}

anyFunc("larry");


// ==== Challenge 2: Create a counter function ====
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
let count = 0;
return function() {
return count += 1;
}
};
// Example usage: const newCounter = counter();
// newCounter(); // 1
Expand All @@ -17,4 +33,15 @@ const counterFactory = () => {
// Return an object that has two methods called `increment` and `decrement`.
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.

let count = 0;

return {
increment() {
return count += 1;
},
decrement() {
return count -= 1;
}

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.

Good.

}
};