Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict"
- // I rewrote this task. Notes copied form prev.
- // :NOTE: operands should be referred to prototype of BinExp
- // :NOTE: copy-paste code for prototype assignment
- function Const(val){
- this.val = val;
- }
- Const.prototype.evaluate = function(x, y, z){
- return this.val;
- };
- Const.prototype.toString = function(){
- return this.val + "";
- };
- function Variable(val){
- this.val = val;
- }
- Variable.prototype.evaluate = function(x, y, z){
- let a = {"x" : x, "y" : y, "z" : z};
- return a[this.val];
- };
- Variable.prototype.toString = function(){
- return this.val;
- }
- let AbstractExpression = {
- evaluate : function(x, y, z){return this.operation(...this.operands.map((currentValue) => {
- return currentValue.evaluate(x, y, z);
- }))},
- "toString" : function(){
- let result = "";
- for(let i of this.operands){
- result += i.toString() + " ";
- }
- return result + this.sign;
- }
- }
- function Operation(operation, sign){
- let op = function(...operands) {
- this.operands = operands;
- }
- op.prototype = Object.create(AbstractExpression);
- op.prototype.operation = operation;
- op.prototype.sign = sign;
- return op;
- }
- //===============base==============
- let Add = Operation((a, b) => (a+b), "+");
- let Subtract = Operation((a,b) => (a-b), "-");
- let Multiply = Operation((a,b) => a*b, "*");
- let Divide = Operation((a, b) => a/b, "/")p;
- let Negate = Operation((a) => -a, "negate");
- //==============easy============
- let Sinh = Operation((a) => Math.sinh(a), "sinh");
- let Cosh = Operation((a) => Math.cosh(a), "cosh");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement