closes #1271
This commit is contained in:
parent
6637d0ccae
commit
5c8d24a582
4 changed files with 8 additions and 9 deletions
|
@ -162,9 +162,6 @@ function SmallUser() {
|
||||||
this.name = "John";
|
this.name = "John";
|
||||||
|
|
||||||
return; // finishes the execution, returns this
|
return; // finishes the execution, returns this
|
||||||
|
|
||||||
// ...
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
alert( new SmallUser().name ); // John
|
alert( new SmallUser().name ); // John
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
function Calculator() {
|
function Calculator() {
|
||||||
|
|
||||||
let methods = {
|
this.methods = {
|
||||||
"-": (a, b) => a - b,
|
"-": (a, b) => a - b,
|
||||||
"+": (a, b) => a + b
|
"+": (a, b) => a + b
|
||||||
};
|
};
|
||||||
|
@ -12,14 +12,14 @@ function Calculator() {
|
||||||
op = split[1],
|
op = split[1],
|
||||||
b = +split[2]
|
b = +split[2]
|
||||||
|
|
||||||
if (!methods[op] || isNaN(a) || isNaN(b)) {
|
if (!this.methods[op] || isNaN(a) || isNaN(b)) {
|
||||||
return NaN;
|
return NaN;
|
||||||
}
|
}
|
||||||
|
|
||||||
return methods[op](a, b);
|
return this.methods[op](a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addMethod = function(name, func) {
|
this.addMethod = function(name, func) {
|
||||||
methods[name] = func;
|
this.methods[name] = func;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
|
||||||
- Please note how methods are stored. They are simply added to the internal object.
|
- Please note how methods are stored. They are simply added to `this.methods` property.
|
||||||
- All tests and numeric conversions are done in the `calculate` method. In future it may be extended to support more complex expressions.
|
- All tests and numeric conversions are done in the `calculate` method. In future it may be extended to support more complex expressions.
|
||||||
|
|
||||||
|
[js src="_js/solution.js"]
|
||||||
|
|
|
@ -31,6 +31,6 @@ The task consists of two parts.
|
||||||
alert( result ); // 8
|
alert( result ); // 8
|
||||||
```
|
```
|
||||||
|
|
||||||
- No brackets or complex expressions in this task.
|
- No parentheses or complex expressions in this task.
|
||||||
- The numbers and the operator are delimited with exactly one space.
|
- The numbers and the operator are delimited with exactly one space.
|
||||||
- There may be error handling if you'd like to add it.
|
- There may be error handling if you'd like to add it.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue