Advertisement
Guest User

Calculator

a guest
Mar 16th, 2015
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var textBox = document.getElementById("textBox");
  2. var memory = 0;
  3.  
  4. if(typeof(Storage) != "undefined") {
  5.     localStorage.setItem("number", memory);
  6. }
  7. else {
  8.     alert("No 'localStorage' support");
  9. }
  10.  
  11. function numButton(num) {
  12.     if(typeof(textBox.value) != "string")
  13.     {
  14.         emptyBox();
  15.     }
  16.         textBox.value += num;
  17. }
  18.  
  19. function emptyBox() {
  20.     textBox.value = "";
  21. }
  22.  
  23. function mathStuff(operator) {
  24.     emptyBox();
  25.     switch(operator) {
  26.         case 'plus':
  27.             memory = memory + Number(textBox.value);
  28.             emptyBox();
  29.             break;
  30.         case 'minus':
  31.             memory = memory - Number(textBox.value);
  32.             emptyBox();
  33.             break;
  34.         case 'multiply':
  35.             if(textBox.value === 0) {
  36.                 memory = Number(textBox.value) * 1;
  37.             }
  38.             else {
  39.                 memory = Number(textBox.value) * memory;
  40.             }
  41.             emptyBox();
  42.             break;
  43.         case 'divide':
  44.             if(textBox.value === 0) {
  45.                 sysError('CAN NOT DIVIDE BY ZERO');
  46.             }
  47.             else {
  48.                 memory = Number(textBox.value) / memory;
  49.                 emptyBox();
  50.             }
  51.             break;
  52.         case 'equal':
  53.             textBox.value = memory;
  54.             break;
  55.         default:
  56.             sysError('UNKNOWN ERROR');
  57.             break;
  58.     }
  59. }
  60.  
  61. function otherButtons(task) {
  62.     switch(task) {
  63.         case 'ce':
  64.             memory = 0;
  65.             break;
  66.         case 'c':
  67.             textBox.value = "";
  68.             break;
  69.         default:
  70.             sysError('UNKNOWN ERROR');
  71.             break;
  72.     }
  73. }
  74.  
  75. function sysError(message) {
  76.     // Add message to text box
  77.     emptyBox();
  78.     textBox.value += message;
  79. }
  80.  
  81. document.getElementById("test").innerText += localStorage.getItem("number");
  82.  
  83. if(parseInt(memory) === 0 && memory > 0) {
  84.     for(var i = 0; i < memory.length+1; i++) {
  85.         if(i === 0) {
  86.             // Remove 0 from the number
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement