Advertisement
Guest User

--

a guest
Apr 9th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict"
  2. // I rewrote this task. Notes copied form prev.
  3. // :NOTE: operands should be referred to prototype of BinExp
  4. // :NOTE: copy-paste code for prototype assignment
  5.  
  6. function Const(val){
  7.     this.val = val;
  8. }
  9. Const.prototype.evaluate = function(x, y, z){
  10.     return this.val;
  11. };
  12. Const.prototype.toString = function(){
  13.     return this.val + "";
  14. };
  15.  
  16. function Variable(val){
  17.     this.val = val;
  18. }
  19. Variable.prototype.evaluate = function(x, y, z){
  20.             let a = {"x" : x, "y" : y, "z" : z};
  21.             return a[this.val];
  22.         };
  23. Variable.prototype.toString = function(){
  24.     return this.val;
  25. }
  26.  
  27. let AbstractExpression = {
  28.     evaluate : function(x, y, z){return this.operation(...this.operands.map((currentValue) => {
  29.         return currentValue.evaluate(x, y, z);
  30.     }))},
  31.     "toString" : function(){
  32.         let result = "";
  33.         for(let i of this.operands){
  34.             result += i.toString() + " ";
  35.         }
  36.         return result + this.sign;
  37.     }
  38. }
  39.  
  40. function Operation(operation, sign){
  41.     let op = function(...operands) {
  42.         this.operands = operands;
  43.     }
  44.     op.prototype = Object.create(AbstractExpression);
  45.     op.prototype.operation = operation;
  46.     op.prototype.sign = sign;
  47.     return op;
  48. }
  49. //===============base==============
  50. let Add = Operation((a, b) => (a+b), "+");
  51. let Subtract = Operation((a,b) => (a-b), "-");
  52. let Multiply = Operation((a,b) => a*b, "*");
  53. let Divide = Operation((a, b) => a/b, "/")p;
  54. let Negate = Operation((a) => -a, "negate");
  55. //==============easy============
  56. let Sinh = Operation((a) => Math.sinh(a), "sinh");
  57. let Cosh = Operation((a) => Math.cosh(a), "cosh");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement