Advertisement
Thefilee

jquery

Sep 25th, 2019
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.72 KB | None | 0 0
  1. function Calculator()
  2. {
  3.     that        = this,
  4.     this.field  = "input#number",
  5.     this.button = "#body .buttons",
  6.     this.init   = false,
  7.    
  8.     this.run = function()
  9.     {
  10.         $(this.button).click(function() {
  11.             var value = $(this).html();
  12.  
  13.             if (that.init == false)
  14.             {
  15.                 $(that.field).val("");
  16.                 that.init = true;
  17.             }
  18.  
  19.             if (value != "=")
  20.                 $(that.field).val($(that.field).val() + value);
  21.  
  22.             that.dispatcher(value);
  23.         });
  24.     },
  25.  
  26.     this.dispatcher = function(value)
  27.     {
  28.         if ($(this.field).val().indexOf("/") != -1)
  29.             this.operation(value, "/");
  30.         if ($(this.field).val().indexOf("*") != -1)
  31.             this.operation(value, "*");
  32.         if ($(this.field).val().indexOf("-") != -1)
  33.             this.operation(value, "-");
  34.         if ($(this.field).val().indexOf("+") != -1)
  35.             this.operation(value, "+");
  36.     },
  37.  
  38.     this.operation = function(value, symbol)
  39.     {
  40.         var numbers = $(this.field).val().split(symbol),
  41.             result;
  42.  
  43.         if (symbol == "/")
  44.             result = numbers[0] / numbers[1];
  45.         else if (symbol == "*")
  46.             result = numbers[0] * numbers[1];
  47.         else if (symbol == "-")
  48.             result = numbers[0] - numbers[1];
  49.         else if (symbol == "+")
  50.             result = parseFloat(numbers[0]) + parseFloat(numbers[1]);
  51.  
  52.         result = Math.round((result) * 100) / 100;
  53.  
  54.         if (numbers.length > 1)
  55.         {
  56.             if (value == "=")
  57.                 $(this.field).val(result);
  58.             else if (numbers.length > 2)
  59.                 $(this.field).val(result + symbol);
  60.         }
  61.     };
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement