-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercises.js
61 lines (54 loc) · 1.29 KB
/
exercises.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
function sum() {
var argArray = [].slice.call(arguments);
var total = 0;
for (i = 0; i < argArray.length; i++) {
total += argArray[i];
}
return total;
}
// console.log(sum(1,2,3,4) == 10);
// console.log(sum(1,2,3,4,5) == 15);
Function.prototype.myBind = function() {
var func = this;
var funcArgs = [].slice.call(arguments);
var obj = funcArgs.shift();
return function () {
var invokedArgs = [].slice.call(arguments);
return func.apply(obj, funcArgs.concat(invokedArgs));
};
};
function curriedSum(numArgs) {
var numbers = [];
function _curriedSum(num) {
numbers.push(num);
if (numbers.length === numArgs) {
var total = 0;
for (i = 0; i < numbers.length; i++) {
total += numbers[i];
}
return total;
} else {
return _curriedSum;
}
}
return _curriedSum;
}
Function.prototype.curry = function (numArgs) {
var args = [];
var func = this;
function _curryFUNK(arg) {
args.push(arg);
if (args.length === numArgs) {
return func.apply(null, args);
} else {
return _curryFUNK;
}
}
return _curryFUNK;
};
Function.prototype.inherits = function(parent) {
function Surrogate() {}
Surrogate.prototype = parent.prototype;
this.prototype = new Surrogate();
this.prototype.constructor = this;
};