Skip to content

Commit b2a2e5b

Browse files
committed
working on closures
1 parent afae86b commit b2a2e5b

4 files changed

Lines changed: 101 additions & 8 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,52 @@ 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 => fullName.push(`${element.first_name} ${element.last_name}`));
60+
//console.log(JSON.stringify(fullName))
5961
console.log(fullName);
6062

6163
// ==== Challenge 2: Use .map() ====
6264
// 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
6365
let allCaps = [];
66+
allCaps = runners.map(element => element.first_name.toUpperCase());
6467
console.log(allCaps);
6568

6669
// ==== Challenge 3: Use .filter() ====
6770
// 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
6871
let largeShirts = [];
72+
largeShirts = runners.filter(element => element.shirt_size==="L");
73+
largeShirtsNames = largeShirts.map(element => `${element.first_name} ${element.last_name}:${element.shirt_size}`);
6974
console.log(largeShirts);
75+
console.log(largeShirtsNames);
7076

7177
// ==== Challenge 4: Use .reduce() ====
7278
// 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
79+
80+
// Dedicated function
81+
{
82+
let ticketPriceTotal = [];
83+
ticketPriceTotal = runners.map(element => element.donation);
84+
function add(first,second){
85+
return first + second;
86+
}
87+
ticketPriceTotal = ticketPriceTotal.reduce(add);
88+
console.log(ticketPriceTotal);
89+
}
90+
91+
// Arrow function
7392
let ticketPriceTotal = [];
93+
ticketPriceTotal = runners.map(element => element.donation);
94+
ticketPriceTotal = ticketPriceTotal.reduce( (first,second) => {return first + second});
7495
console.log(ticketPriceTotal);
7596

7697
// ==== Challenge 5: Be Creative ====
7798
// 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.
7899

79100
// Problem 1
101+
// Put donations in packages. Filter, reduce donations, and classify donations by packages
80102

81103
// Problem 2
104+
// Grab the names and filter by last name.
82105

83-
// Problem 3
106+
// Problem 3
107+
// Count the number of shirt sizes

‎assignments/callbacks.js‎

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,82 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2020
});
2121
2222
*/
23-
23+
a = 2;
24+
b = 3;
2425

2526
function getLength(arr, cb) {
2627
// getLength passes the length of the array into the callback.
28+
return cb(arr.length);
2729
}
30+
getLength(items,console.log)
2831

2932
function last(arr, cb) {
3033
// last passes the last item of the array into the callback.
34+
return cb(arr[arr.length - 1]);
3135
}
36+
last(items,console.log);
3237

3338
function sumNums(x, y, cb) {
3439
// sumNums adds two numbers (x, y) and passes the result to the callback.
40+
return cb(x + y);
3541
}
42+
sumNums(a,b,console.log);
3643

3744
function multiplyNums(x, y, cb) {
3845
// multiplyNums multiplies two numbers and passes the result to the callback.
46+
return cb(x * y);
3947
}
48+
multiplyNums(a,b,console.log)
4049

4150
function contains(item, list, cb) {
4251
// contains checks if an item is present inside of the given array/list.
4352
// Pass true to the callback if it is, otherwise pass false.
53+
let present = null;
54+
if(list.indexOf(item) > -1){
55+
present = true;
56+
} else {
57+
present = false;
58+
}
59+
return cb(present)
4460
}
61+
contains("Gum",items,console.log)
62+
contains("candy",items,console.log)
4563

4664
/* STRETCH PROBLEM */
47-
48-
function removeDuplicates(array, cb) {
49-
// removeDuplicates removes all duplicate values from the given array.
65+
// removeDuplicates removes all duplicate values from the given array.
5066
// Pass the duplicate free array to the callback function.
5167
// Do not mutate the original array.
68+
69+
arrayWithDup = [1,1,2,3,4,5,3,5,6,1]
70+
71+
// Need to work on a filter method with a query
72+
73+
// function comparerFilter(list){
74+
// tmpArray = [];
75+
// newArray = list.filter(function(element){
76+
// if(tmpArray.indexOf(element) > -1){
77+
// return false;
78+
// } else {
79+
// tmpArray.push()
80+
// return true;
81+
// }
82+
// })
83+
84+
function comparerFor(list){
85+
tmpArray = [];
86+
for(i=0;i<list.length;i++){
87+
if(tmpArray.indexOf(list[i]) > -1){
88+
continue;
89+
} else {
90+
tmpArray.push(list[i]);
91+
}
92+
}
93+
return tmpArray;
94+
}
95+
96+
// The callback function is a function that is used to identify and remove the duplicates.
97+
function removeDuplicates(array, cb) {
98+
nonDupArray = cb(array);
99+
return nonDupArray;
52100
}
101+
console.log(removeDuplicates(arrayWithDup, comparerFor));

‎assignments/closure.js‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3-
3+
hello = "Hello"
4+
function Greet(param){
5+
greeting = param;
6+
console.log(greeting);
7+
function Introduction(){
8+
console.log(`${greeting}, my name is Dan`);
9+
}
10+
Introduction();
11+
}
12+
Greet(hello);
413

514
// ==== Challenge 2: Create a counter function ====
615
const counter = () => {
7-
// Return a function that when invoked increments and returns a counter variable.
16+
let count = 0;// Return a function that when invoked increments and returns a counter variable.
17+
// debugger;
18+
function Counting(){
19+
// debugger;
20+
return count += 1;
21+
}
22+
Counting()
23+
return count;
24+
// debugger;
825
};
926
// Example usage: const newCounter = counter();
1027
// newCounter(); // 1
1128
// newCounter(); // 2
29+
const newCounter = counter;
30+
console.log(counter());
31+
console.log(counter());
1232

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

‎assignments/index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<title>JS II</title>
99

10-
<script src="array-methods.js"></script>
10+
<!-- <script src="array-methods.js"></script> -->
1111
<script src="callbacks.js"></script>
1212
<script src="closure.js"></script>
1313
<script src="stretch-function-conversion.js"></script>

0 commit comments

Comments
 (0)