Skip to content

Commit 24d8f97

Browse files
Merge pull request #1 from Manny-Rocks/emmanuel-tanifum
Emmanuel tanifum
2 parents 770541a + b070630 commit 24d8f97

3 files changed

Lines changed: 157 additions & 18 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,96 @@ 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-
console.log(fullName);
59+
fullName.push(runners.forEach(function(names){
60+
61+
console.log(`${names.first_name} ${names.last_name} `);
62+
63+
}))
64+
console.log(fullName);
6065

6166
// ==== Challenge 2: Use .map() ====
6267
// 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
6368
let allCaps = [];
64-
console.log(allCaps);
69+
allCaps.push(runners.map(function(names){
70+
71+
console.log(`${names["first_name"].toUpperCase()}`)
72+
73+
}))
74+
75+
console.log(allCaps);
6576

6677
// ==== Challenge 3: Use .filter() ====
6778
// 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
6879
let largeShirts = [];
80+
largeShirts.push(runners.filter((name) => {
81+
82+
if(name["shirt-size"] === "L"){
83+
84+
return name;
85+
86+
}
87+
88+
}))
89+
6990
console.log(largeShirts);
7091

7192
// ==== Challenge 4: Use .reduce() ====
7293
// 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
7394
let ticketPriceTotal = [];
95+
ticketPriceTotal.push(runners.reduce((start,item) =>{
96+
97+
return start += item["donation"];
98+
99+
}, 0
100+
101+
))
74102
console.log(ticketPriceTotal);
75103

76104
// ==== Challenge 5: Be Creative ====
77105
// 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.
78106

79107
// Problem 1
108+
//Manager Needs all the Emails
109+
110+
let Emails = []
111+
112+
Emails.push(runners.forEach(function(item){
113+
114+
return item["email"];
115+
116+
}));
117+
80118

81119
// Problem 2
82120

83-
// Problem 3
121+
// Who are the people that need XS shirts.
122+
123+
const XSShirts = [];
124+
125+
XSShirts.push(runners.filter(function(object){
126+
127+
if(object.shirt_size === "XS"){
128+
129+
console.log (`${object.first_name} ${object.last_name} needs a ${object.shirt_size}`);
130+
131+
}
132+
133+
}))
134+
135+
console.log(XSShirts);
136+
137+
// Problem 3
138+
139+
//The last contestant
140+
141+
let lastContestant = [];
142+
143+
lastContestant.push(runners.filter(function (item,index,array){
144+
145+
if(index === array.length -1){
146+
147+
return item;
148+
149+
}
150+
151+
}))

‎assignments/callbacks.js‎

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,92 @@
1-
// 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.
1+
// Create a callback function and invoke the 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

33
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
4-
54
/*
6-
75
//Given this problem:
86
97
function firstItem(arr, cb) {
108
// firstItem passes the first item of the given array to the callback function.
119
}
12-
1310
// Potential Solution:
14-
15-
// Higher order function using "cb" as the call back
1611
function firstItem(arr, cb) {
1712
return cb(arr[0]);
1813
}
19-
20-
// Function invocation
2114
firstItem(items, function(first) {
2215
console.log(first)
2316
});
24-
2517
*/
2618

27-
2819
function getLength(arr, cb) {
2920
// getLength passes the length of the array into the callback.
21+
return cb(arr.length);
3022
}
3123

24+
getLength(items, function(length) {
25+
console.log(length);
26+
});
27+
28+
3229
function last(arr, cb) {
3330
// last passes the last item of the array into the callback.
31+
return cb(arr[arr.length-1]);
3432
}
3533

34+
last(items, function(last) {
35+
console.log(last);
36+
});
37+
38+
3639
function sumNums(x, y, cb) {
3740
// sumNums adds two numbers (x, y) and passes the result to the callback.
41+
const sum = x+y;
42+
return cb(sum);
3843
}
3944

45+
sumNums(3, 6, function(sum) {
46+
console.log(sum);
47+
});
48+
49+
4050
function multiplyNums(x, y, cb) {
4151
// multiplyNums multiplies two numbers and passes the result to the callback.
52+
const product = x*y;
53+
return cb(product);
4254
}
4355

56+
multiplyNums(3, 6, function(product) {
57+
console.log(product);
58+
});
59+
60+
4461
function contains(item, list, cb) {
4562
// contains checks if an item is present inside of the given array/list.
4663
// Pass true to the callback if it is, otherwise pass false.
64+
if (list.includes(item) == true) {
65+
return cb(true);
66+
} else {
67+
return cb(false);
68+
}
4769
}
4870

71+
contains('Notebook', items, function(included) {
72+
console.log(included);
73+
});
74+
4975
/* STRETCH PROBLEM */
5076

5177
function removeDuplicates(array, cb) {
5278
// removeDuplicates removes all duplicate values from the given array.
5379
// Pass the duplicate free array to the callback function.
5480
// Do not mutate the original array.
81+
let newArr = [];
82+
for (let i=0; i<array.length; i++) {
83+
if (!newArr.includes(array[i])) {
84+
newArr.push(array[i]);
85+
}
86+
}
87+
return cb(newArr);
5588
}
89+
90+
removeDuplicates(items, function(items) {
91+
console.log(items);
92+
});

‎assignments/closure.js‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,55 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3-
4-
5-
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
6-
7-
3+
function firstName(firstName) {
4+
return function(lastName) {
5+
return `${firstName} ${lastName}`;
6+
}
7+
}
8+
const lastName = firstName('Simon');
9+
console.log(lastName('Davis')); // Simon Davis
810
// ==== Challenge 2: Create a counter function ====
911
const counter = () => {
12+
let counter = 0;
13+
return function() {
14+
return ++counter;
15+
}
1016
// Return a function that when invoked increments and returns a counter variable.
1117
};
18+
19+
const newCounter = counter();
20+
console.log(newCounter()); //1
21+
console.log(newCounter()); //2
22+
console.log(newCounter()); //3
1223
// Example usage: const newCounter = counter();
1324
// newCounter(); // 1
1425
// newCounter(); // 2
1526

27+
1628
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
1729
const counterFactory = () => {
30+
let counter = 0;
31+
return {
32+
increment: function() {
33+
return ++counter;
34+
},
35+
decrement: function() {
36+
return --counter;
37+
}
38+
};
1839
// Return an object that has two methods called `increment` and `decrement`.
1940
// `increment` should increment a counter variable in closure scope and return it.
2041
// `decrement` should decrement the counter variable and return it.
2142
};
43+
44+
const counterObj = counterFactory();
45+
46+
console.log(counterObj.increment());
47+
console.log(counterObj.increment());
48+
49+
console.log(counterObj.decrement());
50+
51+
console.log(counterObj.increment());
52+
53+
console.log(counterObj.decrement());
54+
console.log(counterObj.decrement());
55+
console.log(counterObj.decrement());

0 commit comments

Comments
 (0)