Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Task4/question1.js
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions Task4/question2.js
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions Task4/question3.js
Original file line number Diff line number Diff line change
@@ -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];
}
}
}
8 changes: 8 additions & 0 deletions Task4/question4.js
Original file line number Diff line number Diff line change
@@ -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))
}
}
10 changes: 10 additions & 0 deletions Task4/question5.js
Original file line number Diff line number Diff line change
@@ -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;
}
}