Skip to content

Commit 98a5d7e

Browse files
committed
Complete array methods challenge
1 parent 4f6ff22 commit 98a5d7e

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

assignments/array-methods.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,62 @@ 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+
runners.forEach(x => fullName.push(`${x.first_name} ${x.last_name}`))
5960
console.log(fullName);
6061

6162
// ==== Challenge 2: Use .map() ====
6263
// 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 = [];
64+
let allCaps = runners.map(x => x.first_name.toUpperCase());
6465
console.log(allCaps);
6566

6667
// ==== Challenge 3: Use .filter() ====
6768
// 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 = [];
69+
let largeShirts = runners.filter(x => x.shirt_size === "L");
6970
console.log(largeShirts);
7071

7172
// ==== Challenge 4: Use .reduce() ====
7273
// 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+
let mappedDonations = runners.map(x => x.donation)
75+
let ticketPriceTotal = mappedDonations.reduce((acc, cur) => acc + cur);
7476
console.log(ticketPriceTotal);
7577

7678
// ==== Challenge 5: Be Creative ====
7779
// 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.
7880

7981
// Problem 1
80-
82+
const dotComCheck = x => {
83+
let split = x.email.split('.')
84+
if (split[1] === 'com') {
85+
return `Runner ${x.id} has ${x.email} which ends with .com`
86+
} else {
87+
return `Runner ${x.id} has ${x.email} which ends with something else`
88+
}
89+
}
90+
let dotComChecked = runners.map(dotComCheck);
91+
console.log(dotComChecked);
8192
// Problem 2
8293

83-
// Problem 3
94+
const sortContributors = (x, i, thisArr) => {
95+
let sorted = [...thisArr].sort((a, b) => b.donation - a.donation);
96+
97+
return `${sorted[i].company_name} donated ${sorted[i].donation}`
98+
}
99+
let sortedContributors = runners.map(sortContributors);
100+
console.log(sortedContributors);
101+
102+
// Problem 3
103+
let unshuffledRunners = [...runners]
104+
let shuffled = []
105+
//Durstenfeld shuffle
106+
function shuffle(arr) {
107+
for (let i = arr.length -1; i > 0; i--) {
108+
let j = Math.floor(Math.random() * (i + 1));
109+
let temp = arr[i];
110+
arr[i] = arr[j];
111+
arr[j] = temp;
112+
shuffled.push(arr[i])
113+
}
114+
}
115+
shuffle(runners);
116+
const simulatedWinners = shuffled.filter((x, i) => i < 3)
117+
console.log(simulatedWinners)

0 commit comments

Comments
 (0)