Advertisement
vic_alexiev

Calculator.js

Oct 31st, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* This Script is from www.htmlfreecodes.com, Coded by: Krishna Eydat */
  2.  
  3. var keypad = document.keypad;
  4. var accumulate = 0;
  5. var flagNewNum = false;
  6. var pendingOp = "";
  7.  
  8. function numPressed(num) {
  9.     if (flagNewNum) {
  10.         keypad.result.value = num;
  11.         flagNewNum = false;
  12.     }
  13.     else {
  14.         if (keypad.result.value == "0")
  15.             keypad.result.value = num;
  16.         else
  17.             keypad.result.value += num;
  18.     }
  19. }
  20.  
  21. function operation(op) {
  22.     var result = keypad.result.value;
  23.     if (flagNewNum && pendingOp != "=");
  24.     else {
  25.         flagNewNum = true;
  26.         if (pendingOp == '+')
  27.             accumulate += parseFloat(result);
  28.         else if (pendingOp == '-')
  29.             accumulate -= parseFloat(result);
  30.         else if (pendingOp == '/')
  31.             accumulate /= parseFloat(result);
  32.         else if (pendingOp == '*')
  33.             accumulate *= parseFloat(result);
  34.         else
  35.             accumulate = parseFloat(result);
  36.         keypad.result.value = accumulate;
  37.         pendingOp = op;
  38.     }
  39. }
  40.  
  41. function decimal() {
  42.     var curResult = keypad.result.value;
  43.     if (flagNewNum) {
  44.         curResult = "0.";
  45.         flagNewNum = false;
  46.     }
  47.     else {
  48.         if (curResult.indexOf(".") == -1)
  49.             curResult += ".";
  50.     }
  51.     keypad.result.value = curResult;
  52. }
  53.  
  54. function clearEntry() {
  55.     result = "0";
  56.     keypad.result.value = "0";
  57.     flagNewNum = true;
  58. }
  59.  
  60. function clearAll() {
  61.     accumulate = 0;
  62.     pendingOp = "";
  63.     clearEntry();
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement