Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

create a project with html, css and javascript, create a web calculator

  1. User

    create a project with html, css and javascript, create a web calculator

  2. Assistant

    Sure! Here's a simple example of a web calculator project using HTML, CSS, and JavaScript:

    HTML:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Web Calculator</title>
    </head>
    <body>
      <div class="calculator">
        <input type="text" id="result" readonly>
        <table>
          <tr>
            <td><button onclick="clearResult()">C</button></td>
            <td><button onclick="deleteLast()">DEL</button></td>
            <td><button onclick="appendValue('/')">/</button></td>
            <td><button onclick="appendValue('*')">x</button></td>
          </tr>
          <tr>
            <td><button onclick="appendValue('7')">7</button></td>
            <td><button onclick="appendValue('8')">8</button></td>
            <td><button onclick="appendValue('9')">9</button></td>
            <td><button onclick="appendValue('-')">-</button></td>
          </tr>
          <tr>
            <td><button onclick="appendValue('4')">4</button></td>
            <td><button onclick="appendValue('5')">5</button></td>
            <td><button onclick="appendValue('6')">6</button></td>
            <td><button onclick="appendValue('+')">+</button></td>
          </tr>
          <tr>
            <td><button onclick="appendValue('1')">1</button></td>
            <td><button onclick="appendValue('2')">2</button></td>
            <td><button onclick="appendValue('3')">3</button></td>
            <td rowspan="2"><button onclick="calculate()" class="equal">=</button></td>
          </tr>
          <tr>
            <td colspan="2"><button onclick="appendValue('0')">0</button></td>
            <td><button onclick="appendValue('.')">.</button></td>
          </tr>
        </table>
      </div>
      <script src="script.js"></script>
    </body>
    </html>
    

    CSS:

    body {
      font-family: Arial, sans-serif;
      background-color: #f1f1f1;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }
    
    .calculator {
      border: 1px solid #ccc;
      border-radius: 5px;
      padding: 10px;
      background-color: #fff;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
    }
    
    input[type="text"] {
      width: 100%;
      height: 40px;
      margin-bottom: 10px;
      padding: 5px;
      font-size: 20px;
      border: 1px solid #ccc;
      border-radius: 3px;
    }
    
    table {
      width: 100%;
      text-align: center;
    }
    
    button {
      width: 100%;
      height: 40px;
      font-size: 18px;
      border: none;
      background-color: #f1f1f1;
      cursor: pointer;
    }
    
    .equal {
      height: 80px;
    }
    

    JavaScript (script.js):

    let calculation = '';
    
    function appendValue(value) {
      calculation += value;
      document.getElementById('result').value = calculation;
    }
    
    function clearResult() {
      calculation = '';
      document.getElementById('result').value = '';
    }
    
    function deleteLast() {
      calculation = calculation.slice(0, -1);
      document.getElementById('result').value = calculation;
    }
    
    function calculate() {
      let result = eval(calculation);
      document.getElementById('result').value = result;
      calculation = '';
    }
    

    Save the HTML code in a file with an HTML extension (e.g., index.html) and the CSS code in a file with a CSS extension (e.g., style.css). The JavaScript code should be saved in a file called script.js in the same directory as the HTML file.

    When you open the HTML file in a web browser, you'll see a basic calculator interface with buttons to perform mathematical operations. The user can input calculations by clicking on the buttons, and the result will be displayed in the input field. The calculations are performed using JavaScript's eval() function.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.