Skip to content

Commit 1cd5282

Browse files
authored
Merge pull request #1 from damola-sd/damola-adewunmi
Damola adewunmi
2 parents 770541a + d91b8e9 commit 1cd5282

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

assignments/array-methods.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,33 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
5656
// ==== Challenge 1: Use .forEach() ====
5757
// 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.
5858
let fullName = [];
59+
runners.forEach( element => {
60+
fullName.push(element.first_name+" " +element.last_name);
61+
62+
});
5963
console.log(fullName);
6064

6165
// ==== Challenge 2: Use .map() ====
6266
// 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
6367
let allCaps = [];
68+
runners.map(function(a) {return a.first_name}).forEach(element => allCaps.push(element.toUpperCase()));
6469
console.log(allCaps);
6570

6671
// ==== Challenge 3: Use .filter() ====
6772
// 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
6873
let largeShirts = [];
74+
largeShirts.push(runners.filter(value => {
75+
if(value.shirt_size === "L") { return value;}
76+
}));
6977
console.log(largeShirts);
7078

7179
// ==== Challenge 4: Use .reduce() ====
7280
// 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
7381
let ticketPriceTotal = [];
82+
function totalTicketPrice (total, amount) {
83+
return total += amount;
84+
}
85+
ticketPriceTotal.push(runners.reduce((total, value) => total += value.donation, 0));
7486
console.log(ticketPriceTotal);
7587

7688
// ==== Challenge 5: Be Creative ====

assignments/callbacks.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,52 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2323
});
2424
2525
*/
26-
26+
function logger(sth) {
27+
console.log(sth)
28+
}
2729

2830
function getLength(arr, cb) {
31+
cb(arr.length);
2932
// getLength passes the length of the array into the callback.
3033
}
34+
getLength(items, logger);
35+
3136

3237
function last(arr, cb) {
3338
// last passes the last item of the array into the callback.
39+
let last_item = items[items.length - 1];
40+
cb(last_item);
3441
}
42+
last(items, logger);
3543

3644
function sumNums(x, y, cb) {
3745
// sumNums adds two numbers (x, y) and passes the result to the callback.
46+
let sum = x + y;
47+
cb(sum);
3848
}
49+
sumNums(5, 6, logger);
3950

4051
function multiplyNums(x, y, cb) {
4152
// multiplyNums multiplies two numbers and passes the result to the callback.
53+
let multiply = x * y;
54+
cb(multiply);
4255
}
4356

57+
multiplyNums(3, 6, logger );
58+
4459
function contains(item, list, cb) {
4560
// contains checks if an item is present inside of the given array/list.
4661
// Pass true to the callback if it is, otherwise pass false.
62+
for (let i = 0; i < list.length; i++) {
63+
if (list[i] === item) {
64+
cb(true);
65+
}
66+
else { cb(false); }
67+
}
4768
}
4869

70+
contains("yo-yo", items, logger);
71+
4972
/* STRETCH PROBLEM */
5073

5174
function removeDuplicates(array, cb) {

assignments/closure.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3-
3+
function add(a, b) { return a + b};
44

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

77

88
// ==== Challenge 2: Create a counter function ====
99
const counter = () => {
1010
// Return a function that when invoked increments and returns a counter variable.
11+
let count = 0;
12+
const newCounter = () => {
13+
count = count++;
14+
console.log(count);
15+
16+
}
17+
return newCounter;
1118
};
1219
// Example usage: const newCounter = counter();
1320
// newCounter(); // 1
@@ -18,4 +25,13 @@ const counterFactory = () => {
1825
// Return an object that has two methods called `increment` and `decrement`.
1926
// `increment` should increment a counter variable in closure scope and return it.
2027
// `decrement` should decrement the counter variable and return it.
28+
let newObj = {
29+
counter: 0,
30+
increment: function () {
31+
return this.counter = this.counter + 1;
32+
},
33+
decrement: function () {
34+
return this.counter = this.counter - 1;
35+
}
36+
}
2137
};

0 commit comments

Comments
 (0)