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
6 changes: 3 additions & 3 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.
<!-- * [ ] Create a forked copy of this project.
* [ ] Add your team lead 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>`.
* [ ] 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 All @@ -24,7 +24,7 @@ With some basic JavaScript principles in hand, we can now expand our skills out

This task focuses on getting practice with higher order functions and callback functions by giving you an array of values and instructions on what to do with that array.

* [ ] Review the contents of the [callbacks.js](assignments/callbacks.js) file. Notice you are given an array at the top of the page. Use that array to aid you with your functions.
<!-- * [ ] Review the contents of the [callbacks.js](assignments/callbacks.js) file. Notice you are given an array at the top of the page. Use that array to aid you with your functions. -->

* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.

Expand Down
8 changes: 6 additions & 2 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,25 @@ 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 = [];
runners.forEach(item => fullNames.push(`${item.first_name} ${item.last_name}`));
console.log(fullNames);

// ==== 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(item => firstNamesAllCaps.push(`${item.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 = [];
let runnersLargeSizeShirt = runners.filter(item => (item.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;
let ticketPriceTotal = runners.reduce((acc,val) => {
return acc + val.donation;
},0)
console.log(ticketPriceTotal);

// ==== Challenge 5: Be Creative ====
Expand Down
65 changes: 65 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,94 @@ 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);
}
const test1 = (getLength(items,item=>items.length));
console.log(test1);

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

const test2 = (last(items,item=>items[items.length-1]));
console.log(test2);

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
let result = x+y;
return cb(result);
}
function printSum (sum){
console.log(sum);
}
const test3 = (sumNums(235,15654,printSum));
console.log(test3);


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

function printMult (mult){
console.log(mult);
}
const test4 = (multiplyNums(10,15,printMult));
console.log(test4);






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.
// function checkedPet(item,list) {
let something = false;
for (let i=0; i < list.length; i++) {
if (list[i].includes(item)) {
something = true;
}
}
cb(something);
// }
// cb(checkedPet(item,list));
}

function trueFalse(tf) {
console.log(tf);
}

const pets = ['cat', 'hamster', 'dog', 'mouse', 'hedgehod'];
const test5 = contains('mouse', pets, trueFalse);
console.log(test5);





/* 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 dupArr = [];
array.forEach(function(item){
if(!dupArr.includes(item)){
dupArr.push(item);
}
})
cb(dupArr)
}

const cbFunc = function(thing){
console.log(thing);
}

itemsArr = ['cat', 'hamster', 'dog', 'mouse', 'hedgehod', 'dog', 'horse', 'cat', 'bunny','cat', 'chicken', 'horse'];
const finalArr =removeDuplicates(itemsArr, cbFunc);
console.log(finalArr);
30 changes: 29 additions & 1 deletion assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,48 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

let dog_treat_count = 0;
const dog_treats = function() {
const dog_sits = function() {
dog_treat_count++;
console.log(`Good puppy, you've had ${dog_treat_count} treats now!`)
}
dog_sits();
}


dog_treats();
dog_treats();
dog_treats();
dog_treats();
dog_treats();
dog_treats();

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


// ==== Challenge 2: Implement a "counter maker" function ====
let count = 0;
const counterMaker = () => {
// IMPLEMENTATION OF counterMaker:
// 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`!
// 2- Declare a function `counter`. It should increment and return `count`.
// 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.

function counter() {
return ++count;
}
return counter();
};
// Example usage: const myCounter = counterMaker();
const newCounter = counterMaker;
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());

// Example usage:
// const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2

Expand Down