forked from DevMountain/javascript-2-afternoon-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbackPractice.js
153 lines (105 loc) · 3.61 KB
/
callbackPractice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* In this repo your job is to write functions to make each function call work properly.
Below is a sample problem
//code here for sayHi
sayHi('Hi Katie', function(thingToSay){
alert(thingToSay);
});
and what you should write is the sayHi function that makes the code above work,
var sayHi = function(str, cb){
cb(str);
}
sayHi('Hi Katie', function(thingToSay){
alert(thingToSay); //should alert ('Hi Katie')'
});
*/
// 1. Write a function called first that returns the first item of the array using a callback function
// Code Here
var first = (arr, callback) => callback(arr[0]);
var names = ['Tyler', 'Cahlan', 'Ryan', 'Colt', 'Tyler', 'Blaine', 'Cahlan'];
first(names, function(firstName){
console.log('The first name in names is ' + firstName);
return firstName;
});
// 2. Write a function called last which returns the last item of the array using a callback function.
//Code Here
var last = (arr, callback) => callback(arr[arr.length-1]);
last(names, function(lastName){
console.log('The last name in names is ' + lastName);
return lastName;
});
// 3. Write a function called multiply that multiplies two numbers. Invoke the callback with the result of the multiplication.
//Code Here
var multiply = (num1, num2, callback) => callback(num1*num2);
multiply(4, 3, function(answer){
console.log('The answer is ' + answer); //should console.log 12
})
// 4. Write a function called contains that checks if a name exists in an array.
// If it does, invoke the callback with true as an argument.
// If the name does not exist, invoke the callback with false as an argument.
//Code Here
var contains = (arr, name, callback) => arr.includes(name) ? callback(true) : callback(false);
contains(names, 'Colt', function(result){
if(result === true){
console.log('Colt is in the array');
} else {
console.log('Colt is not in the array');
}
});
// 5. Write a function called uniq that takes the names array and removes all duplicates.
// Invoke the callback with the modified array as an argument.
//Code Here
var uniq = (arr, cb) => {
var newArr = []
for(let i = 0; i < arr.length; i++){
if(!newArr.includes(arr[i])){
newArr.push(arr[i]);
}
}
return cb(newArr);
}
uniq(names, function(uniqArr){
console.log('The new names array with all the duplicate items removed is ', uniqArr);
});
// 6. Write a function called each that takes in an array of names. For each name in the array, invoke the callback and pass in the name and the name's index as arguments.
//Code Here
var each = (arr, cb) => {
for(let i = 0; i < arr.length; i++){
cb(arr[i], i);
}
}
each(names, function(item, indice){
console.log('The item in the ' + indice + ' position is ' + item)
});
// 7. Write a function called getUserById that looks at the array of user objects (users) and searches for a user by ID.
// When the correct user object is found, invoke the callback with the user object as an argument.
// Code here
var getUserById = (arr, id, cb) => {
for(unit in arr) {
if(arr[unit].id === id) {
return cb(arr[unit]);
}
}
}
var users = [
{
id: '12d',
email: '[email protected]',
name: 'Tyler',
address: '167 East 500 North'
},
{
id: '15a',
email: '[email protected]',
name: 'Cahlan',
address: '135 East 320 North'
},
{
id: '16t',
email: '[email protected]',
name: 'Ryan',
address: '192 East 32 North'
},
];
getUserById(users, '16t', function(user){
console.log('The user with the id 16t has the email of ' + user.email + ' the name of ' + user.name + ' and the address of ' + user.address);
});