Introduction

In this blog, you will learn how to develop a calculator application in JavaScript. JavaScript is a scripting language used for HTML web pages. It is a lightweight programming language. JavaScript code is used directly in HTML pages.
Step 1
  • Open a Sublime Text editor or Notepad++
Step 2
If you use the (span-two) keyword then the grid Column will appear inside the box. You can create awesome looking buttons using this CSS. Let's create a button using an (outputprevious-operand) keyword.
  1. *, *::before, *::after {
  2. box-sizing: border-box;
  3. font-family: Gotham Rounded, sans-serif;
  4. font-weight: normal;
  5. }
  6. body {
  7. padding: 0;
  8. margin: 0;
  9. background: linear-gradient(to right, #00AAFF, #00FF6C);
  10. }
  11. .calculator-grid {
  12. display: grid;
  13. justify-content: center;
  14. align-content: center;
  15. min-height: 100vh;
  16. grid-template-columns: repeat(4, 100px);
  17. grid-template-rows: minmax(120px, auto) repeat(5, 100px);
  18. }
  19. .calculator-grid > button {
  20. cursor: pointer;
  21. font-size: 2rem;
  22. border: 1px solid white;
  23. outline: none;
  24. background-color: rgba(255, 255, 255, .75);
  25. }
  26. .calculator-grid > button:hover {
  27. background-color: rgba(255, 255, 255, .9);
  28. }
  29. .span-two {
  30. grid-column: span 2;
  31. }
  32. .output {
  33. grid-column: 1 / -1;
  34. background-color: rgba(0, 0, 0, .75);
  35. display: flex;
  36. align-items: flex-end;
  37. justify-content: space-around;
  38. flex-direction: column;
  39. padding: 10px;
  40. word-wrap: break-word;
  41. word-break: break-all;
  42. }
  43. .output .previous-operand {
  44. color: rgba(255, 255, 255, .75);
  45. font-size: 1.5rem;
  46. }
  47. .output .current-operand {
  48. color: white;
  49. font-size: 2.5rem;
  50. }

JavaScript Code

Now write the below JavaScript code in the head section of the <script> inside page.
  1. class Calculator {
  2. constructor(previousOperandTextElement, currentOperandTextElement) {
  3. this.previousOperandTextElement = previousOperandTextElement
  4. this.currentOperandTextElement = currentOperandTextElement
  5. this.clear()
  6. }
  7. clear() {
  8. this.currentOperand = ''
  9. this.previousOperand = ''
  10. this.operation = #ff0000
  11. }
  12. delete() {
  13. this.currentOperand = this.currentOperand.toString().slice(0, -1)
  14. }
  15. appendNumber(number) {
  16. if (number === '.' && this.currentOperand.includes('.')) return
  17. this.currentOperand = this.currentOperand.toString() + number.toString()
  18. }
  19. chooseOperation(operation) {
  20. if (this.currentOperand === '') return
  21. if (this.previousOperand !== '') {
  22. this.compute()
  23. }
  24. this.operation = operation
  25. this.previousOperand = this.currentOperand
  26. this.currentOperand = ''
  27. }
  28. compute() {
  29. let computation
  30. const prev = parseFloat(this.previousOperand)
  31. const current = parseFloat(this.currentOperand)
  32. if (isNaN(prev) || isNaN(current)) return
  33. switch (this.operation) {
  34. case '+':
  35. computation = prev + current
  36. break
  37. case '-':
  38. computation = prev - current
  39. break
  40. case '*':
  41. computation = prev * current
  42. break
  43. case '÷':
  44. computation = prev / current
  45. break
  46. default:
  47. return
  48. }
  49. this.currentOperand = computation
  50. this.operation = undefined
  51. this.previousOperand = ''
  52. }
  53. getDisplayNumber(number) {
  54. const stringNumber = number.toString()
  55. const integerDigits = parseFloat(stringNumber.split('.')[0])
  56. const decimalDigits = stringNumber.split('.')[1]
  57. let integerDisplay
  58. if (isNaN(integerDigits)) {
  59. integerDisplay = ''
  60. } else {
  61. integerDisplay = integerDigits.toLocaleString('en', { maximumFractionDigits: 0 })
  62. }
  63. if (decimalDigits != null) {
  64. return `${integerDisplay}.${decimalDigits}`
  65. } else {
  66. return integerDisplay
  67. }
  68. }
  69. updateDisplay() {
  70. this.currentOperandTextElement.innerText =
  71. this.getDisplayNumber(this.currentOperand)
  72. if (this.operation != null) {
  73. this.previousOperandTextElement.innerText =
  74. `${this.getDisplayNumber(this.previousOperand)} ${this.operation}`
  75. } else {
  76. this.previousOperandTextElement.innerText = ''
  77. }
  78. }
  79. }
  80. const numberButtons = document.querySelectorAll('[data-number]')
  81. const operationButtons = document.querySelectorAll('[data-operation]')
  82. const equalsButton = document.querySelector('[data-equals]')
  83. const deleteButton = document.querySelector('[data-delete]')
  84. const allClearButton = document.querySelector('[data-all-clear]')
  85. const previousOperandTextElement = document.querySelector('[data-previous-operand]')
  86. const currentOperandTextElement = document.querySelector('[data-current-operand]')
  87. const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement)
  88. numberButtons.forEach(button => {
  89. button.addEventListener('click', () => {
  90. calculator.appendNumber(button.innerText)
  91. calculator.updateDisplay()
  92. })
  93. })
  94. operationButtons.forEach(button => {
  95. button.addEventListener('click', () => {
  96. calculator.chooseOperation(button.innerText)
  97. calculator.updateDisplay()
  98. })
  99. })
  100. equalsButton.addEventListener('click', button => {
  101. calculator.compute()
  102. calculator.updateDisplay()
  103. })
  104. allClearButton.addEventListener('click', button => {
  105. calculator.clear()
  106. calculator.updateDisplay()
  107. })
  108. deleteButton.addEventListener('click', button => {
  109. calculator.delete()
  110. calculator.updateDisplay()
  111. })

Html 5 Code

Here, I have written all the HTML5 file code.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Calculator</title>
  8. <link href="styles.css" rel="stylesheet">
  9. <script src="script.js" defer></script>
  10. </head>
  11. <body>
  12. <h1 style="text-align:center; color: blue;">PRABAKARAN ACT</h1>
  13. <h1 style="text-align: center; color:white;"><b>Calculator</b></h1>
  14. <div class="calculator-grid">
  15. <div class="output">
  16. <div data-previous-operand class="previous-operand"></div>
  17. <div data-current-operand class="current-operand"></div>
  18. </div>
  19. <button data-all-clear class="span-two">AC</button>
  20. <button data-delete>DEL</button>
  21. <button data-operation>÷</button>
  22. <button data-number>1</button>
  23. <button data-number>2</button>
  24. <button data-number>3</button>
  25. <button data-operation>*</button>
  26. <button data-number>4</button>
  27. <button data-number>5</button>
  28. <button data-number>6</button>
  29. <button data-operation>+</button>
  30. <button data-number>7</button>
  31. <button data-number>8</button>
  32. <button data-number>9</button>
  33. <button data-operation>-</button>
  34. <button data-number>.</button>
  35. <button data-number>0</button>
  36. <button data-equals class="span-two">=</button>
  37. </div>
  38. </body>
  39. </html>
Output
Now, right-click on the sublime text window stage. A dialog box appears, select -> open a new browser.

Conclusion

In this blog you learned how to create a calculator application in JavaScript. In an upcoming article, you will learn some more advanced game and application concepts in JavaScript.