Skip to content

Commit a17fcb3

Browse files
authored
Merge pull request #1 from Sherexmykes/sheila-r-moore
JavaScript 2
2 parents 770541a + 7ce159f commit a17fcb3

File tree

3 files changed

+68
-14
lines changed

3 files changed

+68
-14
lines changed

assignments/array-methods.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,60 @@ 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(function (value){
60+
fullName.push(value.first_name + ` ` + value.last_name);
61+
})
62+
5963
console.log(fullName);
6064

6165
// ==== Challenge 2: Use .map() ====
6266
// 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
6367
let allCaps = [];
68+
69+
runners.map(function(value){
70+
console.log(value.first_name.toUpperCase());
71+
})
6472
console.log(allCaps);
6573

6674
// ==== Challenge 3: Use .filter() ====
6775
// 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
6876
let largeShirts = [];
77+
78+
largeShirts.push(runners.filter(runner => runner.shirt_size == "L" ));
79+
6980
console.log(largeShirts);
7081

7182
// ==== Challenge 4: Use .reduce() ====
7283
// 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
7384
let ticketPriceTotal = [];
85+
runners.reduce(function (accumulator, value) {
86+
console.log(`the accumulator ${accumulator}`);
87+
console.log(` the value ${value.donation}`);
88+
return accumulator + value.donation;
89+
}, 0);
90+
7491
console.log(ticketPriceTotal);
7592

7693
// ==== Challenge 5: Be Creative ====
7794
// 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.
7895

7996
// Problem 1
97+
// Find all the small shirts
98+
let smallShirts = [];
8099

81-
// Problem 2
100+
smallShirts.push(runners.filter(runner => runner.shirt_size == "S" ));
82101

83-
// Problem 3
102+
console.log(smallShirts);
103+
104+
// Problem 2
105+
//Find all the emails
106+
let emailList =[];
107+
emailList.push(runners.map(runner => runner.email));
108+
console.log(emailList);
109+
// Problem 3
110+
//Name and Donation
111+
let nameDonation =[];
112+
runners.forEach(runner => {
113+
nameDonation.push(`${runner.first_name} ${runner.donation}`);
114+
});
115+
console.log(nameDonation)

assignments/callbacks.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
1313
// Potential Solution:
1414
1515
// Higher order function using "cb" as the call back
16-
function firstItem(arr, cb) {
17-
return cb(arr[0]);
18-
}
16+
17+
1918
2019
// Function invocation
2120
firstItem(items, function(first) {
@@ -26,26 +25,44 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2625

2726

2827
function getLength(arr, cb) {
29-
// getLength passes the length of the array into the callback.
28+
const arrLength =arr.length;
29+
return cb(arrLength);
3030
}
31+
getLength(items, function(length){
32+
console.log(length);
33+
})
3134

32-
function last(arr, cb) {
35+
// getLength passes the length of the array into the callback.
36+
37+
38+
function last(arr, cb) {return cb(arr[3]);
3339
// last passes the last item of the array into the callback.
3440
}
35-
36-
function sumNums(x, y, cb) {
41+
last(items, function(last) {
42+
console.log(last)
43+
});
44+
function sumNums(x, y, cb) { return cb(x+y);
3745
// sumNums adds two numbers (x, y) and passes the result to the callback.
3846
}
47+
sumNums(1, 2, function(add) {
48+
console.log(add);
49+
})
3950

40-
function multiplyNums(x, y, cb) {
51+
function multiplyNums(x, y, cb) {return cb(x*y);
4152
// multiplyNums multiplies two numbers and passes the result to the callback.
4253
}
43-
44-
function contains(item, list, cb) {
54+
multiplyNums(2,3, function(multipy) {
55+
console.log(multipy);
56+
})
57+
function contains(item, list, cb) {if(list.includes(item)) {
58+
return cb(true)
59+
} else return cb(false);
4560
// contains checks if an item is present inside of the given array/list.
4661
// Pass true to the callback if it is, otherwise pass false.
4762
}
48-
63+
contains('Gum', items, function(lastword) {
64+
console.log(lastword);
65+
})
4966
/* STRETCH PROBLEM */
5067

5168
function removeDuplicates(array, cb) {

assignments/closure.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3-
3+
function sayHello(name) {
4+
let text = 'Hello ' + name;
5+
let say = function() { console.log(text); }
6+
say();
7+
}
8+
sayHello('Sheila');
49

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

0 commit comments

Comments
 (0)