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
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229
},
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
36 changes: 31 additions & 5 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// A local community center is holding a fund raising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.
/* A local community center is holding a fund raising 5k fun run and has invited 50 small businesses to make a small donation on their behalf
for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.
*/

// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.

Expand Down Expand Up @@ -58,21 +60,41 @@ const runners = [
// ==== 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 and populate a new array called `fullNames`. This array will contain just strings.
let fullNames = [];
console.log(fullNames);
runners.forEach (runner => {
fullNames.push(`${runner.first_name} ${runner.last_name}`);
});
console.log(JSON.stringify(fullNames));

let firstNameLastName = [];
runners .forEach(runner => {
firstNameLastName.push(`${runner.first_name} ${runner.last_name}`)
});
console.log(firstNameLastName);

// ==== Challenge 2: Use .map() ====
// The event director needs to have all the runners' first names in uppercase because the director BECAME DRUNK WITH POWER. Populate an array called `firstNamesAllCaps`. This array will contain just strings.
let firstNamesAllCaps = [];
runners.map((runner, fName) => {
firstNamesAllCaps[fName] = runner.first_name.toUpperCase();
})
console.log(firstNamesAllCaps);

// ==== Challenge 3: Use .filter() ====
// The large shirts won't be available for the event due to an ordering issue. We need a filtered version of the runners array, containing only those runners with large sized shirts so they can choose a different size. This will be an array of objects.
let runnersLargeSizeShirt = [];
// The large shirts won't be available for the event due to an ordering issue. We need a filtered version of the runners array, containing only those runners
// with large sized shirts so they can choose a different size. This will be an array of objects.
let runnersLargeSizeShirt =[];
runnersLargeSizeShirt = runners.filter(runner => {
return runner.shirt_size === "L";
});
console.log(runnersLargeSizeShirt);

// ==== Challenge 4: Use .reduce() ====
// The donations need to be tallied up and reported for tax purposes. Add up all the donations and save the total into a ticketPriceTotal variable.
let ticketPriceTotal = 0;
ticketPriceTotal = runners.reduce((total, runner)=> {
return total += runner.donation;
},0);

console.log(ticketPriceTotal);

// ==== Challenge 5: Be Creative ====
Expand All @@ -82,4 +104,8 @@ console.log(ticketPriceTotal);

// Problem 2

// Problem 3
// Problem 3
// provide a count of all runners that ordered a size "M" shirt



38 changes: 37 additions & 1 deletion assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Create a higher order function and invoke the callback function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems.
/* Create a higher order function and invoke the callback function to test your work.
You have been provided an example of a problem and a solution to see how this works
with our items array. Study both the problem and the solution to figure out
the rest of the problems.
*/

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

Expand Down Expand Up @@ -41,24 +45,56 @@ 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(arrLength) {
console.log(arrLength);
});

function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr[arr.length - 1]);
}

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

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

sumNums(10,10, function(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(10, 10, function(x,y) {
console.log(x*y);
});

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.
if (list.includes(item)) {
return cb(true);
}else {
return cb(false);
}
}
contains('Pencil', items, function(outcome){
console.log(outcome)
});

contains('Eraser', items, function(outcome){
console.log(outcome)
});

/* STRETCH PROBLEM */

Expand Down
51 changes: 49 additions & 2 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

let a = 4;
function addTip() {
const tipPercentage = a * .15;
const plusTip = a + tipPercentage
console.log(tipPercentage);
return "your total plus tip is: $" + plusTip;
}

console.log(addTip());


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

Expand All @@ -16,18 +26,55 @@ const counterMaker = () => {
// NOTE: This `counter` function, being nested inside `counterMaker`,
// "closes over" the `count` variable. It can "see" it in the parent scope!
// 3- Return the `counter` function.

let count = 0;
return () => {
return ++count;
}
};
const myCounter = counterMaker();
console.log(myCounter());
console.log(myCounter());
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2

// ==== Challenge 3: Make `counterMaker` more sophisticated ====
// It should have a `limit` parameter. Any counters we make with `counterMaker`
// will refuse to go over the limit, and start back at 1.
const counterMakerII = (limit = 3) => {
let count = 1;
return () => {
return count > limit ? (count = 1) : count++;
};
};

myCounterII = counterMakerII();

console.log(myCounterII());
console.log(myCounterII());
console.log(myCounterII());
console.log(myCounterII());


// ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
const counterFactory = () => {
// Return an object that has two methods called `increment` and `decrement`.
// 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.
const counterFactory = () => {
let count = 0;
return {
increment: () => (++count),
decrement: () => (--count),
};
};

myCounterIII = counterFactory();
console.log(myCounterIII.increment());
console.log(myCounterIII.increment());
console.log(myCounterIII.increment());
console.log(myCounterIII.increment());
console.log(myCounterIII.increment());
console.log(myCounterIII.increment());
console.log(myCounterIII.decrement());
console.log(myCounterIII.decrement());