Skip to content

Commit c3daa6d

Browse files
Merge pull request #1 from SaschaMajewsky/sascha-majewsky
Sascha majewsky
2 parents 770541a + 6012afd commit c3daa6d

4 files changed

Lines changed: 213 additions & 11 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,60 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
5555

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.
58-
let fullName = [];
58+
59+
/* version one */
60+
61+
/* const fullName = runners.map((person) => {
62+
return `${person.first_name} ${person.last_name}`;
63+
});
64+
*/
65+
66+
const fullName = []
67+
68+
runners.forEach(person => fullName.push(`${person.first_name} ${person.last_name}`));
69+
5970
console.log(fullName);
6071

6172
// ==== Challenge 2: Use .map() ====
6273
// 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
63-
let allCaps = [];
74+
75+
const allCaps = runners.map(person => `${person.first_name.toUpperCase()}`);
76+
6477
console.log(allCaps);
6578

6679
// ==== Challenge 3: Use .filter() ====
6780
// 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
68-
let largeShirts = [];
81+
82+
const largeShirts = runners.filter(person => person.shirt_size === 'L');
83+
6984
console.log(largeShirts);
7085

7186
// ==== Challenge 4: Use .reduce() ====
7287
// 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
73-
let ticketPriceTotal = [];
88+
const ticketPriceTotal = runners.reduce((acc, currentPerson) => {
89+
return acc += currentPerson.donation;
90+
}, 0);
91+
7492
console.log(ticketPriceTotal);
7593

7694
// ==== Challenge 5: Be Creative ====
7795
// 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.
7896

7997
// Problem 1
98+
//Show each name of the student and how much they donated in USD notation.
99+
const studentDonation = runners.map(person => `${person.first_name} ${person.donation}.00 USD`);
80100

101+
console.log(studentDonation);
81102
// Problem 2
103+
//There was an error with sending out some emails to students, we need info about all students with an email from the provider yale.edu. Quick! We need to get this fixxed.
104+
const studentEmailFromYale = runners.filter(person => person.email.includes('yale.edu'));
105+
106+
console.log(studentEmailFromYale);
107+
// Problem 3
108+
// We need a list of all students with their name and their donations next to it as an Array containing each of them in an object.
109+
110+
const DonationsAsJSON = runners.map((person) => {
111+
return { name: `${person.first_name}`, donation: `${person.donation}`};
112+
});
82113

83-
// Problem 3
114+
console.log(DonationsAsJSON);

‎assignments/callbacks.js‎

Lines changed: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
44

5-
/*
65

6+
/*
77
//Given this problem:
88
99
function firstItem(arr, cb) {
@@ -20,36 +20,169 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2020
// Function invocation
2121
firstItem(items, function(first) {
2222
console.log(first)
23-
});
23+
}); */
24+
25+
2426

25-
*/
2627

28+
/* function getLength(arr, cb) {
29+
// getLength passes the length of the array into the callback.
30+
return cb(arr);
31+
} */
32+
33+
/* version one */
2734

35+
/*
36+
function length(arr) {
37+
console.log(arr.length);
38+
}
39+
40+
getLength(items, length); */
41+
42+
/* version two */
43+
44+
/* getLength(items, function length(arr) {
45+
console.log(arr.length);
46+
});
47+
*/
2848
function getLength(arr, cb) {
2949
// getLength passes the length of the array into the callback.
50+
return cb(arr.length);
51+
}
52+
53+
getLength(items, console.log);
54+
55+
/* ---------------------------------------------------------------- */
56+
57+
/* function last(arr, cb) {
58+
// last passes the last item of the array into the callback.
59+
return cb(arr[arr]);
60+
}
61+
*/
62+
/* version one */
63+
64+
/* function lastItem(arr) {
65+
console.log(arr[arr.length -1]);
3066
}
3167
68+
last(items, lastItem); */
69+
70+
/* version two */
71+
72+
/* last(items, function lastItem(arr) {
73+
console.log(arr[arr.length -1]);
74+
}); */
75+
76+
3277
function last(arr, cb) {
3378
// last passes the last item of the array into the callback.
79+
return cb(arr[arr.length -1]);
3480
}
3581

82+
last(items, console.log);
83+
84+
85+
/* ---------------------------------------------------------------- */
86+
87+
/* function sumNums(x, y, cb) {
88+
// sumNums adds two numbers (x, y) and passes the result to the callback.
89+
return cb(x, y);
90+
} */
91+
92+
/* There are some problems with this version!
93+
function sum(x,y){
94+
return cb(x + y);
95+
}
96+
97+
sumNums(2, 3, sum);
98+
*/
99+
100+
/* version two */
101+
102+
/* sumNums(2, 3, function(x, y) {
103+
console.log(x + y)
104+
});
105+
*/
106+
36107
function sumNums(x, y, cb) {
37108
// sumNums adds two numbers (x, y) and passes the result to the callback.
109+
return cb(x + y);
110+
}
111+
112+
sumNums(2, 3, console.log);
113+
114+
/* ---------------------------------------------------------------- */
115+
116+
/* function multiplyNums(x, y, cb) {
117+
// multiplyNums multiplies two numbers and passes the result to the callback.
118+
return cb(x, y);
119+
} */
120+
121+
/* There are some problems with this version!
122+
function multiply(x, y){
123+
console.log(x * y);
38124
}
39125
126+
multiplyNums(2, 3, multiply(x, y));
127+
*/
128+
129+
/* version two */
130+
131+
/* multiplyNums(2, 3, function(x, y) {
132+
console.log(x * y)
133+
}); */
134+
40135
function multiplyNums(x, y, cb) {
41136
// multiplyNums multiplies two numbers and passes the result to the callback.
137+
return cb(x * y);
138+
}
139+
140+
multiplyNums(2, 3, console.log);
141+
142+
/* ---------------------------------------------------------------- */
143+
/*
144+
function contains(item, list, cb) {
145+
// contains checks if an item is present inside of the given array/list.
146+
// Pass true to the callback if it is, otherwise pass false.
147+
return cb(item, list)
148+
} */
149+
150+
/* version one */
151+
152+
/* function containCheck(item, list) {
153+
console.log(list.includes(item));
42154
}
43155
156+
contains("Pencil", items, containCheck); */
157+
158+
/* version two */
159+
160+
/* contains("Pencil", items, function(item, list) {
161+
console.log(list.includes(item));
162+
});
163+
*/
164+
44165
function contains(item, list, cb) {
45166
// contains checks if an item is present inside of the given array/list.
46167
// Pass true to the callback if it is, otherwise pass false.
168+
return cb(list.includes(item))
47169
}
48170

171+
contains("Pencil", items, console.log);
172+
173+
49174
/* STRETCH PROBLEM */
50175

51176
function removeDuplicates(array, cb) {
52177
// removeDuplicates removes all duplicate values from the given array.
53178
// Pass the duplicate free array to the callback function.
54179
// Do not mutate the original array.
180+
return cb(array.filter((value, index, self) => self.indexOf(value) === index))
55181
}
182+
183+
const itemsCheated = ['Notebook', 'Pencil','Gum', 'Pencil', 'Notebook', 'yo-yo', 'Gum', 'Notebook'];
184+
185+
removeDuplicates(itemsCheated, console.log);
186+
187+
188+

‎assignments/closure.js‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,52 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3-
3+
const cat = 'kittie';
4+
function returnCat () {
5+
let inside_variable = 'so much private';
6+
let cat_variable = cat;
7+
return cat_variable;
8+
}
9+
console.log(returnCat());
10+
/* console.log(inside_variable); */ //is not defined bercause the variable in within the function scope
411

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

714

815
// ==== Challenge 2: Create a counter function ====
916
const counter = () => {
1017
// Return a function that when invoked increments and returns a counter variable.
18+
let count = 0;
19+
return function() {
20+
return ++count;
21+
}
1122
};
23+
24+
const newCounter = counter();
25+
console.log(newCounter()); // 1
26+
console.log(newCounter()); // 2
27+
1228
// Example usage: const newCounter = counter();
1329
// newCounter(); // 1
1430
// newCounter(); // 2
1531

1632
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
17-
const counterFactory = () => {
33+
34+
// doesn't work so far
35+
36+
/* const counterFactory = (param) => {
1837
// Return an object that has two methods called `increment` and `decrement`.
1938
// `increment` should increment a counter variable in closure scope and return it.
2039
// `decrement` should decrement the counter variable and return it.
21-
};
40+
let countObj = { counter: 0,
41+
increment: function() {return this.counter +1},
42+
decrement: function() {return this.counter -1},
43+
};
44+
return function(param) {
45+
return param === 'increment' ? countObj.increment() : countObj.decrement();
46+
}
47+
};
48+
49+
const newCounterFactory = counterFactory();
50+
console.log(newCounterFactory('increment')); // 1
51+
console.log(newCounterFactory('increment')); // 2
52+
console.log(newCounterFactory('decrement')); // 1 */

‎assignments/iife-example.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// IIFE stands for Immediately Invoked Function Expression
2+
//It does not get invoked, only referenced thus you don't need the () after the reference
3+
//you can reference to this function alone by its name
4+
let greet = (() => 'Hello, my name is Sascha!')();
5+
6+
console.log(greet);
7+

0 commit comments

Comments
 (0)