-
Notifications
You must be signed in to change notification settings - Fork 0
/
callapplybind.js
355 lines (281 loc) · 9.69 KB
/
callapplybind.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
Write a function called arrayFrom which converts an array-like-object into an array.
Examples:
var divs = document.getElementsByTagName('divs');
divs.reduce // undefined
var converted = arrayFrom(divs);
converted.reduce // function(){}....
*/
function arrayFrom(arrayLikeObject){
return Array.prototype.slice.call(arrayLikeObject);
}
console.log("array from array-like object");
var spans = document.getElementsByTagName('span');
console.log(spans.reduce);
var arr = arrayFrom(spans);
console.log(arr.reduce);
/*
// Write a function called sumEvenArguments which takes all of the arguments passed
to a function and returns the sum of the even ones.
Examples:
sumEvenArguments(1,2,3,4) // 6
sumEvenArguments(1,2,6) // 8
sumEvenArguments(1,2) // 2
*/
function sumEvenArguments(){
var arr = arrayFrom(arguments);
var sum = 0;
arr.forEach(element => {
if ( element % 2 === 0) {
sum += element;
}
});
return sum;
}
// OR
function sumEvenArgumentsAlternative(){
var arr = arrayFrom(arguments);
return arr.reduce(function(acc, next) {
if(next % 2 === 0) {
return acc + next;
}
return acc;
}, 0);
}
console.log("sum even arguments");
console.log(sumEvenArgumentsAlternative(1,2,3,4));
console.log(sumEvenArgumentsAlternative(1,2,6));
console.log(sumEvenArgumentsAlternative(1,2));
/*
Write a function called invokeMax which accepts a function and a maximum amount.
invokeMax should return a function that when called increments a counter.
If the counter is greater than the maximum amount, the inner function should
return "Maxed Out"
Examples:
function add(a,b){
return a+b
}
var addOnlyThreeTimes = invokeMax(add,3);
addOnlyThreeTimes(1,2) // 3
addOnlyThreeTimes(2,2) // 4
addOnlyThreeTimes(1,2) // 3
addOnlyThreeTimes(1,2) // "Maxed Out!"
*/
function add(a,b){
return a + b;
}
function invokeMax(fn, num){
var counter = 0;
return function () {
counter ++;
if ( counter > num ) {
return "Maxed Out!";
} else {
return fn.apply(this, arguments);
}
}
}
console.log("invoke max times");
var addOnlyThreeTimes = invokeMax(add,3);
console.log(addOnlyThreeTimes(1,2));
console.log(addOnlyThreeTimes(2,2));
console.log(addOnlyThreeTimes(1,2));
console.log(addOnlyThreeTimes(1,2));
/*
Write a function called once which accepts two parameters, a function and a value
for the keyword 'this'. Once should return a new function that can only be invoked once,
with the value of the keyword this in the function set to be the second parameter.
Examples:
function add(a,b){
return a+b
}
var addOnce = once(add, this);
addOnce(2,2) // 4
addOnce(2,2) // undefined
addOnce(2,2) // undefined
function doMath(a,b,c){
return this.firstName + " adds " + (a+b+c)
}
var instructor = {firstName: "Elie"}
var doMathOnce = once(doMath, instructor);
doMathOnce(1,2,3) // "Elie adds 6"
doMathOnce(1,2,3) // undefined
*/
function once(fn, thisArg) {
var alreadyInvoked = false;
return function () {
if (!alreadyInvoked) {
alreadyInvoked = true;
return fn.apply(thisArg, arguments);
}
return undefined;
}
}
console.log("invoke only once");
var addOnce = once(add, this);
console.log(addOnce(2,2));
console.log(addOnce(2,2));
console.log(addOnce(2,2));
// BONUSES!
/*
Write a function called bind which accepts a function and a value for the keyword this.
Bind should return a new function that when invoked, will invoke the function passed
to bind with the correct value of the keyword this. HINT - if you pass more than two
parameters to bind, those parameters should be included as parameters to the inner function
when it is invoked. You will have to make use of closure!
Examples:
function firstNameFavoriteColor(favoriteColor){
return this.firstName + "'s favorite color is " + favoriteColor
}
var person = {
firstName: 'Elie'
}
var bindFn = bind(firstNameFavoriteColor, person);
bindFn('green') // "Elie's favorite color is green"
var bindFn2 = bind(firstNameFavoriteColor, person, 'blue');
bindFn2('green') // "Elie's favorite color is blue"
function addFourNumbers(a,b,c,d){
return a+b+c+d;
}
bind(addFourNumbers,this,1)(2,3,4) // 10
bind(addFourNumbers,this,1,2)(3,4) // 10
bind(addFourNumbers,this,1,2,3)(4) // 10
bind(addFourNumbers,this,1,2,3,4)() // 10
bind(addFourNumbers,this)(1,2,3,4) // 10
bind(addFourNumbers,this)(1,2,3,4,5,6,7,8,9,10) // 10
*/
function addFourNumbers(a,b,c,d){
return a+b+c+d;
}
function firstNameFavoriteColor(favoriteColor){
return this.firstName + "'s favorite color is " + favoriteColor;
}
var person = {
firstName: 'Elie'
}
function bind(fn, thisArg){
var args = [];
if (arguments.length > 2) {
for (let index = 2; index < arguments.length; index++) {
//console.log(arguments[index]);
args.push(arguments[index]);
}
return function () {
var oldargs = arrayFrom(arguments);
var newargs = args.concat(oldargs);
return fn.apply(thisArg, newargs);
}
} else {
return function () {
return fn.apply(thisArg, arguments);
}
}
}
// OR
function bindAlternative (fn, thisArg) {
var outerArgs = Array.prototype.slice.call(arguments, 2);
return function() {
var innerArgs = arrayFrom(arguments);
var allArgs = outerArgs.concat(innerArgs);
return fn.apply(thisArg, allArgs);
}
}
console.log("bind");
var bindFn = bind(firstNameFavoriteColor, person);
console.log(bindFn('green'));
var bindFn2 = bind(firstNameFavoriteColor, person, 'blue');
console.log(bindFn2('green'));
console.log(bind(addFourNumbers,this,1)(2,3,4));
console.log(bind(addFourNumbers,this,1,2,3)(4));
console.log(bind(addFourNumbers,this,1,2,3,4)());
console.log(bind(addFourNumbers,this)(1,2,3,4,5,6,7,8,9,10));
/*
Write a function called flip which accepts a function and a value for the keyword this.
Flip should return a new function that when invoked, will invoke the function passed
to flip with the correct value of the keyword this and all of the arguments passed
to the function REVERSED. HINT - if you pass more than two parameters to flip, those
parameters should be included as parameters to the inner function when it is invoked.
You will have to make use of closure!
Flip should return a new function that when invoked takes the correct number of
required arguments to that function which are then reversed. HINT - you will
need to use the .length property on functions to figure out the correct
amount of arguments. For example:
Die length Eigenschaft gibt die Anzahl der von der Funktion erwarteten Parameter an.
flip(subtractFourNumbers,this,11,12,13,14,15)(1,2,3,4,5,6,7,8,9,10)
*/
function personSubtract(a,b,c){
return this.firstName + " subtracts " + (a-b-c);
}
function flip(fn, thisArg){
var args = [];
// var expectedParams = fn.length;
if (arguments.length > 2) {
for (let index = 2; index < arguments.length; index++) {
args.push(arguments[index]);
}
return function () {
var oldargs = arrayFrom(arguments);
var newargs = args.concat(oldargs);
// extract elements according to fn.length and then reverse
newargs = newargs.slice(0, fn.length).reverse();
// console.log(newargs);
return fn.apply(thisArg, newargs);
}
}
return function () {
return fn.apply(thisArg, arrayFrom(arguments).slice(0, fn.length).reverse());
}
}
// OR
function flipAlternative (fn, thisArg) {
var outerArgs = Array.prototype.slice.call(arguments, 2);
return function() {
var innerArgs = arrayFrom(arguments);
var allArgs = outerArgs.concat(innerArgs);
allArgs = allArgs.slice(0, fn.length).reverse();
return fn.apply(thisArg, allArgs);
}
}
var person = {
firstName: 'Elie'
}
console.log("flip: reverse arguments");
var flipFn = flip(personSubtract, person);
console.log(flipFn(5, 4,3,2,1));
var flipFn2 = flip(personSubtract, person, 5,6);
console.log(flipFn2(7,8));
function subtractFourNumbers(a,b,c,d){
return a-b-c-d;
}
console.log(flip(subtractFourNumbers,this,1)(2,3,4));
console.log(flip(subtractFourNumbers,this,1,2)(3,4));
console.log(flip(subtractFourNumbers,this,1,2,3)(4));
console.log(flip(subtractFourNumbers,this,1,2,3,4)());
console.log(flip(subtractFourNumbers,this)(1,2,3,4));
console.log(flip(subtractFourNumbers,this,1,2,3)(4,5,6,7));
console.log(flip(subtractFourNumbers,this)(1,2,3,4,5,6,7,8,9,10));
console.log(flip(subtractFourNumbers,this,11,12,13,14,15)(1,2,3,4,5,6,7,8,9,10));
/*
Examples:
function personSubtract(a,b,c){
return this.firstName + " subtracts " + (a-b-c);
}
var person = {
firstName: 'Elie'
}
var flipFn = flip(personSubtract, person);
flipFn(3,2,1) // "Elie subtracts -4"
var flipFn2 = flip(personSubtract, person, 5,6);
flipFn2(7,8). // "Elie subtracts -4"
function subtractFourNumbers(a,b,c,d){
return a-b-c-d;
}
flip(subtractFourNumbers,this,1)(2,3,4) // -2
flip(subtractFourNumbers,this,1,2)(3,4) // -2
flip(subtractFourNumbers,this,1,2,3)(4) // -2
flip(subtractFourNumbers,this,1,2,3,4)() // -2
flip(subtractFourNumbers,this)(1,2,3,4) // -2
flip(subtractFourNumbers,this,1,2,3)(4,5,6,7) // -2
flip(subtractFourNumbers,this)(1,2,3,4,5,6,7,8,9,10) // -2
flip(subtractFourNumbers,this,11,12,13,14,15)(1,2,3,4,5,6,7,8,9,10) // -22
*/