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
15 changes: 15 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript classes and inheritance</title>
<script src="script.js"></script>
<script src="tests.js"></script>
</head>
<body>
<h1>Task: JavaScript classes and inheritance</h1>
<p>See the results of the tests in the console😉</p>
</body>
</html>
107 changes: 107 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -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 = '';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Обрати внимание на метод repeat у стрингов. Думаю, он бы тут хорошо подошел

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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Думаю метод includes поможет в этом месте

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;
};
28 changes: 28 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
@@ -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);