Skip to content

Commit ca64bd4

Browse files
authored
Merge pull request #1 from Lydster/lydia-thornton
started javascript-II
2 parents afae86b + c5cbbae commit ca64bd4

File tree

3 files changed

+129
-9
lines changed

3 files changed

+129
-9
lines changed

assignments/array-methods.js

Lines changed: 74 additions & 6 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+
runners.forEach(function(obj) {
60+
fullName.push([obj.first_name + obj.last_name])
61+
return fullName;
62+
})
63+
//console.log(fullName);
64+
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+
runners.map(function(obj) {
70+
allCaps.push(obj.first_name.toUpperCase());
71+
})
72+
//console.log(allCaps);
6573

6674
// ==== Challenge 3: Use .filter() ====
6775
// 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
6876
let largeShirts = [];
69-
console.log(largeShirts);
77+
runners.filter(function(obj) {
78+
if (obj.shirt_size === "L") {
79+
largeShirts.push(obj)
80+
}
81+
return largeShirts;
82+
})
83+
//console.log(largeShirts);
7084

7185
// ==== Challenge 4: Use .reduce() ====
7286
// 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 = [];
74-
console.log(ticketPriceTotal);
87+
88+
let ticketPriceTotal =
89+
runners.reduce(function(total, runner) {
90+
return total + runner.donation
91+
},0)
92+
93+
94+
runners.reduce((total, runner) => {
95+
return total + runner.donation
96+
},0)
97+
98+
99+
100+
//console.log(ticketPriceTotal)
101+
75102

76103
// ==== Challenge 5: Be Creative ====
77104
// 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.
78105

79106
// Problem 1
107+
let companies = []
108+
runners.forEach(function(obj) {
109+
companies.push(obj.company_name)
110+
return companies
111+
})
112+
//console.log(companies)
80113

81114
// Problem 2
82115

83-
// Problem 3
116+
let correctEmail = []
117+
runners.map(function(obj) {
118+
if (obj.email.split("@").length === 2) {
119+
correctEmail.push(obj.email)
120+
}
121+
return correctEmail;
122+
})
123+
//console.log(correctEmail)
124+
125+
// Problem 3 Made a shirt size counter
126+
127+
128+
129+
130+
let shirts = []
131+
runners.forEach(function(obj) {
132+
shirts.push(obj.shirt_size)
133+
return shirts
134+
})
135+
136+
137+
console.log(shirts)
138+
139+
140+
var countedShirts = shirts.reduce(function (allShirts, shirt) {
141+
if (shirt in allShirts) {
142+
allShirts[shirt]++;
143+
}
144+
else {
145+
allShirts[shirt] = 1;
146+
}
147+
return allShirts;
148+
}, {});
149+
150+
151+
console.log(countedShirts)

assignments/callbacks.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,56 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2323

2424

2525
function getLength(arr, cb) {
26+
return cb(arr.length)
2627
// getLength passes the length of the array into the callback.
2728
}
2829

30+
getLength(items, function(length) {
31+
console.log(length);
32+
})
33+
34+
2935
function last(arr, cb) {
36+
return cb(arr[arr.length - 1])
3037
// last passes the last item of the array into the callback.
3138
}
3239

40+
last(items, function(last) {
41+
console.log(last);
42+
})
43+
3344
function sumNums(x, y, cb) {
45+
return cb(x + y)
3446
// sumNums adds two numbers (x, y) and passes the result to the callback.
3547
}
3648

49+
sumNums(6, 5, function(add) {
50+
console.log(add)
51+
})
52+
3753
function multiplyNums(x, y, cb) {
54+
return cb( x * y)
3855
// multiplyNums multiplies two numbers and passes the result to the callback.
3956
}
4057

58+
multiplyNums(5, 6, function(product) {
59+
console.log(product);
60+
})
61+
4162
function contains(item, list, cb) {
63+
if (list.includes(item)) {
64+
return cb(true);
65+
} else {
66+
return cb(false);
67+
}
4268
// contains checks if an item is present inside of the given array/list.
4369
// Pass true to the callback if it is, otherwise pass false.
4470
}
4571

72+
contains('Notebook', items, function(isThere) {
73+
console.log(isThere)
74+
})
75+
4676
/* STRETCH PROBLEM */
4777

4878
function removeDuplicates(array, cb) {

assignments/closure.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,41 @@
22
// Write a simple closure of your own creation. Keep it simple!
33

44

5+
let decrementer = 0;
6+
function sub() {
7+
return decrementer -= 1
8+
}
9+
console.log(sub())
10+
11+
512
// ==== Challenge 2: Create a counter function ====
613
const counter = () => {
714
// Return a function that when invoked increments and returns a counter variable.
15+
let count = 0
16+
return function () {
17+
return count += 1
18+
}
819
};
9-
// Example usage: const newCounter = counter();
10-
// newCounter(); // 1
11-
// newCounter(); // 2
20+
21+
const newCounter = counter();
22+
console.log(newCounter()); // 1
23+
console.log(newCounter()); // 2
1224

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

1527
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
28+
let count = 0
1629
const counterFactory = () => {
30+
31+
let count = 0;
32+
function increment() {
33+
return count += 1
34+
}
35+
function decrement() {
36+
return count -= 1
37+
}
1738
// Return an object that has two methods called `increment` and `decrement`.
1839
// `increment` should increment a counter variable in closure scope and return it.
1940
// `decrement` should decrement the counter variable and return it.
2041
};
42+

0 commit comments

Comments
 (0)