Creating a Unique Feature-Rich HTML Calculator

Answered using OpenAI o1 preview by OpenAI o1

Question:

Create me a calculator in HTML and make it unique and make it the best calculator version available with all features

Answer:

Creating a unique and feature-rich calculator in HTML, CSS, and JavaScript can be a fun project! Below is a simple yet effective calculator that includes basic arithmetic operations, a clear button, and a memory function. You can expand upon this foundation to add more features as needed.

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Unique Calculator</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="calculator">
        <input type="text" id="display" disabled>
        <div class="buttons">
            <button onclick="clearDisplay()">C</button>
            <button onclick="appendToDisplay('7')">7</button>
            <button onclick="appendToDisplay('8')">8</button>
            <button onclick="appendToDisplay('9')">9</button>
            <button onclick="appendToDisplay('/')">/</button>
            <button onclick="appendToDisplay('4')">4</button>
            <button onclick="appendToDisplay('5')">5</button>
            <button onclick="appendToDisplay('6')">6</button>
            <button onclick="appendToDisplay('*')">*</button>
            <button onclick="appendToDisplay('1')">1</button>
            <button onclick="appendToDisplay('2')">2</button>
            <button onclick="appendToDisplay('3')">3</button>
            <button onclick="appendToDisplay('-')">-</button>
            <button onclick="appendToDisplay('0')">0</button>
            <button onclick="calculateResult()">=</button>
            <button onclick="appendToDisplay('+')">+</button>
            <button onclick="memoryStore()">M+</button>
            <button onclick="memoryRecall()">MR</button>
            <button onclick="memoryClear()">MC</button>
        </div>
    </div>
    <script src="script.js"></script>
</body>
</html>

CSS Code (styles.css)

body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f0f0f0;
    font-family: Arial, sans-serif;
}

.calculator {
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
    padding: 20px;
    width: 300px;
}

#display {
    width: 100%;
    height: 50px;
    font-size: 24px;
    text-align: right;
    border: 1px solid #ccc;
    border-radius: 5px;
    margin-bottom: 10px;
    padding: 10px;
}

.buttons {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
}

button {
    padding: 20px;
    font-size: 18px;
    border: none;
    border-radius: 5px;
    background-color: #007bff;
    color: white;
    cursor: pointer;
    transition: background-color 0.3s;
}

button:hover {
    background-color: #0056b3;
}

JavaScript Code (script.js)

let memory = 0;

function appendToDisplay(value) {
    document.getElementById('display').value += value;
}

function clearDisplay() {
    document.getElementById('display').value = '';
}

function calculateResult() {
    const display = document.getElementById('display');
    try {
        display.value = eval(display.value);
    } catch (error) {
        display.value = 'Error';
    }
}

function memoryStore() {
    memory = parseFloat(document.getElementById('display').value);
}

function memoryRecall() {
    document.getElementById('display').value = memory;
}

function memoryClear() {
    memory = 0;
}

Features of the Calculator

  1. Basic Operations: Addition, subtraction, multiplication, and division.
  2. Memory Functions: Store, recall, and clear memory.
  3. Clear Button: Clear the display easily.
  4. Responsive Design: Works well on different screen sizes.

How to Use

  1. Create three files: index.html, styles.css, and script.js.
  2. Copy the respective code into each file.
  3. Open index.html in a web browser to see your calculator in action.

Future Enhancements

You can enhance this calculator by adding:

  • Scientific functions (sin, cos, tan, etc.)
  • A history feature to keep track of previous calculations.
  • A theme switcher for light/dark mode.
  • Keyboard support for input.

Feel free to modify and expand upon this code to make it your own!