ok
This commit is contained in:
parent
9064e35f3f
commit
4c531b5ae7
371 changed files with 338 additions and 316 deletions
|
@ -0,0 +1,14 @@
|
|||
let calculator = {
|
||||
sum() {
|
||||
return this.a + this.b;
|
||||
},
|
||||
|
||||
mul() {
|
||||
return this.a * this.b;
|
||||
},
|
||||
|
||||
read() {
|
||||
this.a = +prompt('a?', 0);
|
||||
this.b = +prompt('b?', 0);
|
||||
}
|
||||
};
|
|
@ -0,0 +1,28 @@
|
|||
|
||||
|
||||
describe("calculator", function() {
|
||||
|
||||
context("when 2 and 3 entered", function() {
|
||||
beforeEach(function() {
|
||||
sinon.stub(window, "prompt");
|
||||
|
||||
prompt.onCall(0).returns("2");
|
||||
prompt.onCall(1).returns("3");
|
||||
|
||||
calculator.read();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
prompt.restore();
|
||||
});
|
||||
|
||||
it("the sum is 5", function() {
|
||||
assert.equal(calculator.sum(), 5);
|
||||
});
|
||||
|
||||
it("the multiplication product is 6", function() {
|
||||
assert.equal(calculator.mul(), 6);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
|
@ -0,0 +1,23 @@
|
|||
|
||||
|
||||
```js run demo
|
||||
let calculator = {
|
||||
sum() {
|
||||
return this.a + this.b;
|
||||
},
|
||||
|
||||
mul() {
|
||||
return this.a * this.b;
|
||||
},
|
||||
|
||||
read() {
|
||||
this.a = +prompt('a?', 0);
|
||||
this.b = +prompt('b?', 0);
|
||||
}
|
||||
};
|
||||
|
||||
calculator.read();
|
||||
alert( calculator.sum() );
|
||||
alert( calculator.mul() );
|
||||
```
|
||||
|
24
1-js/4-object-basics/03-object-methods/7-calculator/task.md
Normal file
24
1-js/4-object-basics/03-object-methods/7-calculator/task.md
Normal file
|
@ -0,0 +1,24 @@
|
|||
importance: 5
|
||||
|
||||
---
|
||||
|
||||
# Create a calculator
|
||||
|
||||
Create an object `calculator` with three methods:
|
||||
|
||||
- `read()` prompts for two values and saves them as object properties.
|
||||
- `sum()` returns the sum of saved values.
|
||||
- `mul()` multiplies saved values and returns the result.
|
||||
|
||||
```js
|
||||
let calculator = {
|
||||
// ... your code ...
|
||||
};
|
||||
|
||||
calculator.read();
|
||||
alert( calculator.sum() );
|
||||
alert( calculator.mul() );
|
||||
```
|
||||
|
||||
[demo]
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue