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

Calculator category with html, css, JavaScript code besazm

  1. User

    Calculator category with html, css, JavaScript code besazm

  2. Assistant

    To create a calculator using HTML, CSS, and JavaScript, you can follow the code below:

    HTML:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
      <title>Calculator</title>
      <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
      <div class="calculator">
        <input type="text" id="result" disabled>
        
        <div class="buttons">
          <button onclick="clearResult()">C</button>
          <button onclick="deleteChar()"><</button>
          <button onclick="appendChar('7')">7</button>
          <button onclick="appendChar('8')">8</button>
          <button onclick="appendChar('9')">9</button>
          <button onclick="appendChar('+')">+</button>
          <button onclick="appendChar('4')">4</button>
          <button onclick="appendChar('5')">5</button>
          <button onclick="appendChar('6')">6</button>
          <button onclick="appendChar('-')">-</button>
          <button onclick="appendChar('1')">1</button>
          <button onclick="appendChar('2')">2</button>
          <button onclick="appendChar('3')">3</button>
          <button onclick="appendChar('*')">*</button>
          <button onclick="appendChar('0')">0</button>
          <button onclick="appendChar('.')">.</button>
          <button onclick="calculate()">=</button>
          <button onclick="appendChar('/')">/</button>
        </div>
      </div>
    
      <script src="script.js"></script>
    </body>
    </html>
    

    CSS (style.css):

    .calculator {
      width: 250px;
      margin: 100px auto;
    }
    
    input[type="text"] {
      width: 100%;
      font-size: 20px;
      padding: 10px;
      text-align: right;
      margin-bottom: 10px;
    }
    
    .buttons button {
      width: 50px;
      height: 50px;
      font-size: 20px;
      margin: 5px;
    }
    

    JavaScript (script.js):

    function clearResult() {
      document.getElementById("result").value = "";
    }
    
    function deleteChar() {
      var result = document.getElementById("result").value;
      document.getElementById("result").value = result.slice(0, -1);
    }
    
    function appendChar(char) {
      document.getElementById("result").value += char;
    }
    
    function calculate() {
      var result = document.getElementById("result").value;
      
      try {
        var calculatedResult = eval(result);
        document.getElementById("result").value = calculatedResult;
      } catch (error) {
        document.getElementById("result").value = "Error";
      }
    }
    

    Save the HTML code as index.html, the CSS code as style.css, and the JavaScript code as script.js. Then, open the index.html file in a web browser and you will have a basic calculator interface.

    Note: Be cautious when using the eval() function, as it can execute any JavaScript code and may pose security risks.

  3. User

    A site with php for me.

  4. Assistant

    Sure! Here's an example of a simple PHP website that displays the current date:

    index.php:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
      <title>PHP Website</title>
    </head>
    <body>
      <h1>Welcome to my PHP Website</h1>
      <p>Today's date is: <?php echo date("Y-m-d"); ?></p>
    </body>
    </html>
    

    Save the above code as index.php.

    To run this PHP website, you will need to set up a local development environment with PHP. You can use tools like XAMPP, WAMP, or MAMP depending on your operating system. Once your local environment is set up, move your index.php file to the appropriate folder (e.g., htdocs for XAMPP) and start the local server.

    Access the website by opening a web browser and navigating to http://localhost/index.php, and you will see the welcome message along with the current date displayed.

    Note: Make sure you have PHP installed and properly set up on your local environment.

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