Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

html webm calculator

  1. User

    html webm calculator

  2. Assistant

    Creating a simple web-based calculator using HTML and JavaScript (without WebM specifically, as that pertains to video formats) involves writing HTML for the user interface, along with JavaScript to handle the logic. Below is a straightforward example of a basic calculator:

    HTML and JavaScript Code

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Simple Calculator</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
                background-color: #f4f4f4;
            }
            #calculator {
                background: white;
                padding: 20px;
                border-radius: 5px;
                box-shadow: 0 0 10px rgba(0,0,0,0.1);
            }
            input[type="text"],
            button {
                width: 50px;
                height: 50px;
                font-size: 18px;
                margin: 5px;
            }
        </style>
    </head>
    <body>
    
    <div id="calculator">
        <input type="text" id="result" disabled>
        <br>
        <button onclick="clearResult()">C</button>
        <button onclick="appendToResult('/')">/</button>
        <button onclick="appendToResult('*')">*</button>
        <button onclick="appendToResult('-')">-</button>
        <br>
        <button onclick="appendToResult('7')">7</button>
        <button onclick="appendToResult('8')">8</button>
        <button onclick="appendToResult('9')">9</button>
        <button onclick="appendToResult('+')">+</button>
        <br>
        <button onclick="appendToResult('4')">4</button>
        <button onclick="appendToResult('5')">5</button>
        <button onclick="appendToResult('6')">6</button>
        <button onclick="calculateResult()">=</button>
        <br>
        <button onclick="appendToResult('1')">1</button>
        <button onclick="appendToResult('2')">2</button>
        <button onclick="appendToResult('3')">3</button>
        <br>
        <button onclick="appendToResult('0')">0</button>
    </div>
    
    <script>
        function appendToResult(value) {
            document.getElementById('result').value += value;
        }
    
        function clearResult() {
            document.getElementById('result').value = '';
        }
    
        function calculateResult() {
            const resultField = document.getElementById('result');
            try {
                resultField.value = eval(resultField.value);
            } catch (error) {
                resultField.value = 'Error';
            }
        }
    </script>
    
    </body>
    </html>
    

    Explanation

    1. HTML Structure: The calculator consists of an input field to display results and buttons for numbers and operations.
    2. CSS Styles: Basic styles are applied for easy visualization. Flexbox centers the calculator on the page.
    3. JavaScript Functions:
      • appendToResult(value): Appends the clicked button's value (number or operator) to the input field.
      • clearResult(): Clears the input field.
      • calculateResult(): Evaluates the expression in the input field when the "=" button is pressed. It uses eval(), which allows calculating the string as a mathematical expression. (Note: Use eval with caution in production code for security reasons.)

    How to Use

    1. Copy the code above.
    2. Paste it into an HTML file (for example, calculator.html).
    3. Open the file in a web browser to use your new calculator.

    Make sure to handle the use of eval with care to prevent any potential security issues in a production environment. For educational purposes and simple projects, it's useful for demonstration.