WindFell

Calculator.js

Jul 31st, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Calculator(leftOperand, operator, rightOperand) {
  2.     this.leftOperand = leftOperand;
  3.     this.operator = operator;
  4.     this.rightOperand = rightOperand;
  5.  
  6.     this.calculateResult = function () {
  7.         let result = 0;
  8.  
  9.         switch (this.operator) {
  10.             case '+':
  11.                 result = this.leftOperand + this.rightOperand;
  12.                 break;
  13.             case '-':
  14.                 result = this.leftOperand - this.rightOperand;
  15.                 break;
  16.             case '*':
  17.                 result = this.leftOperand * this.rightOperand;
  18.                 break;
  19.             case '/':
  20.                 result = this.leftOperand / this.rightOperand;
  21.                 break;
  22.             case '^':
  23.                 result = this.leftOperand ** this.rightOperand;
  24.                 break;
  25.         }
  26.  
  27.         return result;
  28.     }
  29. }
  30.  
  31. module.exports = Calculator;
Advertisement
Add Comment
Please, Sign In to add comment