closes #441
This commit is contained in:
parent
12bdf98aff
commit
8d69ef158f
5 changed files with 14 additions and 4 deletions
|
@ -1,25 +0,0 @@
|
|||
function Calculator() {
|
||||
|
||||
let methods = {
|
||||
"-": (a, b) => a - b,
|
||||
"+": (a, b) => a + b
|
||||
};
|
||||
|
||||
this.calculate = function(str) {
|
||||
|
||||
let split = str.split(' '),
|
||||
a = +split[0],
|
||||
op = split[1],
|
||||
b = +split[2]
|
||||
|
||||
if (!methods[op] || isNaN(a) || isNaN(b)) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
return methods[op](a, b);
|
||||
}
|
||||
|
||||
this.addMethod = function(name, func) {
|
||||
methods[name] = func;
|
||||
};
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
describe("Calculator", function() {
|
||||
let calculator;
|
||||
|
||||
before(function() {
|
||||
calculator = new Calculator;
|
||||
});
|
||||
|
||||
it("calculate(12 + 34) = 46", function() {
|
||||
assert.equal(calculator.calculate("12 + 34"), 46);
|
||||
});
|
||||
|
||||
it("calculate(34 - 12) = 22", function() {
|
||||
assert.equal(calculator.calculate("34 - 12"), 22);
|
||||
});
|
||||
|
||||
it("add multiplication: calculate(2 * 3) = 6", function() {
|
||||
calculator.addMethod("*", (a, b) => a * b);
|
||||
assert.equal(calculator.calculate("2 * 3"), 6);
|
||||
});
|
||||
|
||||
it("add power: calculate(2 ** 3) = 8", function() {
|
||||
calculator.addMethod("**", (a, b) => a ** b);
|
||||
assert.equal(calculator.calculate("2 ** 3"), 8);
|
||||
});
|
||||
});
|
|
@ -1,3 +0,0 @@
|
|||
|
||||
- Please note how methods are stored. They are simply added to the internal object.
|
||||
- All tests and numeric conversions are done in the `calculate` method. In future it may be extended to support more complex expressions.
|
|
@ -1,36 +0,0 @@
|
|||
importance: 5
|
||||
|
||||
---
|
||||
|
||||
# Create an extendable calculator
|
||||
|
||||
Create a constructor function `Calculator` that creates "extendable" calculator objects.
|
||||
|
||||
The task consists of two parts.
|
||||
|
||||
1. First, implement the method `calculate(str)` that takes a string like `"1 + 2"` in the format "NUMBER operator NUMBER" (space-delimited) and returns the result. Should understand plus `+` and minus `-`.
|
||||
|
||||
Usage example:
|
||||
|
||||
```js
|
||||
let calc = new Calculator;
|
||||
|
||||
alert( calc.calculate("3 + 7") ); // 10
|
||||
```
|
||||
2. Then add the method `addMethod(name, func)` that teaches the calculator a new operation. It takes the operator `name` and the two-argument function `func(a,b)` that implements it.
|
||||
|
||||
For instance, let's add the multiplication `*`, division `/` and power `**`:
|
||||
|
||||
```js
|
||||
let powerCalc = new Calculator;
|
||||
powerCalc.addMethod("*", (a, b) => a * b);
|
||||
powerCalc.addMethod("/", (a, b) => a / b);
|
||||
powerCalc.addMethod("**", (a, b) => a ** b);
|
||||
|
||||
let result = powerCalc.calculate("2 ** 3");
|
||||
alert( result ); // 8
|
||||
```
|
||||
|
||||
- No brackets or complex expressions in this task.
|
||||
- The numbers and the operator are delimited with exactly one space.
|
||||
- There may be error handling if you'd like to add it.
|
|
@ -85,6 +85,10 @@ The constructor can't be called again, because it is not saved anywhere, just cr
|
|||
|
||||
## Dual-syntax constructors: new.target
|
||||
|
||||
```smart header="Advanced stuff"
|
||||
The syntax from this section is rarely used, skip it unless you want to know everything.
|
||||
```
|
||||
|
||||
Inside a function, we can check whether it was called with `new` or without it, using a special `new.target` property.
|
||||
|
||||
It is empty for regular calls and equals the function if called with `new`:
|
||||
|
@ -94,14 +98,18 @@ function User() {
|
|||
alert(new.target);
|
||||
}
|
||||
|
||||
// without new:
|
||||
// without "new":
|
||||
*!*
|
||||
User(); // undefined
|
||||
*/!*
|
||||
|
||||
// with new:
|
||||
// with "new":
|
||||
*!*
|
||||
new User(); // function User { ... }
|
||||
*/!*
|
||||
```
|
||||
|
||||
That can be used to allow both `new` and regular syntax to work the same:
|
||||
That can be used to allow both `new` and regular calls to work the same. That is, create the same object:
|
||||
|
||||
```js run
|
||||
function User(name) {
|
||||
|
@ -116,7 +124,9 @@ let john = User("John"); // redirects call to new User
|
|||
alert(john.name); // John
|
||||
```
|
||||
|
||||
This approach is sometimes used in libraries to make the syntax more flexible. Probably not a good thing to use everywhere though, because omitting `new` makes it a bit less obvious what's going on. With `new` we all know that the new object is being created, that's a good thing.
|
||||
This approach is sometimes used in libraries to make the syntax more flexible. So that people may call the function with or without `new`, and it still works.
|
||||
|
||||
Probably not a good thing to use everywhere though, because omitting `new` makes it a bit less obvious what's going on. With `new` we all know that the new object is being created.
|
||||
|
||||
## Return from constructors
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue