diff --git a/Task4/question1.js b/Task4/question1.js new file mode 100644 index 0000000..93646ee --- /dev/null +++ b/Task4/question1.js @@ -0,0 +1,27 @@ +// http://www.codewars.com/kata/using-closures-to-share-class-state + +var totalWeight = 0, + count = 0; +var Cat = function(name, weight) { + + if (!name && !weight) + throw Error; + + this.name = name; + totalWeight += weight; + count++; + + Object.defineProperty(this, 'weight', { + get: function() { return weight; }, + set: function(value) { + totalWeight -= weight; + totalWeight += value; + weight = value; + } + }) + +}; + +Cat.averageWeight = function() { + return totalWeight / count; +} diff --git a/Task4/question2.js b/Task4/question2.js new file mode 100644 index 0000000..064769b --- /dev/null +++ b/Task4/question2.js @@ -0,0 +1,11 @@ +// http://www.codewars.com/kata/a-chain-adding-function + +function add(n) { + let tempfunc = function(number) { + return add(number + n); + } + tempfunc.valueOf = function() { + return n; + } + return tempfunc; +} diff --git a/Task4/question3.js b/Task4/question3.js new file mode 100644 index 0000000..8fcd035 --- /dev/null +++ b/Task4/question3.js @@ -0,0 +1,21 @@ +// http://www.codewars.com/kata/function-cache + +function cache(func) { + + var obj = {} + var arr = new Array(); + return function(arg1, arg2) { + let arr1 = new Array(); + arr1.push(arg1); + arr1.push(arg2); + + let strArr1 = JSON.stringify(arr1); + if (!(arr.includes(strArr1))) { + arr.push(strArr1); + obj[strArr1] = func(arg1, arg2); + return obj[strArr1]; + } else { + return obj[strArr1]; + } + } +} diff --git a/Task4/question4.js b/Task4/question4.js new file mode 100644 index 0000000..6e02733 --- /dev/null +++ b/Task4/question4.js @@ -0,0 +1,8 @@ +// http://www.codewars.com/kata/function-composition + +function compose(f, g) { + // Compose the two functions here! + return function(...args) { + return f(g(...args)) + } +} diff --git a/Task4/question5.js b/Task4/question5.js new file mode 100644 index 0000000..85ce27a --- /dev/null +++ b/Task4/question5.js @@ -0,0 +1,10 @@ +// http://www.codewars.com/kata/function-composition-1 + +function compose(...args) { + return function(num) { + for (let i = args.length - 1; i >= 0; i--) { + num = args[i](num); + } + return num; + } +}