Skip to content

Commit 1f5b35e

Browse files
Merge pull request #1 from karsevar/mason-karsevar
JavaScript-II problem sets
2 parents bc4a039 + cbedbf8 commit 1f5b35e

File tree

3 files changed

+148
-8
lines changed

3 files changed

+148
-8
lines changed

assignments/array-methods.js

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,104 @@ 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+
/*
60+
the following code is inspired by the MDN example on forEach() and populating
61+
a new array with the method.
62+
63+
with map the code could have been:
64+
const fullName = runners.forEach(runner => {
65+
return fullName.push(runner.first_name + ' ' + runner.last_name);
66+
});
67+
68+
But it seems that such a syntax doesn't work with this method.
69+
*/
70+
const fullName = []
71+
72+
runners.forEach(runner => {
73+
return fullName.push(runner.first_name + ' ' + runner.last_name);
74+
});
5975
console.log(fullName);
6076

6177
// ==== Challenge 2: Use .map() ====
6278
// 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 = [];
79+
let allCaps = runners.map(runner => {
80+
return runner.first_name.toUpperCase();
81+
});
6482
console.log(allCaps);
6583

6684
// ==== Challenge 3: Use .filter() ====
6785
// 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 = [];
86+
let largeShirts = runners.filter(runner => {
87+
return runner.shirt_size == 'L';
88+
});
6989
console.log(largeShirts);
7090

7191
// ==== Challenge 4: Use .reduce() ====
7292
// 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 = [];
93+
let ticketPriceTotal = runners.reduce((acc, currentValue) => {
94+
acc += currentValue.donation;
95+
return acc;
96+
}, 0);
7497
console.log(ticketPriceTotal);
7598

7699
// ==== Challenge 5: Be Creative ====
77100
// 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.
78101

79102
// Problem 1
103+
// Getting the name and contact information of the largest doners. For now above 100 dollars.
104+
let largestDonations = runners.filter(runner => runner.donation >= 100).map(runner => {
105+
return {'first_name': runner.first_name, 'last_name': runner.last_name, 'email': runner.email, 'donation': runner.donation};
106+
});
107+
console.log(largestDonations);
108+
109+
/*
110+
111+
found out that writing arr.map(runner => {key: value}) confusions interpretor.
112+
you need to write object returns in .map() methods as:
113+
114+
arr.map(runner => {
115+
return {key: value};
116+
});
117+
118+
*/
80119

81120
// Problem 2
121+
// How many people wear 'L' T-shirts? With reduce:
122+
let largeCount = runners.reduce((count, currentValue) => {
123+
if (currentValue.shirt_size === 'L') {
124+
count += 1;
125+
return count
126+
}
127+
return count
128+
}, 0)
129+
130+
console.log(largeCount);
131+
// 6 // let's see if this is correct.
132+
133+
let largeCountCheck = runners.filter(runner => {
134+
return runner.shirt_size === 'L';
135+
})
136+
console.log(largeCountCheck.length);
137+
// console.log(largeCountCheck);
138+
// The reduce command was correct the answer was 6 in total.
139+
140+
141+
142+
// Problem 3
143+
// What is the number of donation companies?
144+
145+
/*
146+
147+
Got the following code from:
148+
https://stackoverflow.com/questions/15052702/count-unique-elements-in-array-without-sorting
149+
150+
It uses reduce to find and count the unique values in an array;
151+
152+
*/
82153

83-
// Problem 3
154+
let uniqShirtSizes = runners.map(runner => runner.shirt_size).reduce((acc, val) => {
155+
acc[val] = acc[val] === undefined ? 1 : acc[val] += 1;
156+
return acc;
157+
}, {});
158+
console.log(uniqShirtSizes);

assignments/callbacks.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,42 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2727

2828
function getLength(arr, cb) {
2929
// getLength passes the length of the array into the callback.
30+
return cb(arr.length);
3031
}
32+
// invocation getLength():
33+
getLength(items, length => console.log('The length of the array is: ', length));
3134

3235
function last(arr, cb) {
3336
// last passes the last item of the array into the callback.
37+
return cb(arr[arr.length - 1]);
3438
}
39+
// invocation last():
40+
last(items, last => console.log('The last item in the array is: ', last));
3541

3642
function sumNums(x, y, cb) {
3743
// sumNums adds two numbers (x, y) and passes the result to the callback.
44+
return cb(x + y);
3845
}
46+
//invocation sumNums():
47+
sumNums(1, 2, sum => console.log('The sum of the numbers is: ', sum));
3948

4049
function multiplyNums(x, y, cb) {
4150
// multiplyNums multiplies two numbers and passes the result to the callback.
51+
return cb(x * y);
4252
}
53+
// invocation multiplyNums():
54+
multiplyNums(5, 2, function(product) {
55+
console.log('The product of the numbers is: ', product);
56+
});
4357

4458
function contains(item, list, cb) {
4559
// contains checks if an item is present inside of the given array/list.
4660
// Pass true to the callback if it is, otherwise pass false.
61+
let logic = list.includes(item);
62+
return cb(logic);
4763
}
64+
// invocation contains():
65+
contains('Pencilly', items, truthy => console.log(truthy));
4866

4967
/* STRETCH PROBLEM */
5068

assignments/closure.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3+
const repeatName = (name) => {
4+
return function() {
5+
return name += ' ' + name;
6+
}
7+
}
8+
9+
const name = repeatName('mason');
10+
console.log(name());
11+
console.log(name());
12+
console.log(name());
13+
// Interesting with each iteration the name printed to the console
14+
// doubles inplace of increasing by only one.
315

416

517
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
@@ -8,14 +20,49 @@
820
// ==== Challenge 2: Create a counter function ====
921
const counter = () => {
1022
// Return a function that when invoked increments and returns a counter variable.
23+
let count = 0;
24+
return function() {
25+
return ++count;
26+
}
1127
};
1228
// Example usage: const newCounter = counter();
1329
// newCounter(); // 1
1430
// newCounter(); // 2
1531

32+
const newCounter = counter();
33+
34+
console.log(newCounter());
35+
console.log(newCounter());
36+
console.log(newCounter());
37+
// Works perfectly!!!
38+
1639
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
1740
const counterFactory = () => {
18-
// Return an object that has two methods called `increment` and `decrement`.
19-
// `increment` should increment a counter variable in closure scope and return it.
20-
// `decrement` should decrement the counter variable and return it.
41+
42+
let count = 1;
43+
44+
return {
45+
increment: function() {
46+
return ++count;
47+
},
48+
49+
decrement: function() {
50+
return --count;
51+
}
52+
}
2153
};
54+
55+
const counterObject = counterFactory();
56+
console.log(counterObject.increment());
57+
console.log(counterObject.increment());
58+
console.log(counterObject.increment());
59+
console.log(counterObject.decrement());
60+
console.log(counterObject.decrement());
61+
console.log(counterObject.decrement());
62+
console.log(counterObject.increment());
63+
console.log(counterObject.increment());
64+
65+
66+
// The behavior seems a little weird will need to check the logic.
67+
68+

0 commit comments

Comments
 (0)