Skip to content

Commit 4d86b4e

Browse files
author
Sascha
committed
added answers to array-methods.js
1 parent 770541a commit 4d86b4e

2 files changed

Lines changed: 122 additions & 7 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +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+
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) => {
69+
fullName.push(`${person.first_name} ${person.last_name}`);
70+
});
71+
5972
console.log(fullName);
6073

6174
// ==== Challenge 2: Use .map() ====
6275
// 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 = [];
76+
77+
const allCaps = runners.map((person) => {
78+
return `${person.first_name.toUpperCase()}`;
79+
});
80+
6481
console.log(allCaps);
6582

6683
// ==== Challenge 3: Use .filter() ====
6784
// 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 = [];
85+
86+
const largeShirts = runners.filter((person) => {
87+
return person.shirt_size === 'L';
88+
});
89+
6990
console.log(largeShirts);
7091

7192
// ==== Challenge 4: Use .reduce() ====
7293
// 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 = [];
94+
const ticketPriceTotal = runners.reduce((acc, currentValue) => {
95+
return acc += currentValue.donation;
96+
}, 0);
97+
7498
console.log(ticketPriceTotal);
7599

76100
// ==== Challenge 5: Be Creative ====

‎assignments/callbacks.js‎

Lines changed: 94 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,32 +20,123 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2020
// Function invocation
2121
firstItem(items, function(first) {
2222
console.log(first)
23-
});
23+
}); */
24+
2425

25-
*/
2626

2727

2828
function getLength(arr, cb) {
2929
// getLength passes the length of the array into the callback.
30+
return cb(arr);
3031
}
3132

33+
/* version one */
34+
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+
48+
49+
50+
51+
52+
53+
/* ---------------------------------------------------------------- */
54+
3255
function last(arr, cb) {
3356
// last passes the last item of the array into the callback.
57+
return cb(arr);
3458
}
3559

60+
/* version one */
61+
62+
/* function lastItem(arr) {
63+
console.log(arr[arr.length -1]);
64+
}
65+
66+
last(items, lastItem); */
67+
68+
/* version two */
69+
70+
last(items, function lastItem(arr) {
71+
console.log(arr[arr.length -1]);
72+
});
73+
74+
75+
/* ---------------------------------------------------------------- */
76+
3677
function sumNums(x, y, cb) {
3778
// sumNums adds two numbers (x, y) and passes the result to the callback.
79+
return cb(x, y);
80+
}
81+
82+
/* There are some problems with this version!
83+
function sum(x,y){
84+
return cb(x + y);
3885
}
3986
87+
sumNums(2, 3, sum);
88+
*/
89+
90+
/* version two */
91+
92+
sumNums(2, 3, function(x, y) {
93+
console.log(x + y)
94+
});
95+
96+
/* ---------------------------------------------------------------- */
97+
4098
function multiplyNums(x, y, cb) {
4199
// multiplyNums multiplies two numbers and passes the result to the callback.
100+
return cb(x, y);
42101
}
43102

103+
/* There are some problems with this version!
104+
function multiply(x, y){
105+
console.log(x * y);
106+
}
107+
108+
multiplyNums(2, 3, multiply(x, y));
109+
*/
110+
111+
/* version two */
112+
113+
multiplyNums(2, 3, function(x, y) {
114+
console.log(x * y)
115+
});
116+
117+
/* ---------------------------------------------------------------- */
118+
44119
function contains(item, list, cb) {
45120
// contains checks if an item is present inside of the given array/list.
46121
// Pass true to the callback if it is, otherwise pass false.
122+
return cb(item, list)
123+
}
124+
125+
/* version one */
126+
127+
/* function containCheck(item, list) {
128+
console.log(list.includes(item));
47129
}
48130
131+
contains("Pencil", items, containCheck); */
132+
133+
/* version two */
134+
135+
contains("Pencil", items, function(item, list) {
136+
console.log(list.includes(item));
137+
});
138+
139+
49140
/* STRETCH PROBLEM */
50141

51142
function removeDuplicates(array, cb) {

0 commit comments

Comments
 (0)