Advertisement
NewBestPastebins

Popup Calculator

Aug 9th, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. // Create a div element to hold the calculator
  2. var calcDiv = document.createElement("div");
  3. calcDiv.id = "calcDiv";
  4. calcDiv.style.position = "fixed";
  5. calcDiv.style.bottom = "0";
  6. calcDiv.style.right = "0";
  7. calcDiv.style.width = "300px";
  8. calcDiv.style.height = "400px";
  9. calcDiv.style.backgroundColor = "#088DA5";
  10. calcDiv.style.border = "1px solid black";
  11. calcDiv.style.zIndex = "9999";
  12.  
  13. // Create a button to close and open the calculator
  14. var calcBtn = document.createElement("button");
  15. calcBtn.id = "calcBtn";
  16. calcBtn.innerHTML = "Lommeregner";
  17. calcBtn.style.position = "absolute";
  18. calcBtn.style.top = "-40px";
  19. calcBtn.style.left = "0";
  20. calcBtn.style.width = "200px";
  21. calcBtn.style.height = "40px";
  22.  
  23. // Add an event listener to toggle the visibility of the calculator
  24. calcBtn.addEventListener("click", function() {
  25. if (calcDiv.style.height == "400px") {
  26. calcDiv.style.height = "20px"; // Change the height of the div element to hide the calculator
  27. } else {
  28. calcDiv.style.height = "400px"; // Change the height of the div element to show the calculator
  29. }
  30. });
  31.  
  32. // Append the button to the div element
  33. calcDiv.appendChild(calcBtn);
  34.  
  35. // Create an input element to display the result
  36. var calcResult = document.createElement("input");
  37. calcResult.id = "calcResult";
  38. calcResult.type = "text";
  39. calcResult.value = "0";
  40. calcResult.readOnly = true;
  41. calcResult.style.width = "290px";
  42. calcResult.style.height = "40px";
  43. calcResult.style.fontSize = "24px";
  44. calcResult.style.textAlign = "right";
  45.  
  46. // Append the input element to the div element
  47. calcDiv.appendChild(calcResult);
  48.  
  49. // Create an array of buttons for the calculator
  50. var calcButtons = [
  51. {value: "7", type: "number"},
  52. {value: "8", type: "number"},
  53. {value: "9", type: "number"},
  54. {value: "/", type: "operator"},
  55. {value: "4", type: "number"},
  56. {value: "5", type: "number"},
  57. {value: "6", type: "number"},
  58. {value: "*", type: "operator"},
  59. {value: "1", type: "number"},
  60. {value: "2", type: "number"},
  61. {value: "3", type: "number"},
  62. {value: "-", type: "operator"},
  63. {value: ".", type: "decimal"},
  64. {value: "0", type: "number"},
  65. {value: "=", type: "equal"},
  66. {value: "+", type: "operator"}
  67. ];
  68.  
  69. // Create a variable to store the current expression
  70. var calcExpression = "";
  71.  
  72. // Create a function to handle the button click
  73. function calcButtonClick(event) {
  74. // Get the value of the clicked button
  75. var buttonValue = event.target.value;
  76.  
  77. // Check the type of the button
  78. if (buttonValue == "=") {
  79. // Evaluate the expression and display the result
  80. try {
  81. var result = eval(calcExpression);
  82. calcResult.value = result;
  83. calcExpression = result.toString();
  84. } catch (error) {
  85. // Display an error message if the expression is invalid
  86. calcResult.value = error.message;
  87. calcExpression = "";
  88. }
  89. } else if (buttonValue == ".") {
  90. // Append a decimal point only if there is no decimal point in the last number
  91. var lastNumber = calcExpression.split(/[\+\-\*\/]/).pop();
  92. if (lastNumber.indexOf(".") == -1) {
  93. calcExpression += buttonValue;
  94. calcResult.value = calcExpression;
  95. }
  96. } else {
  97. // Append the button value to the expression and display it
  98. calcExpression += buttonValue;
  99. calcResult.value = calcExpression;
  100. }
  101. }
  102.  
  103. // Loop through the array of buttons and create them
  104. for (var i=0; i<calcButtons.length; i++) {
  105. // Create a button element
  106. var calcButton = document.createElement("button");
  107. calcButton.value = calcButtons[i].value;
  108. calcButton.innerHTML = calcButtons[i].value;
  109. calcButton.style.width = (290/4) + 'px';
  110. calcButton.style.height = (360/4) + 'px';
  111. calcButton.style.fontSize = '24px';
  112.  
  113. // Add an event listener to handle the button click
  114. calcButton.addEventListener("click", calcButtonClick);
  115.  
  116. // Append the button element to the div element
  117. calcDiv.appendChild(calcButton);
  118. }
  119.  
  120. // Append the div element to the body of the document
  121. document.body.appendChild(calcDiv);
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement