-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
practice.js
169 lines (121 loc) · 4.18 KB
/
practice.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Once you complete a problem, refresh ./SpecRunner.html in your browser and check to see if the problem's test(s) are passing.
Passed tests will be indicated by a green circle.
Failed tests will be indicated by a red X.
You can refresh the page at any time to re-run all the tests.
In this repo your job is to write functions to make each function call work properly.
Here's an example of code that will be given to you:
sayHi('Hi Katie', function(thingToSay){
alert(thingToSay);
});
It would be your job to create the sayHi function:
var sayHi = function(str, cb){
cb(str);
}
*/
////////// PROBLEM 1 //////////
/*
Write a function called first that takes in two parameters, an array and a callback function.
Then invoke the callback function, passing in the first element in the array as it's argument.
*/
// Code Here
// Do not edit the code below.
var names = ['Tyler', 'Cahlan', 'Ryan', 'Colt', 'Tyler', 'Blaine', 'Cahlan'];
first(names, function(firstName){
console.log('The first name in names is ' + firstName);
return firstName;
});
// Do not edit the code above.
////////// PROBLEM 2 //////////
/*
Write a function called last that takes in an array and a callback function.
Then invoke the callback, passing in the last element in the array as the argument.
*/
//Code Here
// Do not edit the code below.
last(names, function(lastName){
console.log('The last name in names is ' + lastName);
return lastName;
});
// Do not edit the code above.
////////// PROBLEM 3 //////////
/*
Write a function called multiply that takes in three parameters: two numbers and a callback function.
Invoke the callback, passing in the product of the two numbers multiplied as the argument.
*/
//Code Here
// Do not edit the code below.
multiply(4, 3, function(answer){
console.log('The answer is ' + answer); //should console.log 12
});
// Do not edit the code above.
////////// PROBLEM 4 //////////
/*
Write a function called contains that takes in three parameters: an array, a name and a callback.
Check if the name exists in the array.
If it does, invoke the callback with true as the argument.
If the name does not exist, invoke the callback with false as the argument.
*/
//Code Here
// Do not edit the code below.
contains(names, 'Colt', function(result){
if(result === true){
console.log('Colt is in the array');
} else {
console.log('Colt is not in the array');
}
});
// Do not edit the code above.
////////// PROBLEM 5 //////////
/*
Write a function called uniq that takes in an array and a callback function.
Remove any duplicate values from the array, and invoke the callback with the modified array as an argument.
*/
//Code Here
// Do not edit the code below.
uniq(names, function(uniqArr){
console.log('The new names array with all the duplicate items removed is ', uniqArr);
});
// Do not edit the code above.
////////// PROBLEM 6 //////////
/*
Write a function called each that takes in an array of names and a callback function.
For each name in the array, invoke the callback and pass in the name and the name's index as arguments.
*/
//Code Here
// Do not edit the code below.
each(names, function(item, indice){
console.log('The item in the ' + indice + ' position is ' + item)
});
// Do not edit the code above.
////////// PROBLEM 7 //////////
/*
Write a function called getUserById that takes in three parameters: an array of objects (users), an id and a callback, and searches for the user with a matching id.
When the correct user object is found, invoke the callback with the user object as an argument.
*/
// Code here
// Do not edit the code below.
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);
});
// Do not edit the code above.