This commit is contained in:
Ilya Kantor 2019-08-27 08:20:18 +03:00
parent 6637d0ccae
commit 5c8d24a582
4 changed files with 8 additions and 9 deletions

View file

@ -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

View file

@ -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;
}; };
} }

View file

@ -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"]

View file

@ -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.