Skip to content

Commit fe08deb

Browse files
author
Yeshi
committed
completed
1 parent 9f42464 commit fe08deb

4 files changed

Lines changed: 69 additions & 12 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,25 @@ console.log(ticketPriceTotal);
480480
// 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.
481481

482482
// Problem 1
483+
// find the email of "Vida"
484+
runners.forEach(ele => {
485+
if (ele.first_name === "Vida") {
486+
console.log(ele.email);
487+
}
488+
});
483489

484490
// Problem 2
485-
491+
// filter all companies with names starting with "J"
492+
let jCompany = runners.filter(ele => {
493+
if (ele.company_name[0] === "J") {
494+
return ele.company_name;
495+
}
496+
});
497+
console.log(jCompany);
486498
// Problem 3
499+
let lessThan150 = runners.filter(ele => {
500+
if (ele.donation < 150) {
501+
return ele.donation;
502+
}
503+
});
504+
console.log(lessThan150);

‎assignments/callbacks.js‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 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.
22

3-
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
3+
const items = ["Pencil", "Notebook", "yo-yo", "Gum"];
44

55
/*
66
@@ -24,28 +24,47 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2424
2525
*/
2626

27-
2827
function getLength(arr, cb) {
2928
// getLength passes the length of the array into the callback.
29+
cb(arr.length);
3030
}
31+
getLength(items, function(length) {
32+
console.log(length);
33+
});
3134

3235
function last(arr, cb) {
3336
// last passes the last item of the array into the callback.
37+
cb(arr[arr.length - 1]);
3438
}
35-
39+
last(items, function(last) {
40+
console.log(last);
41+
});
3642
function sumNums(x, y, cb) {
3743
// sumNums adds two numbers (x, y) and passes the result to the callback.
44+
return cb(x, y);
3845
}
39-
46+
function add(a, b) {
47+
return a + b;
48+
}
49+
console.log(sumNums(2, 4, add));
4050
function multiplyNums(x, y, cb) {
4151
// multiplyNums multiplies two numbers and passes the result to the callback.
4252
}
4353

4454
function contains(item, list, cb) {
4555
// contains checks if an item is present inside of the given array/list.
4656
// Pass true to the callback if it is, otherwise pass false.
57+
cb(item, list);
4758
}
48-
59+
function find(b, arr) {
60+
for (let i = 0; i < arr.length; i++) {
61+
if (arr[i] === b) {
62+
return true;
63+
}
64+
return false;
65+
}
66+
}
67+
console.log(contains("Pencil", items, find));
4968
/* STRETCH PROBLEM */
5069

5170
function removeDuplicates(array, cb) {

‎assignments/closure.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ execute();
1414
// ==== Challenge 2: Create a counter function ====
1515
const counter = () => {
1616
// Return a function that when invoked increments and returns a counter variable.
17+
let count = 0;
18+
return () => ++count;
1719
};
20+
const newCounter = counter();
21+
console.log(newCounter());
22+
console.log(newCounter());
23+
console.log(newCounter());
1824
// Example usage: const newCounter = counter();
1925
// newCounter(); // 1
2026
// newCounter(); // 2
@@ -24,4 +30,14 @@ const counterFactory = () => {
2430
// Return an object that has two methods called `increment` and `decrement`.
2531
// `increment` should increment a counter variable in closure scope and return it.
2632
// `decrement` should decrement the counter variable and return it.
33+
let increment = 0;
34+
let decrement = 0;
35+
return () => {
36+
console.log(++increment);
37+
console.log(--decrement);
38+
};
2739
};
40+
let x = counterFactory();
41+
x();
42+
x();
43+
x();
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax
22

33
// let myFunction = function () {};
4-
4+
let myFunction = () => {};
55
// let anotherFunction = function (param) {
66
// return param;
77
// };
8-
8+
let anotherFunction = param => param;
99
// let add = function (param1, param2) {
1010
// return param1 + param2;
1111
// };
1212
// add(1,2);
13-
13+
add = (param1, param2) => param1 + param2;
14+
add(1, 2);
1415
// let subtract = function (param1, param2) {
1516
// return param1 - param2;
1617
// };
1718
// subtract(1,2);
18-
19-
// exampleArray = [1,2,3,4];
19+
let subtract = (param1, param2) => param1 - param2;
20+
subtract(1, 2);
21+
let exampleArray = [1, 2, 3, 4];
2022
// const triple = exampleArray.map(function (num) {
2123
// return num * 3;
2224
// });
23-
// console.log(triple);
25+
// console.log(triple);
26+
const triple = exampleArray.map(num => num * 3);
27+
console.log(triple);

0 commit comments

Comments
 (0)