Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- This project too me liek 2 days to complete, but I'm glad that I was able to figure it out--> 
<!-- I ran into some issues wiht parts of it, and I really struggled wiht the 15th test cases --> 
<!-- Glad to be done with this however --> 
<!-- Enjoy --> 
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Arav Arora's JavaScript Calculator</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="title">
        Arav Arora's JS Calculator
        <a href="https://www.linkedin.com/in/arav-arora-5b5b1228b/">
            <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS0bGEl9v47XieEtHyj0TqTr1tOXJmib-KHtw&s">
        </a>
        <a href="https://github.com/AravArora05">
            <img src="https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png">
        </a>
    </div>
    <div class="calculator">
        <div class="input-display" id="inputDisplay">0</div>
        <div id="display" class="display">0</div>
        <button id="clear">Clear</button>
        <button id="delete">DEL</button>
        <button id="divide">/</button>
        <button id="multiply">*</button>
        <button id="seven">7</button>
        <button id="eight">8</button>
        <button id="nine">9</button>
        <button id="subtract">-</button>
        <button id="four">4</button>
        <button id="five">5</button>
        <button id="six">6</button>
        <button id="add">+</button>
        <button id="one">1</button>
        <button id="two">2</button>
        <button id="three">3</button>
        <button id="zero">0</button>
        <button id="decimal">.</button>
        <button id="negative">+/-</button>
        <button id="equals">=</button>
    </div>
    <script src="script.js"></script>
</body>
</html>

              
            
!

CSS

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

.title {
    text-align: center;
    margin-bottom: 20px;
    font-size: 2em;
    color: #00274c;
}

.title img {
    width: 40px;
    height: 40px;
    margin: 0 10px;
    vertical-align: middle;
}

.calculator {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
    max-width: 400px;
    width: 100%;
    padding: 20px;
    background: #fff;
    border-radius: 5px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.input-display, .display {
    grid-column: span 4;
    background: #00274c;
    color: #ffcb05;
    text-align: right;
    padding: 10px;
    font-size: 1.5em;
    border-radius: 5px;
}

button {
    padding: 20px;
    font-size: 1.2em;
    border: none;
    border-radius: 5px;
    background: #ffcb05;
    color: #00274c;
    cursor: pointer;
}

button:hover {
    background: #ffb700;
}

button#clear:hover {
    background: #d62828;
}

button#equals {
    background: #457b9d;
    color: #fff;
}

              
            
!

JS

              
                
document.getElementById('clear').addEventListener('click', clearDisplay);
document.getElementById('delete').addEventListener('click', deleteLast);
document.getElementById('equals').addEventListener('click', calculateResult);


document.getElementById('decimal').addEventListener('click', addDecimal);
document.getElementById('negative').addEventListener('click', toggleNegative);
document.getElementById('divide').addEventListener('click', () => addOperator('/'));
document.getElementById('multiply').addEventListener('click', () => addOperator('*'));
document.getElementById('subtract').addEventListener('click', () => addOperator('-'));
document.getElementById('add').addEventListener('click', () => addOperator('+'));


const display = 
      document.getElementById('display');
const inputDisplay = 
      document.getElementById('inputDisplay');


let currentInput = '0';
let formula = '0';



function updateDisplay() {
  
    display.textContent = currentInput;
    inputDisplay.textContent = formula;
}

function addNumber(value) {
    if (currentInput === '0') {
        currentInput = value;
        formula = value;
      
    } else {
      
        currentInput += value;
        formula += value;
      
    }
    updateDisplay();
}

function addOperator(operator) {
    const operators = ['+', '-', '*', '/'];
    const beforeLastChar = formula.charAt(formula.length - 2);
    const lastChar = formula.charAt(formula.length - 1);

    if (formula.length) {
        const lastCharIsOperator = operators.includes(lastChar);
        const beforeLastCharIsOperator = operators.includes(beforeLastChar);

      
      
        if ((lastCharIsOperator && operator !== '-') || (beforeLastCharIsOperator && lastCharIsOperator)) {
            const indexToSlice = beforeLastCharIsOperator ? -2 : -1;
            const updatedFormula = formula.slice(0, formula.length + indexToSlice) + operator;
            currentInput = operator;
            formula = updatedFormula;
        } else {
            currentInput = operator;
            formula += operator;
        }
    }
  
    updateDisplay();
}

/**
This function was a struggle to write. Definetly had a lot of issues with this one. I thouoght the best way to fis this issue
*/
function addDecimal() {
    if (currentInput.indexOf('.') === -1) {
        currentInput += '.';
        formula += '.';
    }
    updateDisplay();
}

function calculateResult() {
    try {
        let result = eval(formula);
        result = parseFloat(result.toFixed(4));
        currentInput = result.toString();
        formula = result.toString();
    } catch (error) {
        currentInput = 'Error';
        formula = '0';
    }
    updateDisplay();
}

function clearDisplay() {
    currentInput = '0';
    formula = '0';
    updateDisplay();
}

function deleteLast() {
    const newFormula = formula.slice(0, -1);
    const updatedDisplay = newFormula ? newFormula : '0';
    currentInput = updatedDisplay;
    formula = newFormula || '0';
    updateDisplay();
}

function toggleNegative() {
    if (currentInput.startsWith('-')) {
        currentInput = currentInput.slice(1);
    } else {
        currentInput = '-' + currentInput;
    }
    formula = currentInput;
    updateDisplay();
}

['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'].forEach(id => {
    document.getElementById(id).addEventListener('click', () => addNumber(document.getElementById(id).textContent));
});

updateDisplay();
              
            
!
999px

Console