From 4399d173ee45b245f2547cf0f77ff8622fcc09ad Mon Sep 17 00:00:00 2001 From: mandeep duggal Date: Thu, 16 Apr 2020 12:40:11 +0530 Subject: [PATCH 1/2] Task3 --- Task3/question1.js | 11 +++++++++++ Task3/question2.js | 7 +++++++ Task3/question3.js | 13 +++++++++++++ Task3/question4.js | 13 +++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 Task3/question1.js create mode 100644 Task3/question2.js create mode 100644 Task3/question3.js create mode 100644 Task3/question4.js diff --git a/Task3/question1.js b/Task3/question1.js new file mode 100644 index 0000000..d5204e7 --- /dev/null +++ b/Task3/question1.js @@ -0,0 +1,11 @@ +// http://www.codewars.com/kata/prefill-an-array + +function prefill(n, v) { + + if (isNaN(n) || Number(n) == NaN || n === Infinity || n === -Infinity || n < 0 || typeof(n) == 'boolean' || n % 1 != 0) + throw new TypeError(n + " is invalid"); + else { + arr = (new Array(Number(n))).fill(v); + } + return arr; +} diff --git a/Task3/question2.js b/Task3/question2.js new file mode 100644 index 0000000..35bd745 --- /dev/null +++ b/Task3/question2.js @@ -0,0 +1,7 @@ +// http://www.codewars.com/kata/a-function-within-a-function + +function always(n) { + return function() { + return n + } +} diff --git a/Task3/question3.js b/Task3/question3.js new file mode 100644 index 0000000..963b93b --- /dev/null +++ b/Task3/question3.js @@ -0,0 +1,13 @@ +// http://www.codewars.com/kata/closures-and-scopes/train/javascript + +function createFunctions(n) { + var callbacks = []; + + for (let i = 0; i < n; i++) { + callbacks.push(function() { + return i; + }); + } + + return callbacks; +} diff --git a/Task3/question4.js b/Task3/question4.js new file mode 100644 index 0000000..05baea3 --- /dev/null +++ b/Task3/question4.js @@ -0,0 +1,13 @@ +// http://www.codewars.com/kata/can-you-keep-a-secret + +function createSecretHolder(secret) { + return { + sec: secret, + getSecret: function() { + return this.sec; + }, + setSecret: function(b) { + this.sec = b; + } + } +} From 1cca2d6975847b9258637c310396e7d6b3f467e5 Mon Sep 17 00:00:00 2001 From: mandeep duggal Date: Thu, 16 Apr 2020 12:45:10 +0530 Subject: [PATCH 2/2] Task4 --- Task3/question1.js | 11 ----------- Task3/question2.js | 7 ------- Task3/question3.js | 13 ------------- Task3/question4.js | 13 ------------- Task4/question1.js | 27 +++++++++++++++++++++++++++ Task4/question2.js | 11 +++++++++++ Task4/question3.js | 21 +++++++++++++++++++++ Task4/question4.js | 8 ++++++++ Task4/question5.js | 10 ++++++++++ 9 files changed, 77 insertions(+), 44 deletions(-) delete mode 100644 Task3/question1.js delete mode 100644 Task3/question2.js delete mode 100644 Task3/question3.js delete mode 100644 Task3/question4.js create mode 100644 Task4/question1.js create mode 100644 Task4/question2.js create mode 100644 Task4/question3.js create mode 100644 Task4/question4.js create mode 100644 Task4/question5.js diff --git a/Task3/question1.js b/Task3/question1.js deleted file mode 100644 index d5204e7..0000000 --- a/Task3/question1.js +++ /dev/null @@ -1,11 +0,0 @@ -// http://www.codewars.com/kata/prefill-an-array - -function prefill(n, v) { - - if (isNaN(n) || Number(n) == NaN || n === Infinity || n === -Infinity || n < 0 || typeof(n) == 'boolean' || n % 1 != 0) - throw new TypeError(n + " is invalid"); - else { - arr = (new Array(Number(n))).fill(v); - } - return arr; -} diff --git a/Task3/question2.js b/Task3/question2.js deleted file mode 100644 index 35bd745..0000000 --- a/Task3/question2.js +++ /dev/null @@ -1,7 +0,0 @@ -// http://www.codewars.com/kata/a-function-within-a-function - -function always(n) { - return function() { - return n - } -} diff --git a/Task3/question3.js b/Task3/question3.js deleted file mode 100644 index 963b93b..0000000 --- a/Task3/question3.js +++ /dev/null @@ -1,13 +0,0 @@ -// http://www.codewars.com/kata/closures-and-scopes/train/javascript - -function createFunctions(n) { - var callbacks = []; - - for (let i = 0; i < n; i++) { - callbacks.push(function() { - return i; - }); - } - - return callbacks; -} diff --git a/Task3/question4.js b/Task3/question4.js deleted file mode 100644 index 05baea3..0000000 --- a/Task3/question4.js +++ /dev/null @@ -1,13 +0,0 @@ -// http://www.codewars.com/kata/can-you-keep-a-secret - -function createSecretHolder(secret) { - return { - sec: secret, - getSecret: function() { - return this.sec; - }, - setSecret: function(b) { - this.sec = b; - } - } -} 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; + } +}