Skip to content

Commit 20784e7

Browse files
committed
up
1 parent d7d25f4 commit 20784e7

File tree

48 files changed

+293
-388
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+293
-388
lines changed

‎1-js/03-code-quality/05-testing/3-pow-test-wrong/solution.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
Этот тест демонстрирует один из соблазнов, которые ожидают начинающего автора тестов.
1+
The test demonstrates one of temptations a developer meets when writing tests.
22

3-
Вместо того, чтобы написать три различных теста, он изложил их в виде одного потока вычислений, с несколькими `assert`.
3+
What we have here is actually 3 tests, but layed out as a single function with 3 asserts.
44

5-
Иногда так написать легче и проще, однако при ошибке в тесте гораздо менее очевидно, что же пошло не так.
5+
Sometimes it's easier to write this way, but if an error occurs, it's much less obvious what went wrong.
6+
7+
If an error happens inside a complex execution flow, then we'll have to figure out what was the data at that point.
8+
9+
TODO
610

711
Если в сложном тесте произошла ошибка где-то посередине потока вычислений, то придётся выяснять, какие конкретно были входные и выходные данные на этот момент, то есть по сути -- отлаживать код самого теста.
812

‎1-js/03-code-quality/05-testing/3-pow-test-wrong/task.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ importance: 5
22

33
---
44

5-
# Что не так в тесте?
5+
# What's wrong in the test?
66

7-
Что не так в этом тесте функции `pow`?
7+
What's wrong in the test of `pow` below?
88

99
```js
10-
it("Возводит x в степень n", function() {
11-
var x = 5;
10+
it("Raises x to the power n", function() {
11+
let x = 5;
1212

13-
var result = x;
13+
let result = x;
1414
assert.equal(pow(x, 1), result);
1515

16-
var result *= x;
16+
result *= x;
1717
assert.equal(pow(x, 2), result);
1818

19-
var result *= x;
19+
result *= x;
2020
assert.equal(pow(x, 3), result);
2121
});
2222
```
2323

24-
P.S. Синтаксически он верен и работает, но спроектирован неправильно.
24+
P.S. Syntactically it's correct and passes.
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
Да, возможны.
1+
Yes, it's possible.
22

3-
Они должны возвращать одинаковый объект. При этом если функция возвращает объект, то `this` не используется.
3+
If a function returns an object then `new` returns it instead of `this`.
44

5-
Например, они могут вернуть один и тот же объект `obj`, определённый снаружи:
5+
So thay can, for instance, return the same externally defined object `obj`:
66

77
```js run no-beautify
8-
var obj = {};
8+
let obj = {};
99

1010
function A() { return obj; }
1111
function B() { return obj; }
1212

13-
var a = new A;
14-
var b = new B;
15-
16-
alert( a == b ); // true
13+
alert( new A() == new B() ); // true
1714
```
18-

‎1-js/04-object-basics/06-constructor-new/1-two-functions-one-object/task.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 2
22

33
---
44

5-
# Две функции один объект
5+
# Two functions -- one object
66

7-
Возможны ли такие функции `A` и `B` в примере ниже, что соответствующие объекты `a,b` равны (см. код ниже)?
7+
Is it possible to create functions `A` and `B` such as `new A()==new B()`?
88

99
```js no-beautify
1010
function A() { ... }
@@ -16,4 +16,4 @@ var b = new B;
1616
alert( a == b ); // true
1717
```
1818

19-
Если да -- приведите пример кода с такими функциями.
19+
If it is, then provide an example of their code.
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
sinon.stub(window, "prompt")
2-
3-
prompt.onCall(0).returns("2");
4-
prompt.onCall(1).returns("3");
51

62
describe("calculator", function() {
7-
var calculator;
3+
let calculator;
84
before(function() {
5+
sinon.stub(window, "prompt")
6+
7+
prompt.onCall(0).returns("2");
8+
prompt.onCall(1).returns("3");
9+
910
calculator = new Calculator();
1011
calculator.read();
1112
});
1213

13-
it("при вводе 2 и 3 сумма равна 5", function() {
14+
it("when 2 and 3 are entered, the sum is 5", function() {
1415
assert.equal(calculator.sum(), 5);
1516
});
1617

17-
it("при вводе 2 и 3 произведение равно 6", function() {
18+
it("when 2 and 3 are entered, the product is 6", function() {
1819
assert.equal(calculator.mul(), 6);
1920
});
2021

22+
after(function() {
23+
prompt.restore();
24+
});
2125
});
22-
23-
after(function() {
24-
prompt.restore();
25-
});

‎1-js/04-object-basics/06-constructor-new/2-calculator-constructor/solution.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ function Calculator() {
1717
};
1818
}
1919

20-
var calculator = new Calculator();
20+
let calculator = new Calculator();
2121
calculator.read();
2222

23-
alert( "Сумма=" + calculator.sum() );
24-
alert( "Произведение=" + calculator.mul() );
23+
alert( "Sum=" + calculator.sum() );
24+
alert( "Mul=" + calculator.mul() );
2525
```
26-

‎1-js/04-object-basics/06-constructor-new/2-calculator-constructor/task.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@ importance: 5
22

33
---
44

5-
# Создать Calculator при помощи конструктора
5+
# Create new Calculator
66

7-
Напишите *функцию-конструктор* `Calculator`, которая создает объект с тремя методами:
7+
Create a constructor function `Calculator` that creates objects with 3 methods:
88

9-
- Метод `read()` запрашивает два значения при помощи `prompt` и запоминает их в свойствах объекта.
10-
- Метод `sum()` возвращает сумму запомненных свойств.
11-
- Метод `mul()` возвращает произведение запомненных свойств.
9+
- `read()` asks for two values using `prompt` and remembers them in object properties.
10+
- `sum()` returns the sum of these properties.
11+
- `mul()` returns the multiplication product of these properties.
1212

13-
Пример использования:
13+
For instance:
1414

1515
```js
16-
var calculator = new Calculator();
16+
let calculator = new Calculator();
1717
calculator.read();
1818

19-
alert( "Сумма=" + calculator.sum() );
20-
alert( "Произведение=" + calculator.mul() );
19+
alert( "Sum=" + calculator.sum() );
20+
alert( "Mul=" + calculator.mul() );
2121
```
2222

2323
[demo]
24-

‎1-js/04-object-basics/06-constructor-new/3-accumulator/_js.view/solution.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function Accumulator(startingValue) {
22
this.value = startingValue;
33

44
this.read = function() {
5-
this.value += +prompt('Сколько добавлять будем?', 0);
5+
this.value += +prompt('How much to add?', 0);
66
};
77

8-
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
describe("Accumulator(1)", function() {
2-
var accumulator;
3-
before(function() {
4-
accumulator = new Accumulator(1);
5-
});
1+
describe("Accumulator", function() {
62

73
beforeEach(function() {
84
sinon.stub(window, "prompt")
@@ -12,26 +8,23 @@ describe("Accumulator(1)", function() {
128
prompt.restore();
139
});
1410

15-
it("начальное значение 1", function() {
11+
it("initial value is the argument of the constructor", function() {
12+
let accumulator = new Accumulator(1);
13+
1614
assert.equal(accumulator.value, 1);
1715
});
1816

19-
it("после ввода 0 значение 1", function() {
17+
it("after reading 0, the value is 1", function() {
18+
let accumulator = new Accumulator(1);
2019
prompt.returns("0");
2120
accumulator.read();
2221
assert.equal(accumulator.value, 1);
2322
});
2423

25-
it("после ввода 1 значение 2", function() {
24+
it("after reading 1, the value is 2", function() {
25+
let accumulator = new Accumulator(1);
2626
prompt.returns("1");
2727
accumulator.read();
2828
assert.equal(accumulator.value, 2);
2929
});
30-
31-
it("после ввода 2 значение 4", function() {
32-
prompt.returns("2");
33-
accumulator.read();
34-
assert.equal(accumulator.value, 4);
35-
});
36-
37-
});
30+
});

‎1-js/04-object-basics/06-constructor-new/3-accumulator/solution.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ function Accumulator(startingValue) {
55
this.value = startingValue;
66

77
this.read = function() {
8-
this.value += +prompt('Сколько добавлять будем?', 0);
8+
this.value += +prompt('How much to add?', 0);
99
};
1010

1111
}
1212

13-
var accumulator = new Accumulator(1);
13+
let accumulator = new Accumulator(1);
1414
accumulator.read();
1515
accumulator.read();
16-
alert( accumulator.value );
16+
alert(accumulator.value);
1717
```
18-

‎1-js/04-object-basics/06-constructor-new/3-accumulator/task.md

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,24 @@ importance: 5
22

33
---
44

5-
# Создать Accumulator при помощи конструктора
5+
# Create new Accumulator
66

7-
Напишите *функцию-конструктор* `Accumulator(startingValue)`.
8-
Объекты, которые она создает, должны хранить текущую сумму и прибавлять к ней то, что вводит посетитель.
7+
Create a constructor function `Accumulator(startingValue)`.
98

10-
Более формально, объект должен:
9+
Object that it creates should:
1110

12-
- Хранить текущее значение в своём свойстве `value`. Начальное значение свойства `value` ставится конструктором равным `startingValue`.
13-
- Метод `read()` вызывает `prompt`, принимает число и прибавляет его к свойству `value`.
11+
- Store the "current value" in the property `value`. The starting value is set to the argument of the constructor `startingValue`.
12+
- The `read()` method should use `prompt` to read a new number and add it to `value`.
1413

15-
Таким образом, свойство `value` является текущей суммой всего, что ввел посетитель при вызовах метода `read()`, с учетом начального значения `startingValue`.
14+
In other words, the `value` property is the sum of all user-entered values with the initial value `startingValue`.
1615

17-
Ниже вы можете посмотреть работу кода:
16+
Here's the demo of the code:
1817

1918
```js
20-
var accumulator = new Accumulator(1); // начальное значение 1
21-
accumulator.read(); // прибавит ввод prompt к текущему значению
22-
accumulator.read(); // прибавит ввод prompt к текущему значению
23-
alert( accumulator.value ); // выведет текущее значение
19+
let accumulator = new Accumulator(1); // initial value 1
20+
accumulator.read(); // adds the user-entered value
21+
accumulator.read(); // adds the user-entered value
22+
alert(accumulator.value); // shows the sum of these values
2423
```
2524

2625
[demo]
27-

‎1-js/04-object-basics/06-constructor-new/4-calculator-extendable/_js.view/solution.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
function Calculator() {
22

3-
var methods = {
4-
"-": function(a, b) {
5-
return a - b;
6-
},
7-
"+": function(a, b) {
8-
return a + b;
9-
}
3+
let methods = {
4+
"-": (a, b) => a - b,
5+
"+": (a, b) => a + b
106
};
117

128
this.calculate = function(str) {
139

14-
var split = str.split(' '),
10+
let split = str.split(' '),
1511
a = +split[0],
1612
op = split[1],
1713
b = +split[2]
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
var calculator;
2-
before(function() {
3-
calculator = new Calculator;
4-
});
1+
describe("Calculator", function() {
2+
let calculator;
53

6-
it("calculate(12 + 34) = 46", function() {
7-
assert.equal(calculator.calculate("12 + 34"), 46);
8-
});
4+
before(function() {
5+
calculator = new Calculator;
6+
});
97

10-
it("calculate(34 - 12) = 22", function() {
11-
assert.equal(calculator.calculate("34 - 12"), 22);
12-
});
8+
it("calculate(12 + 34) = 46", function() {
9+
assert.equal(calculator.calculate("12 + 34"), 46);
10+
});
1311

14-
it("добавили умножение: calculate(2 * 3) = 6", function() {
15-
calculator.addMethod("*", function(a, b) {
16-
return a * b;
12+
it("calculate(34 - 12) = 22", function() {
13+
assert.equal(calculator.calculate("34 - 12"), 22);
14+
});
15+
16+
it("add multiplication: calculate(2 * 3) = 6", function() {
17+
calculator.addMethod("*", (a, b) => a * b);
18+
assert.equal(calculator.calculate("2 * 3"), 6);
1719
});
18-
assert.equal(calculator.calculate("2 * 3"), 6);
19-
});
2020

21-
it("добавили возведение в степень: calculate(2 ** 3) = 8", function() {
22-
calculator.addMethod("**", function(a, b) {
23-
return Math.pow(a, b);
21+
it("add power: calculate(2 ** 3) = 8", function() {
22+
calculator.addMethod("**", (a, b) => a ** b);
23+
assert.equal(calculator.calculate("2 ** 3"), 8);
2424
});
25-
assert.equal(calculator.calculate("2 ** 3"), 8);
26-
});
25+
});

0 commit comments

Comments
 (0)