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.
- *, *::before, *::after {
- box-sizing: border-box;
- font-family: Gotham Rounded, sans-serif;
- font-weight: normal;
- }
-
- body {
- padding: 0;
- margin: 0;
- background: linear-gradient(to right, #00AAFF, #00FF6C);
- }
-
- .calculator-grid {
- display: grid;
- justify-content: center;
- align-content: center;
- min-height: 100vh;
- grid-template-columns: repeat(4, 100px);
- grid-template-rows: minmax(120px, auto) repeat(5, 100px);
- }
-
- .calculator-grid > button {
- cursor: pointer;
- font-size: 2rem;
- border: 1px solid white;
- outline: none;
- background-color: rgba(255, 255, 255, .75);
- }
-
- .calculator-grid > button:hover {
- background-color: rgba(255, 255, 255, .9);
- }
-
- .span-two {
- grid-column: span 2;
- }
-
- .output {
- grid-column: 1 / -1;
- background-color: rgba(0, 0, 0, .75);
- display: flex;
- align-items: flex-end;
- justify-content: space-around;
- flex-direction: column;
- padding: 10px;
- word-wrap: break-word;
- word-break: break-all;
- }
-
- .output .previous-operand {
- color: rgba(255, 255, 255, .75);
- font-size: 1.5rem;
- }
-
- .output .current-operand {
- color: white;
- font-size: 2.5rem;
- }
JavaScript Code
Now write the below JavaScript code in the head section of the <script> inside page.
Html 5 Code
Here, I have written all the HTML5 file code.
- <!DOCTYPE html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Calculator</title>
- <link href="styles.css" rel="stylesheet">
- <script src="script.js" defer></script>
- </head>
-
- <body>
- <h1 style="text-align:center; color: blue;">PRABAKARAN ACT</h1>
- <h1 style="text-align: center; color:white;"><b>Calculator</b></h1>
- <div class="calculator-grid">
- <div class="output">
- <div data-previous-operand class="previous-operand"></div>
- <div data-current-operand class="current-operand"></div>
- </div>
- <button data-all-clear class="span-two">AC</button>
- <button data-delete>DEL</button>
- <button data-operation>÷</button>
- <button data-number>1</button>
- <button data-number>2</button>
- <button data-number>3</button>
- <button data-operation>*</button>
- <button data-number>4</button>
- <button data-number>5</button>
- <button data-number>6</button>
- <button data-operation>+</button>
- <button data-number>7</button>
- <button data-number>8</button>
- <button data-number>9</button>
- <button data-operation>-</button>
- <button data-number>.</button>
- <button data-number>0</button>
- <button data-equals class="span-two">=</button>
- </div>
- </body>
-
- </html>
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.