Skip to content

Merge Jason-Barr to master #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ With some basic JavaScript principles in hand, we can now expand our skills out

**Follow these steps to set up and work on your project:**

* [ ] Create a forked copy of this project.
* [ ] Add your project manager as collaborator on Github.
* [ ] Clone your OWN version of the repository (Not Lambda's by mistake!).
* [ ] Create a new branch: git checkout -b `<firstName-lastName>`.
* [x] Create a forked copy of this project.
* [x] Add your project manager as collaborator on Github.
* [x] Clone your OWN version of the repository (Not Lambda's by mistake!).
* [x] Create a new branch: git checkout -b `<firstName-lastName>`.
* [ ] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
* [ ] Push commits: git push origin `<firstName-lastName>`.

Expand Down Expand Up @@ -50,4 +50,4 @@ We have learned that closures allow us to access values in scope that have alrea
## Stretch Goals

* [ ] Go back through the stretch problems that you skipped over and complete as many as you can.
* [ ] Look up what an IIFE is in JavaScript and experiment with them
* [ ] Look up what an IIFE is in JavaScript and experiment with them
12 changes: 9 additions & 3 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,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.forEach(runner => fullName.push(`${runner.first_name} ${runner.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 = [];
allCaps = fullName.map(name => {
let [first, last] = name.split(' ');
return `${first.toUpperCase()} ${last}`

});
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 = [];
let largeShirts = runners.filter(runner => runner.shirt_size !== 'XL');
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 = [];
let ticketPriceTotal = runners.reduce((total, runner) => total + runner.donation, 0);
console.log(ticketPriceTotal);

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

// Problem 2

// Problem 3
// Problem 3
68 changes: 55 additions & 13 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

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

/**
* Recursive implementation of Array.prototype.map
*
* @param {Function} fn Callback to transform array elements
* @param {Array} param1 Array destructured into head and ...tail
* @return {Array} Transformed array
*/
const recursiveMap = function (fn, [head, ...tail]) {
if (head === undefined && tail.length < 1) {
return [];
}

return [fn(head), ...recursiveMap(fn, tail)];
}

console.log(recursiveMap(function(str) {
return str.toLowerCase();
}, items));

/*

//Given this problem:
Expand All @@ -18,38 +37,61 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
}

// Function invocation
firstItem(items, function(first) {
console.log(first)
});

*/

/**
* Takes a Strings and returns the first letter
*
* @param {String} str a string
* @returns {String} first letter
*/
const firstLetter = function (str) {
return str[0];
}

*/
// Process first item in array
function firstItem(items, fn) {
return fn(items[0]);
}

// Get first letter of first in an array of strings
console.log(firstItem(items, firstLetter));

function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
return cb(arr.length);
}

function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(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);
}

function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
return cb(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.
return cb(list.includes(item));
}

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
const removed = [];
for (let i = 0; i < array.length; i++) {
if (!removed.includes(array[i])) {
removed.push(array[i]);
}
}

return removed;
}

// Alternatively
function removeDuplicatesV2(array, fn) {
return fn(Array.from(new Set(array)));
}
21 changes: 20 additions & 1 deletion assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!
const createHTMLElementFactory = function(elem) {
return function(text) {
const htmlElem = document.createElement(elem);
htmlElem.textContent = text; // undefined if not set with arg is the desired behavior
return htmlElem;
};
}

// example usage
const h1 = createHTMLElementFactory('h1');
const pageHeader = h1('Welcome to my page');

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


// ==== Challenge 2: Create a counter function ====
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
let counterVar = 0;
return function() {
return ++counterVar; // append increment operator so it increments then returns
}
};

// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
Expand All @@ -18,4 +32,9 @@ 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 i = 1;
return Object.assign({
increment: () => ++i,
decrement: () => --i,
})
};
3 changes: 1 addition & 2 deletions assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
<script src="array-methods.js"></script>
<script src="callbacks.js"></script>
<script src="closure.js"></script>
<script src="stretch-function-conversion.js"></script>
</head>

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