| Folder Name | Branch | Coefficient |
|---|---|---|
| /js-classes | js-classes | 0.4 |
In a single file index.js, implement a base class with common methods and two child classes:
All three classes must be implemented in one index.js file using JavaScript.
Almost all methods should support chaining.
You may add your own methods and properties, but the methods described below are mandatory.
index.js file in the js-classes folder.class).get and static methods must support chaining.get() is called.// API:
new IntBuilder(int) // constructor takes starting integer, if not provided starts with 0
.plus(...n) // takes any number of integers and adds them to the value
.minus(...n) // takes any number of integers and subtracts them from the value
.multiply(n) // multiplies the value by n
.divide(n) // leaves the integer part of dividing the value by n
.mod(n) // leaves the remainder of dividing the value by n
.get() // returns the current value
IntBuilder.random(from, to) // static method, returns a random integer in the range [from, to]
Example:
IntBuilder.random(10, 100); // 42
const intBuilder = new IntBuilder(10); // 10
intBuilder
.plus(2, 3, 2) // 17
.minus(1, 2) // 14
.multiply(2) // 28
.divide(4) // 7
.mod(3) // 1
.get(); // -> 1
// API:
new StringBuilder(str) // constructor takes starting string, if not provided starts with ''
.plus(...str) // takes any number of strings and concatenates them to the value
.minus(n) // cuts off n characters from the end of the string
.multiply(int) // repeats the string int times
.divide(n) // leaves the first k characters, where k = Math.floor(str.length / n)
.remove(str) // removes all occurrences of substring str (without using replace)
.sub(from, n) // leaves a substring starting from 'from' of length n
.get() // returns the current value
Example:
var strBuilder = new StringBuilder('Hello'); // 'Hello'
strBuilder
.plus(' all', '!') // 'Hello all!'
.minus(4) // 'Hello '
.multiply(3) // 'Hello Hello Hello '
.divide(4) // 'Hell'
.remove('l') // 'He'
.sub(1,1) // 'e'
.get(); // -> 'e'
index.js file in the js-classes folder.Your Pull Request must include:
While working on this task, keep a developer’s diary. Write down the decisions you made, the approaches you considered, where you got stuck, and how you worked through it.
The diary is not graded. Its purpose is to help you understand your own work more deeply and to give your mentor a basis for a real conversation about the task.
The “Diary” folder can be placed in the root of the project.
Maximum Score: 100 points
| Criteria | Points |
|---|---|
The branch is named js-classes |
1 |
| Commit messages follow RS School Git Convention | 2 |
The js-classes folder exists |
1 |
The index.js file exists in the correct folder |
1 |
| All three classes are implemented in the file | 1 |
| Base class is implemented and contains common methods | 2 |
| Two child classes are implemented and inherit from the base | 1 |
All methods except get and static methods support chaining |
2 |
| All methods described in the task are implemented | 2 |
| Lazy evaluation is implemented | 2 |
| Code is well-structured, readable, and follows best practices | 1 |
| PR description includes: | |
| Task URL | 1 |
| Description of classes and methods | 1 |
| Submission and deadline dates | 1 |
| Your self-check using checkboxes | 1 |
| Penalty: | |
| Fewer than 3 commits in the PR | -5 |
| Commit after the deadline and before mentor review | -5 |
| The solution contains comments | -10 |
| The solution contains console.log | -2 |
The PR includes more than one required index.js file |
-10 |
After submitting the task, your mentor will ask 2–3 questions from the areas below. Answers account for ~80 points of the total score, so make sure you can explain the concepts in your own words — not just recite a definition.
class keyword (ES6)?class declarations just syntactic sugar? What does JavaScript actually create under the hood?Constructor.prototype in ES5 instead of defining them inside the constructor function?Object.create and why is it preferred over new Parent() when setting up the prototype chain for inheritance?Child.prototype.constructor = Child do, and what breaks if you omit it?extends and super() compare to the manual ES5 inheritance setup?super() inside an ES6 subclass constructor?__proto__ and prototype?instanceof work, and what does it actually check?this and returning a new instance? When would you choose one over the other?this? Why or why not?