DEV Community

Cover image for I asked ChatGPT to build a calculator
M Fahad Iqbal
M Fahad Iqbal

Posted on

I asked ChatGPT to build a calculator

In this tutorial, we will be building a proper calculator using ChatGPT, a large language model developed by OpenAI. We will be using HTML, CSS, and JavaScript to create the calculator, and ChatGPT to help us write the code and ensure that everything is working properly.

Getting Started
First, we will create the basic layout of the calculator using HTML. We will create a div element with the class calculator to hold all of the calculator elements, and within that div we will create a div element with the class display to hold the result of the calculations.

Next, we will add the buttons for the calculator using HTML button elements. We will include buttons for the four basic math operations (addition, subtraction, multiplication, and division), as well as a clear button that resets the calculator and a display area to show the results of the calculations.

We will also use CSS to style the calculator and the buttons, including adding a hover effect for the buttons and styling the operator buttons differently.

Here is the HTML code for the basic layout of the calculator:

<div class="calculator">
  <div class="display">0</div>
  <button class="button" id="clear">C</button>
  <button class="button" id="divide">/</button>
  <button class="button" id="multiply">*</button>
  <button class="button" id="seven">7</button>
  <button class="button" id="eight">8</button>
  <button class="button" id="nine">9</button>
  <button class="button" id="subtract">-</button>
  <button class="button" id="four">4</button>
  <button class="button" id="five">5</button>
  <button class="button" id="six">6</button>
  <button class="button" id="add">+</button>
  <button class="button" id="one">1</button>
  <button class="button" id="two">2</button>
  <button class="button" id="three">3</button>
  <button class="button" id="equals">=</button>
  <button class="button" id="zero">0</button>
  <button class="button" id="decimal">.</button>
</div>
Enter fullscreen mode Exit fullscreen mode

Now that we have the basic layout of the calculator, we can use JavaScript to add functionality to the buttons.

Adding Functionality with JavaScript
We will start by using ChatGPT to help us write the JavaScript code to add click event listeners to all the buttons. When a button is clicked, we want to perform the appropriate action based on the value of the button. If the clear button is clicked, we want to reset the display to zero. If an operator button is clicked, we want to add the operator to the display. If the equals button is clicked, we want to evaluate the expression and display the result. If a number button is clicked, we want to add the number to the display.

Here is the JavaScript code to add the click event listeners and handle the button clicks:

// Get all the buttons
const buttons = document.querySelectorAll('.button');
// Get the display area
const display = document.querySelector('.display');

// Add click event listeners to all the buttons
buttons.forEach(button => {
  button.addEventListener('click', event => {
    const value = event.target.innerText;
    if (value === 'C') {
      // Clear the display if the clear button is clicked
      display.innerText = '';
    } else if (value === '+' || value === '-' || value === '*' || value === '/') {
      // If an operator is clicked, add it to the display
      display.innerText += value;
    } else if (value === '=') {
      // If the equals button is clicked, evaluate the expression and display the result
      display.innerText = eval(display.innerText);
    } else {
      // If a number is clicked, add it to the display
      display.innerText += value;
    }
  });
});
Enter fullscreen mode Exit fullscreen mode

With this JavaScript code, the calculator should now be fully functional and working properly.

Finishing Up
To complete the calculator, we will add some additional CSS styling to make it look nicer and more polished. We can add some colors, rounded corners, and other styles to give the calculator a more professional appearance.

Here is the CSS code for the calculator:

     /* Style the calculator */
      .calculator {
        width: 300px;
        margin: 0 auto;
        text-align: center;
        background-color: #f3f3f3;
        border: 1px solid #ccc;
        border-radius: 5px;
      }

      /* Style the buttons */
      .button {
        width: 50px;
        height: 50px;
        font-size: 18px;
        background-color: white;
        border: 1px solid #ccc;
        border-radius: 5px;
        margin: 5px;
        cursor: pointer;
      }

      /* Style the operator buttons differently */
      .button.operator {
        background-color: #f3c623;
        color: white;
      }

      /* Add a hover effect for the buttons */
      .button:hover {
        background-color: #f5f5f5;
      }

      /* Style the display area */
      .display {
        width: 100%;
        height: 50px;
        font-size: 18px;
        background-color: white;
        border: 1px solid #ccc;
        border-radius: 5px;
        text-align: right;
        padding: 10px;
        box-sizing: border-box;
      }
Enter fullscreen mode Exit fullscreen mode

In conclusion, ChatGPT is a powerful tool for building a proper calculator. Its ability to understand and write code makes it easy to create a functional calculator with just a few lines of code. With ChatGPT, we were able to build a calculator that can perform basic math operations, clear the display, and evaluate expressions, all while looking nice and polished thanks to some additional CSS styling. Overall, ChatGPT is a great tool for anyone looking to build a calculator or other types of applications.

Top comments (0)