<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bigyan Karki</title>
    <description>The latest articles on DEV Community by Bigyan Karki (@bigyan_karki).</description>
    <link>https://dev.to/bigyan_karki</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2690116%2Fd941c2d3-951b-4953-9aa7-4ca57356a47a.jpg</url>
      <title>DEV Community: Bigyan Karki</title>
      <link>https://dev.to/bigyan_karki</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bigyan_karki"/>
    <language>en</language>
    <item>
      <title>Creating a Basic Calculator Using HTML, CSS and JavaScript</title>
      <dc:creator>Bigyan Karki</dc:creator>
      <pubDate>Sat, 11 Jan 2025 12:38:14 +0000</pubDate>
      <link>https://dev.to/bigyan_karki/creating-a-basic-calculator-using-html-css-and-javascript-33j8</link>
      <guid>https://dev.to/bigyan_karki/creating-a-basic-calculator-using-html-css-and-javascript-33j8</guid>
      <description>&lt;p&gt;Creating a functional calculator in JavaScript is fun and there are a lots of concept used such as DOM Manipulation, Event Handling, Conditional Logic, String Manipulation, Arithmetic Operations, Keyboard Input Integration and CSS Styling for user interface. In this blog post, we'll dive deep into the code, breaking down each line to understand its properties and functionality. By the end of this blog, we will have a solid grasp of how the calculator works.&lt;/p&gt;

&lt;p&gt;Lets get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up the HTML Structure
&lt;/h2&gt;

&lt;p&gt;The HTML is straight forward, starting with a standard boilerplate. below is the code snippet for index.html&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffov0zlemx8b7ye1oy9pj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffov0zlemx8b7ye1oy9pj.png" alt="Image description" width="800" height="1140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;div class="wrapper"&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;div class="calculator-container"&amp;gt;&lt;/code&gt; are used to structure the layout of the calculator, which includes both the buttons and the display box. The &lt;code&gt;&amp;lt;div class="display-box"&amp;gt;&lt;/code&gt;shows the result of the calculation, starting with 0.0. The calculator buttons are organized into different &lt;/p&gt; sections, each containing a set of buttons. These sections include digits (0-9), mathematical operators (+, -, *, /), and other essential buttons such as AC, C, %, =, and ..

&lt;p&gt;The &lt;code&gt;&amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt; tag links to an external JavaScript file, script.js, where the logic and functionality of the calculator are defined. This external file handles the user interactions and calculations, enabling the calculator to perform operations as intended.&lt;/p&gt;

&lt;p&gt;Here, The display-box shows the input and displays the result while the button-box holds all the calculator buttons.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding CSS for Styling
&lt;/h2&gt;

&lt;p&gt;Now, let's style our calculator to make it visually appealing and user-friendly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbi71wnoax149zwmbsm6n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbi71wnoax149zwmbsm6n.png" alt="Image description" width="558" height="1148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets breakdown the css code.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Wrapper Styling&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F92w43ktj2ldsbxztyeuu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F92w43ktj2ldsbxztyeuu.png" alt="Image description" width="770" height="558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;min-height: 100vh;&lt;/code&gt; ensures the wrapper occupies at least the full height of the viewport. The &lt;code&gt;display: flex;&lt;/code&gt; enables a flexible layout, allowing alignment of its children. The &lt;code&gt;justify-content: center;&lt;/code&gt; centers the content horizontally, while &lt;code&gt;align-items: center;&lt;/code&gt; centers the content vertically. Lastly, the &lt;code&gt;border: 2px solid black;&lt;/code&gt; adds a border around the wrapper.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Calculator Container Styling:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe4ad7hr04uzfrks1kral.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe4ad7hr04uzfrks1kral.png" alt="Image description" width="754" height="558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;display: flex;&lt;/code&gt; makes the calculator container flexible, allowing child elements to be laid out in a row or column. The &lt;code&gt;flex-direction: column;&lt;/code&gt; arranges the child elements vertically. The &lt;code&gt;gap: 12px;&lt;/code&gt; adds spacing between each section or element. Finally, &lt;code&gt;width: 500px;&lt;/code&gt; sets the width of the calculator to 500px.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Display Box Styling&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjpct8oe4q5zaxwg1fri5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjpct8oe4q5zaxwg1fri5.png" alt="Image description" width="770" height="558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;border: 1px solid black;&lt;/code&gt; adds a border to the display box. The &lt;code&gt;padding: 16px;&lt;/code&gt; provides space inside the box for better readability. The &lt;code&gt;text-align: right;&lt;/code&gt; ensures the text is aligned to the right. The &lt;code&gt;font-size: 24px;&lt;/code&gt; increases the font size for improved visibility, while &lt;code&gt;border-radius: 5px;&lt;/code&gt; rounds the corners of the box.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Buttons Container and Buttons Styling&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1hwymkldlusfa1t97xkt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1hwymkldlusfa1t97xkt.png" alt="Image description" width="800" height="841"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the buttons container is styled with .flex-container, where &lt;code&gt;display: flex;&lt;/code&gt; creates a flexible layout for its child elements. The &lt;code&gt;justify-content: space-between;&lt;/code&gt; property evenly distributes the buttons with space between them, while &lt;code&gt;gap: 8px;&lt;/code&gt; ensures proper spacing between each button for better alignment.&lt;/p&gt;

&lt;p&gt;Each button is styled with &lt;code&gt;flex: 1;&lt;/code&gt;, which makes them take up equal space within a row. &lt;code&gt;Padding: 16px;&lt;/code&gt; adds space inside each button for comfort, and &lt;code&gt;font-size: 20px;&lt;/code&gt; ensures the text is readable. The &lt;code&gt;font-weight: bold;&lt;/code&gt; makes the text stand out, while&lt;code&gt;border: 1px solid black;&lt;/code&gt; adds a border around each button. Additionally, &lt;code&gt;border-radius: 8px;&lt;/code&gt; slightly rounds the corners of the buttons, and &lt;code&gt;cursor: pointer;&lt;/code&gt; changes the cursor to a pointer when hovered over. The background color of the buttons is set to white with &lt;code&gt;background-color: rgb(255, 255, 255);&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For the "=" button, the .equal class uses &lt;code&gt;flex: 2.5;&lt;/code&gt; to give it more space, making it 2.5 times the width of the other buttons. When the button is hovered over, the &lt;code&gt;button:hover&lt;/code&gt; style changes the background color to grey, &lt;code&gt;background color:rgb(127, 131, 131);&lt;/code&gt; and the text color to white. This transition effect is smoothed by &lt;code&gt;transition: background-color 0.3s ease, color 0.3s ease;&lt;/code&gt;, which allows for a 0.3-second fade between the colors.&lt;/p&gt;

&lt;p&gt;With above HTML and CSS, our Calculator looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8t2k5sd79dx65m69p6rt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8t2k5sd79dx65m69p6rt.png" alt="Image description" width="800" height="713"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now lets dive into the main part, giving life to our calculator.&lt;br&gt;
JavaScript Code Snippet&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhrydq4dysw91qb3mimg5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhrydq4dysw91qb3mimg5.png" alt="Image description" width="415" height="1193"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets break down the code for better understanding.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Selecting DOM Elements&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc2tzja4sz41p8e9yzf00.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc2tzja4sz41p8e9yzf00.png" alt="Image description" width="800" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;displayBox&lt;/code&gt; variable holds a reference to the display box (&lt;code&gt;&amp;lt;div class="display-box"&lt;/code&gt;&amp;gt;), which is used to show calculation results or user inputs. Meanwhile, the &lt;code&gt;calculatorBtns&lt;/code&gt; variable stores references to all the buttons on the calculator, allowing event listeners to be attached to each button for interaction.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Variables for Display and Operators&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9yysg4mif5hyo0v4kw3a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9yysg4mif5hyo0v4kw3a.png" alt="Image description" width="800" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;displayValue&lt;/code&gt; variable holds the current value to be shown on the screen, ensuring accurate updates during calculations. The &lt;code&gt;lastOperator&lt;/code&gt; variable keeps track of the last operator used, preventing errors such as consecutive operator inputs. Additionally, &lt;code&gt;console.log&lt;/code&gt; is utilized for debugging purposes, specifically to log the calculatorBtns node list for review.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Button Click Event Listener/Looping Over Each Buttons&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw7iz93slhv6bjuvai8aj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw7iz93slhv6bjuvai8aj.png" alt="Image description" width="800" height="526"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;forEach&lt;/code&gt; method is used to loop through each button in the &lt;code&gt;calculatorBtns&lt;/code&gt; collection. For each button, the &lt;code&gt;innerText&lt;/code&gt; property is assigned to the &lt;code&gt;buttonValue&lt;/code&gt; variable, which holds the text displayed on the button, such as "AC", "C", "9", "+", etc.&lt;/p&gt;

&lt;p&gt;An &lt;code&gt;onclick&lt;/code&gt; event listener is then added to each button. When a button is clicked, the assigned function is executed. This function, &lt;code&gt;handleButtonAction(buttonValue)&lt;/code&gt;, takes the button's text (&lt;code&gt;buttonValue&lt;/code&gt;) as an argument. By passing the button's value, the function allows the calculator to perform the correct action, such as clearing the display, inputting a number, or performing a mathematical operation.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Handling Key-press for Keyboard Input&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs4bdz5qbcbvxzzho4b4t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs4bdz5qbcbvxzzho4b4t.png" alt="Image description" width="800" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This allows the calculator to also work with the keyboard. When a key is pressed, the corresponding button's action is triggered. For example, pressing "1" on the keyboard will trigger the handleButtonAction() function with the value "1".&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Display Function&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Focw2oqcuevvwfssswx7j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Focw2oqcuevvwfssswx7j.png" alt="Image description" width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;display()&lt;/code&gt; function updates the content of the display box &lt;code&gt;(displayBox)&lt;/code&gt; with the current &lt;code&gt;displayValue&lt;/code&gt;. If &lt;code&gt;displayValue&lt;/code&gt; is empty, it shows "0.0" by default.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Button Action Handler(Main Logic):&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdzk3vc22lhxwu7i5r8iv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdzk3vc22lhxwu7i5r8iv.png" alt="Image description" width="800" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code performs several steps to update the calculator's display and handle the calculation. First, &lt;code&gt;eval(displayValue)&lt;/code&gt; evaluates the mathematical expression stored in the displayValue. For example, if the display shows "3+5", &lt;code&gt;eval&lt;/code&gt; calculates and returns the result, which in this case would be 8.&lt;/p&gt;

&lt;p&gt;Next, &lt;code&gt;displayValue = String(result)&lt;/code&gt; converts the result into a string and updates the &lt;code&gt;displayValue&lt;/code&gt; to show the result on the screen. Once the calculation is complete, &lt;code&gt;lastOperator = ""&lt;/code&gt; resets the &lt;code&gt;lastOperator&lt;/code&gt; to an empty string, ensuring that any previous operator is cleared. Finally, the &lt;code&gt;display()&lt;/code&gt; function updates the display to show the result of the calculation.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;AC(All Clear) and C (Clear) Button Logic&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1812gfxmk9mlinpfmr1x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1812gfxmk9mlinpfmr1x.png" alt="Image description" width="800" height="523"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the "AC" button is clicked, the code checks if &lt;code&gt;buttonValue&lt;/code&gt; is equal to "AC". If true, it resets the &lt;code&gt;displayValue&lt;/code&gt; to an empty string, effectively clearing the entire display and resetting the calculator. The &lt;code&gt;display()&lt;/code&gt; function is then called to update the display with the empty value.&lt;/p&gt;

&lt;p&gt;For the "C" button, if &lt;code&gt;buttonValue&lt;/code&gt; is "C", the code removes the last character from &lt;code&gt;displayValue&lt;/code&gt; using &lt;code&gt;slice(0, -1)&lt;/code&gt;. This allows the user to delete the last input or character, and the &lt;code&gt;display()&lt;/code&gt; function is called again to update the display accordingly.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Validating Operators&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv1ouf9qcoklrnfhtow0i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv1ouf9qcoklrnfhtow0i.png" alt="Image description" width="800" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This condition is used to validate if an operator can be pressed based on the current value displayed.&lt;/p&gt;

&lt;p&gt;The condition &lt;code&gt;["%", "/", "*", "+"].includes(buttonValue)&lt;/code&gt; checks if the button clicked is one of the operators &lt;code&gt;(%, /, *, +)&lt;/code&gt;. If the button is an operator, the next check if &lt;code&gt;(!displayValue || displayValue === "-")&lt;/code&gt; ensures that the operator cannot be pressed if the display is empty or only contains a minus sign &lt;code&gt;(-)&lt;/code&gt;. This prevents errors such as having two consecutive operators or starting with an operator. If the condition is true, the function simply returns and no operator is added to the display.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Prevent Consecutive Operators&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0nfso77umpe29z18gf5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0nfso77umpe29z18gf5.png" alt="Image description" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This block of code handles the scenario where consecutive operators are pressed, preventing invalid input such as "++" or "+-."&lt;/p&gt;

&lt;p&gt;First, &lt;code&gt;if (["%", "/", "*", "+", "-"].includes(buttonValue))&lt;/code&gt; checks if the button clicked is an operator. Then, &lt;code&gt;const lastCharacter = displayValue.slice(-1)&lt;/code&gt; retrieves the last character of the current expression in &lt;code&gt;displayValue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, the &lt;code&gt;lastOperator = buttonValue&lt;/code&gt; updates the &lt;code&gt;lastOperator&lt;/code&gt; variable to store the current operator. If the last character is also an operator, as checked by &lt;code&gt;if (["%", "/", "*", "+", "-"].includes(lastCharacter))&lt;/code&gt;, the code removes it using &lt;code&gt;displayValue.slice(0, -1)&lt;/code&gt;. This ensures that only one operator appears at the end of the expression and prevents consecutive operators from being added.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Validating Decimal Points&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdom98ehvchvm6s8vi2fr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdom98ehvchvm6s8vi2fr.png" alt="Image description" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This block of code ensures that a decimal point (.) can only appear once within a number, preventing invalid inputs like "3..5."&lt;/p&gt;

&lt;p&gt;First, the condition i&lt;code&gt;f (buttonValue === ".")&lt;/code&gt; checks if the button clicked is a decimal point. If so, it proceeds with the validation.&lt;/p&gt;

&lt;p&gt;Next, &lt;code&gt;const lastOperatorIndex = displayValue.lastIndexOf(lastOperator)&lt;/code&gt; finds the position of the last operator in the &lt;code&gt;displayValue&lt;/code&gt;. Then, &lt;code&gt;const currentNumberSet = displayValue.slice(lastOperatorIndex) || displayValue&lt;/code&gt; extracts the portion of &lt;code&gt;displayValue&lt;/code&gt; after the last operator, which represents the current number being entered. If there is no operator, the entire &lt;code&gt;displayValue&lt;/code&gt; is considered.&lt;/p&gt;

&lt;p&gt;Finally, &lt;code&gt;if (currentNumberSet.includes("."))&lt;/code&gt; checks if the extracted number portion already contains a decimal point. If it does, the function returns early, preventing the user from entering a second decimal point. This ensures that numbers like "3.5" are valid, but inputs like "3..5" are not.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Update the Display with New Value:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn20tmbt7ue0qr6ob3o0r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn20tmbt7ue0qr6ob3o0r.png" alt="Image description" width="800" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code &lt;code&gt;displayValue = displayValue + buttonValue;&lt;/code&gt; appends the value of the pressed button (such as a number or operator) to the existing &lt;code&gt;displayValue&lt;/code&gt; string. This builds the current expression or number as the user interacts with the calculator.&lt;/p&gt;

&lt;p&gt;After appending the button value, the &lt;code&gt;display()&lt;/code&gt; function is called to update the display, ensuring it reflects the updated &lt;code&gt;displayValue&lt;/code&gt;. This ensures that the user sees the most current value or expression as they enter it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This JavaScript code handles the logic for displaying values, performing calculations, clearing inputs, and validating expressions in a calculator. It works with both button clicks and keyboard input. The key features include performing calculations when the "=" or "Enter" keys are pressed, handling the AC (all-clear) and C (clear last character) buttons, and preventing invalid operations such as consecutive operators or multiple decimal points. Additionally, the display is updated after each action, ensuring that the user sees the most current value or expression. Together, these features provide the foundation for a functional and interactive calculator.&lt;/p&gt;

&lt;p&gt;Below are my demo links so feel free to check out the full code, clone the repository, or interact with the live demo. Happy coding! &lt;br&gt;
GITHUB - [&lt;a href="https://github.com/bigyan1997/calculator" rel="noopener noreferrer"&gt;https://github.com/bigyan1997/calculator&lt;/a&gt;]&lt;br&gt;
VERCEL - [&lt;a href="https://calculator-delta-sepia-91.vercel.app/" rel="noopener noreferrer"&gt;https://calculator-delta-sepia-91.vercel.app/&lt;/a&gt;]&lt;/p&gt;

</description>
      <category>calculator</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>project</category>
    </item>
  </channel>
</rss>
