Skip to content

Commit f4da041

Browse files
authored
Merge pull request #1 from vebradev/justinas-vebra
JavaScript-II
2 parents 770541a + 806178e commit f4da041

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

assignments/array-methods.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,27 @@ 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(item) {
60+
let combined = item.first_name + " " + item.last_name;
61+
fullName.push(combined);
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
63-
let allCaps = [];
64-
console.log(allCaps);
67+
let allCaps = runners.map(function (runner) {
68+
return runner.first_name.toUpperCase() + " " + runner.last_name.toUpperCase();
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(runner => runner.shirt_size === "L");
6975
console.log(largeShirts);
7076

7177
// ==== Challenge 4: Use .reduce() ====
7278
// 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 = [];
79+
let ticketPriceTotal = runners.reduce((donation, runner) => donation + runner.donation, 0);
7480
console.log(ticketPriceTotal);
7581

7682
// ==== Challenge 5: Be Creative ====

assignments/callbacks.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,50 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2424
2525
*/
2626

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

3235
function last(arr, cb) {
3336
// last passes the last item of the array into the callback.
37+
return cb(arr.pop());
3438
}
39+
last(items, function(cb) {
40+
console.log(cb);
41+
});
3542

3643
function sumNums(x, y, cb) {
3744
// sumNums adds two numbers (x, y) and passes the result to the callback.
45+
return cb(x + y);
3846
}
47+
sumNums(5, 10, function(cb) {
48+
console.log(cb);
49+
});
3950

4051
function multiplyNums(x, y, cb) {
4152
// multiplyNums multiplies two numbers and passes the result to the callback.
53+
return cb(x * y);
4254
}
55+
multiplyNums(5, 10, function(cb) {
56+
console.log(cb);
57+
});
4358

4459
function contains(item, list, cb) {
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.
62+
if (list.includes(item)) {
63+
return cb(true);
64+
} else {
65+
return cb(false);
66+
}
4767
}
68+
contains('Notebook', items, function(cb) {
69+
console.log(cb);
70+
});
4871

4972
/* STRETCH PROBLEM */
5073

assignments/closure.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
33

4+
function outer() {
5+
let a = 5;
6+
function inner() {
7+
b = 10;
8+
console.log(a + b);
9+
}
10+
return inner;
11+
}
12+
const inner = outer();
413

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

7-
816
// ==== Challenge 2: Create a counter function ====
917
const counter = () => {
1018
// Return a function that when invoked increments and returns a counter variable.

assignments/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<script src="array-methods.js"></script>
1111
<script src="callbacks.js"></script>
1212
<script src="closure.js"></script>
13-
<script src="stretch-function-conversion.js"></script>
13+
<!-- <script src="stretch-function-conversion.js"></script> -->
1414
</head>
1515

1616
<body>

0 commit comments

Comments
 (0)