SHARE
TWEET

Untitled

a guest Feb 16th, 2020 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. document.addEventListener('keydown', function(event) {
  2.     if (event.keyCode == 8) {
  3.         let oldstr = document.getElementById("display").value
  4.         let newstr = oldstr.substring(0, oldstr.length - 1);
  5.         document.getElementById("display").value = newstr;
  6.     }
  7. })
  8.  
  9. function ce() {
  10.     let oldstr = document.getElementById("display").value
  11.     let newstr = oldstr.substring(0, oldstr.length - 1);
  12.     document.getElementById("display").value = newstr;
  13. }
  14.  
  15. function clearall() {
  16.     document.getElementById("display").value = "0";
  17.     document.getElementById("expression").value = "";
  18. }
  19.  
  20. function operator(sign) {
  21.     window.entered = "false";
  22.     let displayed_value = document.getElementById("display").value;
  23.     let existing_operator = displayed_value.charAt(displayed_value.length-1);
  24.     if (displayed_value === '0') {
  25.         document.getElementById("display").value = sign;
  26.     } else if (displayed_value === "Error") {
  27.         document.getElementById("display").value = window.previous_expression;
  28.     } else if (existing_operator == '/' || existing_operator =='*' || existing_operator =='+' || existing_operator == '-' || existing_operator == '.') {
  29.         let oldstr = displayed_value.substring(0, displayed_value.length - 1);
  30.         document.getElementById("display").value = oldstr + sign;
  31.     }
  32.     else {
  33.        
  34.         document.getElementById("display").value += sign;
  35.     }
  36. }
  37.  
  38. function equals() {
  39.     window.entered = "false";
  40.     try {
  41.         let expression = document.getElementById("display").value;
  42.         window.previous_expression = expression;
  43.         if (expression === "Error") {
  44.             document.getElementById("expression").value = previous_expression;
  45.             expression.value = "Error";
  46.  
  47.         } else {
  48.             window.entered = "true";
  49.             document.getElementById("expression").value = previous_expression;
  50.             document.getElementById("display").value = eval(document.getElementById("display").value);
  51.         }
  52.     } catch (e) {
  53.         document.getElementById("expression").value = previous_expression;
  54.  
  55.         document.getElementById("display").value = "Error";
  56.     }
  57. }
  58.  
  59. function number(obj) {
  60.  
  61.     let displayed_value = document.getElementById("display").value;
  62.  
  63.     if (displayed_value === '0') {
  64.         document.getElementById("display").value = obj;
  65.     } else if (displayed_value === "Error")
  66.     {
  67.         window.entered = "false";
  68.         document.getElementById("display").value = window.saved_expression + obj;
  69.     } else {
  70.         if (window.entered == "true") {
  71.             document.getElementById("display").value = obj;
  72.             window.entered = "false";
  73.         } else {
  74.             document.getElementById("display").value += obj;
  75.             window.entered = "false";
  76.         }
  77.  
  78.     }
  79. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top