Skip to content

Commit 7b26b2e

Browse files
author
Jacob Angulo
committed
more stuff, javascript is magic and im a muggle
1 parent cfd1201 commit 7b26b2e

4 files changed

Lines changed: 84 additions & 10 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,46 @@ 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+
let fullName = runners.forEach((item) => {
59+
return `${item["first_name"]} ${item["last_name"]}`;
60+
});
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
63-
let allCaps = [];
65+
let allCaps = runners.map((item) => {
66+
return item["first_name"].toUpperCase();
67+
});
6468
console.log(allCaps);
6569

6670
// ==== Challenge 3: Use .filter() ====
6771
// 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 = [];
72+
let largeShirts = runners.filter((item) => {
73+
if(item["shirt_size"] == "L") {
74+
return item
75+
}
76+
});
6977
console.log(largeShirts);
7078

7179
// ==== Challenge 4: Use .reduce() ====
7280
// 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 = [];
81+
let ticketPriceTotal = runners.reduce((a, b) => {
82+
return a + b["donation"];
83+
}, 0);
7484
console.log(ticketPriceTotal);
7585

7686
// ==== Challenge 5: Be Creative ====
7787
// 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.
7888

7989
// Problem 1
90+
// someone wants a list of all the runner's emails
91+
92+
let getEmaiList = runners.forEach((item) => {
93+
return item["email"];
94+
})
95+
96+
console.log(getEmaiList)
97+
8098

8199
// Problem 2
82100

‎assignments/callbacks.js‎

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

3-
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
6+
const items = ['Pencil', 'Notebook', 'Pencil', 'yo-yo', 'Gum'];
47

58
/*
69
@@ -24,29 +27,73 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2427

2528
function getLength(arr, cb) {
2629
// getLength passes the length of the array into the callback.
30+
return cb(arr);
2731
}
2832

33+
getLength(items, function(param) {
34+
console.log(param.length)
35+
});
36+
2937
function last(arr, cb) {
3038
// last passes the last item of the array into the callback.
39+
return cb(arr);
3140
}
3241

42+
last (items, function(param) {
43+
console.log(param[param.length - 1])
44+
})
45+
3346
function sumNums(x, y, cb) {
3447
// sumNums adds two numbers (x, y) and passes the result to the callback.
48+
return cb(x + y)
3549
}
3650

51+
sumNums(1, 2, (param) => {
52+
console.log(param)
53+
})
54+
3755
function multiplyNums(x, y, cb) {
3856
// multiplyNums multiplies two numbers and passes the result to the callback.
57+
return cb(x, y)
3958
}
4059

60+
multiplyNums(2, 3, (a, b) => {
61+
console.log(a * b);
62+
})
63+
4164
function contains(item, list, cb) {
4265
// contains checks if an item is present inside of the given array/list.
4366
// Pass true to the callback if it is, otherwise pass false.
67+
console.log(cb(item, list));
4468
}
4569

70+
contains('Pencil', items, (inquiry, subject) => {
71+
for (let i = 0; i < subject.length; i++) {
72+
if (subject[i] == inquiry) {
73+
return true;
74+
} else {
75+
return false;
76+
}
77+
}
78+
})
79+
4680
/* STRETCH PROBLEM */
4781

4882
function removeDuplicates(array, cb) {
83+
console.log(cb(array))
4984
// removeDuplicates removes all duplicate values from the given array.
5085
// Pass the duplicate free array to the callback function.
5186
// Do not mutate the original array.
5287
}
88+
89+
removeDuplicates(items, (param) => {
90+
let paramCopy = param.slice();
91+
for(let i = 0; i < param.length; i++) {
92+
for (let j = 0; j < param.length; j++) {
93+
if(i !== j && param[i] == param[j]) {
94+
paramCopy.splice(i, 1)
95+
}
96+
}
97+
}
98+
return paramCopy;
99+
})

‎assignments/closure.js‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@
44

55
// ==== Challenge 2: Create a counter function ====
66
const counter = () => {
7+
let count = 0;
8+
return () => {
9+
++count;
10+
console.log(count);
11+
}
712
// Return a function that when invoked increments and returns a counter variable.
813
};
9-
// Example usage: const newCounter = counter();
10-
// newCounter(); // 1
11-
// newCounter(); // 2
14+
15+
const newCounter = counter();
16+
17+
newCounter(); // 1
18+
newCounter(); // 2
19+
newCounter();
20+
newCounter();
1221

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

‎assignments/stretch-function-conversion.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let subtract = (param1, param2) => {
3030
return param1 - param2;
3131
};
3232

33-
// exampleArray = [1,2,3,4];
33+
exampleArray = [1,2,3,4];
3434
// const triple = exampleArray.map(function (num) {
3535
// return num * 3;
3636
// });

0 commit comments

Comments
 (0)