Skip to content

Commit 0f42c25

Browse files
committed
Task 2: Higher Order Functions and Callbacks
1 parent ae0bbcd commit 0f42c25

3 files changed

Lines changed: 54 additions & 19 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ With some basic JavaScript principles in hand, we can now expand our skills out
2424

2525
This task focuses on getting practice with higher order functions and callback functions by giving you an array of values and instructions on what to do with that array.
2626

27-
* [ ] Review the contents of the [callbacks.js](assignments/callbacks.js) file. Notice you are given an array at the top of the page. Use that array to aid you with your functions.
27+
* [x] Review the contents of the [callbacks.js](assignments/callbacks.js) file. Notice you are given an array at the top of the page. Use that array to aid you with your functions.
2828

29-
* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.
29+
* [x] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.
3030

3131
## Task 3: Array Methods
3232

‎assignments/callbacks.js‎

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,83 @@
22

33
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
44

5-
/*
65

7-
//Given this problem:
8-
9-
function firstItem(arr, cb) {
10-
// firstItem passes the first item of the given array to the callback function.
11-
}
126

13-
// Potential Solution:
7+
//Given this problem:
148

15-
// Higher order function using "cb" as the call back
16-
function firstItem(arr, cb) {
17-
return cb(arr[0]);
18-
}
9+
function firstItem(arr, cb) {
10+
// firstItem passes the first item of the given array to the callback function.
11+
}
12+
13+
// Potential Solution:
14+
15+
// Higher order function using "cb" as the call back
16+
function firstItem(arr, cb) {
17+
return cb(arr[0]);
18+
}
19+
20+
// Function invocation
21+
firstItem(items, function (first) {
22+
console.log(first)
23+
});
1924

20-
// Function invocation
21-
firstItem(items, function(first) {
22-
console.log(first)
23-
});
2425

25-
*/
2626

2727

2828
function getLength(arr, cb) {
2929
// getLength passes the length of the array into the callback.
30+
return cb(arr.length);
3031
}
32+
getLength(items, function (arrLength) {
33+
console.log(`Length of array: ${arrLength}`);
34+
});
3135

3236
function last(arr, cb) {
3337
// last passes the last item of the array into the callback.
38+
return cb(arr[arr.length - 1]);
3439
}
40+
last(items, function (arrLast) {
41+
console.log(`Last item in array is: ${arrLast}`);
42+
});
43+
44+
45+
let x = 1;
46+
let y = 1;
3547

3648
function sumNums(x, y, cb) {
3749
// sumNums adds two numbers (x, y) and passes the result to the callback.
50+
let math = x + y;
51+
return cb(math);
3852
}
53+
sumNums(x, y, function (addNums) {
54+
console.log(`${x} pluse ${y} is: ${addNums}`);
55+
});
3956

4057
function multiplyNums(x, y, cb) {
4158
// multiplyNums multiplies two numbers and passes the result to the callback.
59+
let multiply = x * y;
60+
return cb(multiply);
4261
}
62+
multiplyNums(x, y, function (multiply) {
63+
console.log(`${x} times ${y} is: ${multiply}`);
64+
});
65+
66+
const list = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
4367

4468
function contains(item, list, cb) {
4569
// contains checks if an item is present inside of the given array/list.
4670
// Pass true to the callback if it is, otherwise pass false.
71+
for (let i = 0; i < list.length; i++) {
72+
let listItem = list[i];
73+
if (item === listItem) {
74+
return cb(true);
75+
} else {
76+
return cb(false);
77+
}
78+
}
4779
}
4880

81+
4982
/* STRETCH PROBLEM */
5083

5184
function removeDuplicates(array, cb) {

‎assignments/index.html‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!doctype html>
22

33
<html lang="en">
4+
45
<head>
56
<meta charset="utf-8">
67
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -14,6 +15,7 @@
1415
</head>
1516

1617
<body>
17-
<h1>JS II - Check your work in the console!</h1>
18+
<h1>JS II - Check your work in the console!</h1>
1819
</body>
20+
1921
</html>

0 commit comments

Comments
 (0)