Skip to content

Commit 40bbba6

Browse files
committed
finished javascript II and stretch goals
1 parent e543e5d commit 40bbba6

4 files changed

Lines changed: 102 additions & 43 deletions

File tree

assignments/array-methods.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,56 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
5454
{"id":50,"first_name":"Shell","last_name":"Baine","email":"[email protected]","shirt_size":"M","company_name":"Gabtype","donation":171}];
5555

5656
// ==== 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.
58-
let fullName = [];
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.
58+
let fullName = []
59+
runners.forEach((name) => {
60+
fullName.push(name.first_name + " " + name.last_name)
61+
})
5962
console.log(fullName);
6063

6164
// ==== Challenge 2: Use .map() ====
6265
// 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-
console.log(allCaps);
66+
let allCaps = runners.map((name) => {
67+
return name.first_name.toUpperCase()
68+
});
69+
70+
console.log(allCaps);
6571

6672
// ==== Challenge 3: Use .filter() ====
6773
// 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 = [];
74+
let largeShirts = runners.filter((shirt) => {
75+
return shirt.shirt_size === "L"
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((total, current) => {
82+
return total + current.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
80-
90+
// list everyone's name, email, etc.
91+
runners.forEach((element) => {
92+
console.log(element);
93+
})
8194
// Problem 2
95+
// print out last name all lower case
96+
let allLowerCaps = runners.map((name) => {
97+
return name.last_name.toLowerCase()
98+
})
99+
console.log(allLowerCaps)
100+
// Problem 3
101+
// return list of people who works for the same company
102+
const sameCompany = (company) => {
103+
let arr = runners.filter((worker) => {
104+
return worker.company_name === company
105+
})
106+
return arr
107+
}
82108

83-
// Problem 3
109+
console.log(sameCompany("Skinix"));

assignments/callbacks.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// 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.
22

33
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
4-
function callback(value) {
5-
console.log(value)
6-
}
4+
75
/*
86
97
//Given this problem:
@@ -28,39 +26,55 @@ function getLength(arr, cb) {
2826
// getLength passes the length of the array into the callback.
2927
return cb(arr.length)
3028
}
31-
getLength(items,callback)
29+
getLength(items,(arr1) => {
30+
console.log(arr1)
31+
})
3232

3333
function last(arr, cb) {
3434
// last passes the last item of the array into the callback.
3535
return cb(arr[3])
3636
}
37-
last(items, callback)
37+
last(items, (lastItem) => {
38+
console.log(lastItem);
39+
})
3840

3941
function sumNums(x, y, cb) {
4042
// sumNums adds two numbers (x, y) and passes the result to the callback.
41-
return cb(x + y)
43+
return cb(x, y)
4244
}
43-
sumNums(2, 5, callback)
45+
console.log(sumNums(10, 10, (x, y) => {
46+
return x + y
47+
}));
4448

4549

4650
function multiplyNums(x, y, cb) {
4751
// multiplyNums multiplies two numbers and passes the result to the callback.
48-
return cb(x * y)
52+
return cb(x, y)
4953
}
50-
multiplyNums(5, 5, callback)
54+
console.log(multiplyNums(5, 5, (x, y) => {
55+
return x * y
56+
}));
5157

5258
function contains(item, list, cb) {
5359
// contains checks if an item is present inside of the given array/list.
5460
// Pass true to the callback if it is, otherwise pass false.
55-
list.find((element) => {
56-
if(element === item) {
57-
return cb(true)
58-
} else {
59-
return cb(false)
60-
}
61-
})
61+
// list.find((element) => {
62+
// if(element === item) {
63+
// return cb(true)
64+
// } else {
65+
// return cb(false)
66+
// }
67+
// })
68+
return cb(list, item)
69+
}
70+
const listArr = ( list, item ) => {
71+
if(list.indexOf(item) > -1) {
72+
return true
73+
} else {
74+
return false
75+
}
6276
}
63-
contains("Davina", items, callback)
77+
console.log(contains("Pencil", items, listArr))
6478

6579
/* STRETCH PROBLEM */
6680

assignments/closure.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3-
3+
function closure() {
4+
let scopeInner = 0
5+
function inner() {
6+
return scopeInner
7+
}
8+
inner()
9+
}
10+
closure()
411

512
// ==== Challenge 2: Create a counter function ====
613
const counter = () => {
714
// Return a function that when invoked increments and returns a counter variable.
15+
let myCounter = 0
16+
17+
function newClosure() {
18+
myCounter++
19+
}
20+
newClosure()
21+
newClosure()
22+
newClosure()
23+
newClosure()
24+
console.log(myCounter);
25+
826
};
27+
counter()
928
// Example usage: const newCounter = counter();
1029
// newCounter(); // 1
1130
// newCounter(); // 2
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax
22

3-
// let myFunction = function () {};
3+
let myFunction = () => {};
44

5-
// let anotherFunction = function (param) {
6-
// return param;
7-
// };
5+
let anotherFunction = (param) => {
6+
return param;
7+
};
88

9-
// let add = function (param1, param2) {
10-
// return param1 + param2;
11-
// };
12-
// add(1,2);
9+
let add = (param1, param2) => {
10+
return param1 + param2;
11+
};
12+
add(1,2);
1313

14-
// let subtract = function (param1, param2) {
15-
// return param1 - param2;
16-
// };
17-
// subtract(1,2);
14+
let subtract = (param1, param2) => {
15+
return param1 - param2;
16+
};
17+
subtract(1,2);
1818

19-
// exampleArray = [1,2,3,4];
20-
// const triple = exampleArray.map(function (num) {
21-
// return num * 3;
22-
// });
23-
// console.log(triple);
19+
exampleArray = [1,2,3,4];
20+
const triple = exampleArray.map((num) => {
21+
return num * 3;
22+
});
23+
console.log(triple);

0 commit comments

Comments
 (0)