From d5340a84a784c18a0ff3c550b0ba10916776d6f9 Mon Sep 17 00:00:00 2001 From: FoxyRaspberry Date: Sat, 6 May 2023 20:00:29 +0300 Subject: [PATCH 1/2] feat: create `BaseBuilder`, `IntBuilder`, `StringBuilder` --- script.js | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 script.js diff --git a/script.js b/script.js new file mode 100644 index 0000000..70d89ce --- /dev/null +++ b/script.js @@ -0,0 +1,107 @@ +// Создать базовый класс. +function BaseBuilder(value) { + this.value = value; +} + +BaseBuilder.prototype.get = function() { + return this.value; +} + +// Создать первый дочерний класс, который наследуется от базового (ES6). +class IntBuilder extends BaseBuilder { + constructor(value) { + super(value); + } + + static random(min, max) { + return Math.floor(min + Math.random() * (max + 1 - min)); + } + + plus(...numbers) { + this.value += numbers.reduce((sum, item) => sum + item, 0); + return this; + } + + minus(...numbers) { + this.value -= numbers.reduce((sum, item) => sum + item, 0); + return this; + } + + multiply(number) { + this.value *= number; + return this; + } + + divide(number) { + this.value /= number; + return this; + } + + mod(number) { + this.value %= number; + return this; + } +} + +// Создать второй дочерний класс, который наследуется от базового (ES5). +function StringBuilder(value) { + BaseBuilder.call(this, value); +} + +StringBuilder.prototype = Object.create(BaseBuilder.prototype); + +StringBuilder.prototype.plus = function(...strings) { + this.value += strings.join(''); + return this; +}; + +StringBuilder.prototype.minus = function(number) { + this.value = this.value.slice(0, -number); + return this; +}; + +StringBuilder.prototype.multiply = function(number) { + const originalValue = this.value; + let result = ''; + for (let counter = 0; counter < number; counter++) { + result += originalValue; + } + this.value = result; + return this; +}; + +StringBuilder.prototype.divide = function(number) { + const length = Math.floor(this.value.length / number); + this.sub(0, length); + return this; +}; + +StringBuilder.prototype.remove = function(string) { + let result = ''; + let startPosition = 0; + while (true) { + const index = this.value.indexOf(string, startPosition); + if (index === -1) { + break; + } + if (index !== 0) { + result += this.value.slice(startPosition, index); + } + startPosition = index + string.length; + } + this.value = result + this.value.slice(startPosition); + return this; +}; + +StringBuilder.prototype.removeInAnotherWay = function(string) { + this.value = this.value + .split(string) + .filter(item => item !== string) + .join(''); + return this; +}; + +StringBuilder.prototype.sub = function(start, length) { + this.value = this.value.substr(start, length); + return this; +}; From fb366a0d2f948623a929ef500fb9c3690c36efa8 Mon Sep 17 00:00:00 2001 From: FoxyRaspberry Date: Sat, 6 May 2023 20:02:02 +0300 Subject: [PATCH 2/2] feat: add the tests --- index.html | 15 +++++++++++++++ tests.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 index.html create mode 100644 tests.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..7efc421 --- /dev/null +++ b/index.html @@ -0,0 +1,15 @@ + + + + + + + JavaScript classes and inheritance + + + + +

Task: JavaScript classes and inheritance

+

See the results of the tests in the console😉

+ + \ No newline at end of file diff --git a/tests.js b/tests.js new file mode 100644 index 0000000..28ced86 --- /dev/null +++ b/tests.js @@ -0,0 +1,28 @@ +// Тестировать `IntBuilder`. +const randomNumber = IntBuilder.random(1, 100) +console.log('checkRandom', randomNumber); +console.assert(randomNumber >= 1 && randomNumber <= 100, 'Ожидаемый результат от 1 до 100, текущий результат', randomNumber); + +const intBuilder = new IntBuilder(10); +const intResult = intBuilder + .plus(6, 7, 10, 5) + .minus(1, 4, 3, 2) + .multiply(7) + .divide(4) + .mod(5) + .get(); +console.log('intResult', intResult); +console.assert(intResult === 4, 'Ожидаемый результат:', 4, 'Текущий результат:', intResult); + +// Тестировать `StringBuilder`. +const stringBuilder = new StringBuilder('марципан'); +const stringResult = stringBuilder + .plus(' невкусный', '!') + .minus(11) + .divide(4) + .multiply(2) + .remove('а') + .sub(1, 1) + .get(); +console.log('stringResult', stringResult); +console.assert(stringResult === 'м', 'Ожидаемый результат: \'м\'. Текущий результат:', stringResult);