Skip to content

Commit c25fb3a

Browse files
authored
Merge pull request #1 from openwell/timileyin-ojo
added arrray-method solution
2 parents 770541a + 0152186 commit c25fb3a

File tree

3 files changed

+118
-12
lines changed

3 files changed

+118
-12
lines changed

assignments/array-methods.js

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// A local community center is holding a fund raising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.
1+
// A local community center is holding a fund raising 5k fun run and has invited 50 small
2+
// businesses to make a small donation on their behalf for some much needed updates to their facilities.
3+
// Each business has assigned a representative to attend the event along with a small donation.
24

35
// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.
46

@@ -54,30 +56,75 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
5456
{"id":50,"first_name":"Shell","last_name":"Baine","email":"[email protected]","shirt_size":"M","company_name":"Gabtype","donation":171}];
5557

5658
// ==== Challenge 1: Use .forEach() ====
57-
// 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.
59+
// The event director needs both the first and last names of each runner for their running bibs.
60+
// Combine both the first and last names into a new array called fullName.
5861
let fullName = [];
62+
runners.forEach((key)=>{
63+
fullName.push(`${key.first_name} ${key.last_name}`)
64+
})
5965
console.log(fullName);
6066

6167
// ==== Challenge 2: Use .map() ====
62-
// 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
68+
// The event director needs to have all the runner's first names converted to uppercase because the director BECAME DRUNK WITH POWER.
69+
// Convert each first name into all caps and log the result
6370
let allCaps = [];
71+
allCaps = runners.map(all =>{
72+
return all.first_name.toUpperCase();
73+
})
6474
console.log(allCaps);
6575

6676
// ==== Challenge 3: Use .filter() ====
67-
// 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
77+
// The large shirts won't be available for the event due to an ordering issue.
78+
// Get a list of runners with large sized shirts so they can choose a different size.
79+
//Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result
6880
let largeShirts = [];
81+
largeShirts = runners.filter(all =>{
82+
return all.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
7388
let ticketPriceTotal = [];
89+
90+
// ticketPriceTotal = runners.reduce((a,b)=> a.donation + b.donation);
91+
ticketPriceTotal = runners.reduce(function (acc, obj) { return acc + obj.donation; }, 0);
7492
console.log(ticketPriceTotal);
7593

7694
// ==== Challenge 5: Be Creative ====
77-
// 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.
95+
// Now that you have used .forEach(), .map(), .filter(), and .reduce().
96+
// I want you to think of potential problems you could solve given the data set and the 5k fun run theme.
97+
// Try to create and then solve 3 unique problems using one or many of the array methods listed above.
7898

7999
// Problem 1
100+
let clothPerSize = [];
101+
let small =0, medium=0, large=0, xtraLarge=0, xtraSmall=0, xtra2Small=0, xtra3Small = 0;
80102

103+
runners.forEach((key)=>{
104+
if(key.shirt_size === 'S'){
105+
small += 1;
106+
}
107+
if(key.shirt_size === 'M'){
108+
medium += 1;
109+
}
110+
if(key.shirt_size === 'L'){
111+
large += 1;
112+
}
113+
if(key.shirt_size === 'XL'){
114+
xtraLarge += 1;
115+
}
116+
if(key.shirt_size === 'XS'){
117+
xtraSmall += 1;
118+
}
119+
if(key.shirt_size === '2XL'){
120+
xtra2Small += 1;
121+
}
122+
if(key.shirt_size === '3XL'){
123+
xtra3Small += 1;
124+
}
125+
})
126+
// clothPerSize.push({small: small, medium: medium, large: large, xtraLarge: xtraLarge, xtra2Small: xtra2Small, xtra3Small:xtra3Small})
127+
console.log(small, medium, large, xtraLarge, xtraSmall, xtra2Small, xtra3Small);
81128
// Problem 2
82129

83130
// Problem 3

assignments/callbacks.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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 higher order function and invoke the callback function to test your work.
2+
// You have been provided an example of a problem and a solution to see how this works with our items array.
3+
// Study both the problem and the solution to figure out the rest of the problems.
24

3-
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
5+
const items = ["Pencil", "Notebook", "yo-yo", "Gum"];
46

57
/*
68
@@ -24,27 +26,52 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2426
2527
*/
2628

27-
2829
function getLength(arr, cb) {
2930
// getLength passes the length of the array into the callback.
31+
return cb(arr.length);
3032
}
3133

34+
getLength(items, function(first) {
35+
console.log(first)
36+
});
37+
3238
function last(arr, cb) {
39+
const lengthOfArray = arr.length -1;
40+
return cb(arr[lengthOfArray]);
3341
// last passes the last item of the array into the callback.
3442
}
43+
last(items, function(first) {
44+
console.log(first)
45+
});
3546

3647
function sumNums(x, y, cb) {
3748
// sumNums adds two numbers (x, y) and passes the result to the callback.
49+
const additionOfVariable = x + y;
50+
return cb(additionOfVariable);
3851
}
3952

53+
sumNums( 2, 4, function(first) {
54+
console.log(first)
55+
});
56+
4057
function multiplyNums(x, y, cb) {
58+
const multiplyOfVariable = x * y;
59+
return cb(multiplyOfVariable);
4160
// multiplyNums multiplies two numbers and passes the result to the callback.
4261
}
62+
multiplyNums( 2, 4, function(first) {
63+
console.log(first)
64+
});
4365

4466
function contains(item, list, cb) {
4567
// contains checks if an item is present inside of the given array/list.
4668
// Pass true to the callback if it is, otherwise pass false.
69+
return cb(list.includes(item))
4770
}
71+
contains( 'Pencil', items, function(first) {
72+
console.log(first);
73+
});
74+
4875

4976
/* STRETCH PROBLEM */
5077

assignments/closure.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,53 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3-
3+
const addTwoNumbers = (a,b)=>{
4+
return function(){
5+
let initial = a + b;
6+
console.log(initial)
7+
}
8+
}
9+
const newAddTwoNumbers = addTwoNumbers (3,4);
10+
newAddTwoNumbers ();
411

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

7-
814
// ==== Challenge 2: Create a counter function ====
915
const counter = () => {
1016
// Return a function that when invoked increments and returns a counter variable.
17+
let initial = 0;
18+
return function() {
19+
initial += 1;
20+
console.log(initial);
21+
};
1122
};
1223
// Example usage: const newCounter = counter();
13-
// newCounter(); // 1
14-
// newCounter(); // 2
24+
25+
const newCounter = counter();
26+
newCounter();
27+
newCounter();
1528

1629
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
30+
1731
const counterFactory = () => {
32+
let initial = 0;
33+
const factoryObj = {
34+
increment: function() {
35+
initial += 1;
36+
console.log(initial);
37+
},
38+
decrement: function() {
39+
initial -= 1;
40+
console.log(initial);
41+
}
42+
};
43+
return factoryObj;
1844
// Return an object that has two methods called `increment` and `decrement`.
1945
// `increment` should increment a counter variable in closure scope and return it.
2046
// `decrement` should decrement the counter variable and return it.
2147
};
48+
49+
const newCounterFactory = counterFactory();
50+
51+
52+
53+

0 commit comments

Comments
 (0)