diff --git a/Task5/question1.js b/Task5/question1.js new file mode 100644 index 0000000..3a0575c --- /dev/null +++ b/Task5/question1.js @@ -0,0 +1,60 @@ +// http://www.codewars.com/kata/array-helpers + +Array.prototype.square = function() { + let arr = this; + let tempArr = []; + for (i = 0; i < arr.length; i++) { + tempArr.push(arr[i] * arr[i]); + } + return tempArr; +} + +Array.prototype.cube = function() { + let arr = this; + let tempArr = []; + for (i = 0; i < arr.length; i++) { + tempArr.push(arr[i] * arr[i] * arr[i]); + } + return tempArr; +} + +Array.prototype.sum = function() { + let arr = this; + let total = 0; + for (i = 0; i < arr.length; i++) { + total += arr[i]; + } + return total; +} + +Array.prototype.average = function() { + let arr = this; + let total = 0; + console.log(this); + for (i = 0; i < arr.length; i++) { + total += arr[i]; + } + return arr.length > 0 ? total / arr.length : NaN; +} + +Array.prototype.even = function() { + let arr = this; + let tempArr = []; + for (i = 0; i < arr.length; i++) { + if (arr[i] % 2 == 0) { + tempArr.push(arr[i]); + } + } + return tempArr; +} + +Array.prototype.odd = function() { + let arr = this; + let tempArr = []; + for (i = 0; i < arr.length; i++) { + if (arr[i] % 2 != 0) { + tempArr.push(arr[i]); + } + } + return tempArr; +} diff --git a/Task5/question2.js b/Task5/question2.js new file mode 100644 index 0000000..a7e42a0 --- /dev/null +++ b/Task5/question2.js @@ -0,0 +1,13 @@ +// http: //www.codewars.com/kata/extract-nested-object-reference + +Object.prototype.hash = function(string) { + let strArr = string.split("."); + let tempObj = this; + for (let i = 0; i < strArr.length; i++) {\ + tempObj = tempObj[strArr[i]]; + if (tempObj == undefined) { + return undefined; + } + } + return tempObj; +} diff --git a/Task5/question3.js b/Task5/question3.js new file mode 100644 index 0000000..a4c5b64 --- /dev/null +++ b/Task5/question3.js @@ -0,0 +1,5 @@ +// http://www.codewars.com/kata/new-with-apply + +function construct(Class, ...args) { + return new Class(...args); +} diff --git a/Task5/question4.js b/Task5/question4.js new file mode 100644 index 0000000..2f36174 --- /dev/null +++ b/Task5/question4.js @@ -0,0 +1,7 @@ +// http://www.codewars.com/kata/santaclausable-interface + +function isSantaClausable(obj) { + if (typeof(obj.sayHoHoHo) == "function" && typeof(obj.distributeGifts) == "function" && typeof(obj.goDownTheChimney) == "function") + return true; + return false; +}