Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>JS | Calculator</title>
    <link rel="stylesheet" href="css/style.css" />
  </head>
  <body>
    <main id="calculator">
      <div class="main-container">
        <div class="display"></div>
        <button class="clear">AC</button>
        <button class="delete">DELETE</button>
        <button class="sign">+/-</button>
        <button id="divide" class="operator">÷</button>
        <button class="number">7</button>
        <button class="number">8</button>
        <button class="number">9</button>
        <button id="multiply" class="operator">&times;</button>
        <button class="number">4</button>
        <button class="number">5</button>
        <button class="number">6</button>
        <button id="subtract" class="operator">-</button>
        <button class="number">1</button>
        <button class="number">2</button>
        <button class="number">3</button>
        <button id="add" class="operator">+</button>
        <button id="zero" class="number">0</button>
        <button class="decimal">.</button>
        <button id="calculate" class="equal">=</button>
      </div>
    </main>
    <script src="src/index.js"></script>
  </body>
</html>

              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Lato&display=swap");
body {
  background-color: #f9c0c0;
}

#calculator {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  align-content: center;
  background-color: #f3f2da;
}

.main-container {
  background-color: #f9c0c0;
  display: grid;
  grid-template-columns: repeat(4, 125px);
  grid-template-rows: repeat(6, 80px);
  border: 10px solid black;
  border-radius: 6px;
}

.display {
  background-color: #9ab3f5;
  font-family: "Lato", sans-serif;
  display: flex;
  font-size: 3em;
  color: white;
  grid-column: 1 / 5;
  justify-content: flex-end;
  border-radius: 4px;
  padding-right: 8px;
  margin: 4px;
}

#zero {
  grid-column: 1 / 3;
}

button {
  font-family: "Lato", sans-serif;
  font-size: 2.5em;
  font-weight: bolder;
  color: black;
  margin: 4px;
  border-radius: 4px;
}

.operator {
  background-color: #b9fffc;
}

.number,
.decimal,
.equal {
  background-color: #ccf6c8;
}

.clear,
.delete,
.sign,
.percentage {
  background-color: #fafcc2;
}

.delete {
  font-size: 1.4em;
}

              
            
!

JS

              
                function add(x, y) {
  return parseFloat(x) + parseFloat(y);
}

function subtract(x, y) {
  return parseFloat(x) - parseFloat(y);
}

function divide(x, y) {
  return parseFloat(x) / parseFloat(y);
}

function multiply(x, y) {
  return parseFloat(x) * parseFloat(y);
}

function operate(x, y, operator) {
  if (operator === "add") {
    return add(x, y);
  } else if (operator === "subtract") {
    return subtract(x, y);
  } else if (operator === "divide") {
    return divide(x, y).toFixed(5);
  } else if (operator === "multiply") {
    return multiply(x, y);
  } else {
    return null;
  }
}

const numbers = document.querySelectorAll(".number");
const operators = document.querySelectorAll(".operator");
const display = document.querySelector(".display");
const calculate = document.getElementById("calculate");
const clear = document.querySelector(".clear");
const backSpace = document.querySelector(".delete");
const decimal = document.querySelector(".decimal");
const sign = document.querySelector(".sign");
const digits = display.innerText.length;

let currentOperator = null;
let firstNum = null;
let secondNum = null;
let resetScreen = false;
let result = null;
// let count = 0;


numbers.forEach((number) => {
  number.addEventListener("click", (e) => {
    if (resetScreen) {
      clearScreen();
    }
    displayNumber(e.target.innerText);
    resetScreen = false;
  });
});

operators.forEach((operator) => {
  operator.addEventListener("click", (e) => {
    setOperand(showNumber());
    setTheOperator(e.target.id);
    resetScreen = true;
  });
});

calculate.addEventListener("click", () => {
  result = calculateResult();
  clearScreen();
  if (result) {
    displayNumber(result);
  }
});

clear.addEventListener("click", () => {
  clearAllValues();
});

backSpace.addEventListener("click", deleteNumber);
decimal.addEventListener("click", displayDecimal);
sign.addEventListener("click", displaySign);

function displayNumber(number) {
  display.innerText += number;
}

function showNumber() {
  return display.innerText;
}

function setTheOperator(operator) {
  if (currentOperator == null) {
    currentOperator = operator;
  } else if (firstNum && secondNum) {
    result = operate(Number(firstNum), Number(secondNum), currentOperator);
    clearScreen();
    displayNumber(result);
    firstNum = result;
    secondNum = null;
    currentOperator = operator;
  }
}

function setOperand(value) {
  if (firstNum == null) {
    firstNum = value;
  } else {
    secondNum = value;
  }
}

function clearScreen() {
  display.innerText = "";
}

function clearAllValues() {
  firstNum = null;
  secondNum = null;
  currentOperator = null;
  clearScreen();
}

function calculateResult() {
  if (firstNum && currentOperator && !resetScreen && !secondNum) {
    setOperand(showNumber());
    return operate(Number(firstNum), Number(secondNum), currentOperator);
  } else {
    return false;
  }
}

function deleteNumber() {
  if (display.innerText !== "0") {
    display.innerText = display.innerText.toString().slice(0, -1);
  }
  if (display.innerText === "") {
    display.innerText = "";
  }
}

function displayDecimal() {
  if (!display.innerText.includes(".")) {
    display.innerText += ".";
  }
}

function displaySign() {
  display.innerText = -display.innerText;
}

window.addEventListener("keydown", setKey);

function setKey(e) {
  if (e.key >= 0 && e.key <= 9) displayNumber(e.key);
  if (e.key === ".") displayDecimal(e.key);
  if (e.key === "Backspace") deleteNumber(e.key);
  if (e.key === "Delete") clearScreen(e.key);
}

              
            
!
999px

Console