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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
    <link
      href="https://fonts.googleapis.com/css2?family=Exo&display=swap"
      rel="stylesheet"
    />
  </head>
  <body>
    <div class="calculator">
      <div class="current-calculation"></div>
      <div class="result">0</div>
      <div class="clear-btn">AC</div>
      <div class="split-btn grey-color" data-value="/">/</div>
      <div class="times-btn grey-color" data-value="*">x</div>
      <div class="darkGrey-color" data-value="7">7</div>
      <div class="darkGrey-color" data-value="8">8</div>
      <div class="darkGrey-color" data-value="9">9</div>
      <div class="minus-btn grey-color" data-value="-">-</div>
      <div class="darkGrey-color" data-value="4">4</div>
      <div class="darkGrey-color" data-value="5">5</div>
      <div class="darkGrey-color" data-value="6">6</div>
      <div class="plus-btn grey-color" data-value="+">+</div>
      <div class="darkGrey-color" data-value="1">1</div>
      <div class="darkGrey-color" data-value="2">2</div>
      <div class="darkGrey-color" data-value="3">3</div>
      <div class="equal-btn">=</div>
      <div class="cero-btn darkGrey-color" data-value="0">0</div>
      <div class="dot-btn">.</div>
    </div>
    <div class="measuringDiv"></div>
    <script src="app.js"></script>
  </body>
</html>

              
            
!

CSS

              
                body {
  background-color: rgb(180, 180, 180);
  font-family: "Exo", sans-serif;
}

.calculator {
  margin: 20vh auto;
  width: 323px;
  display: grid;
  padding: 2px;
  background-color: black;
  color: aliceblue;
  grid-template-columns: 80px 80px 80px 80px;
  grid-template-rows: 20px 40px 65px 65px 65px 65px 65px;
  grid-gap: 1px;
  border-radius: 1%;
}

.grey-color:hover,
.dot-btn:hover,
.darkGrey-color:hover,
.clear-btn:hover,
.equal-btn:hover {
  border-width: 0.093rem;
  border-color: rgb(109, 108, 108);
  border-style: outset;
}

.grey-color {
  background-color: rgb(133, 133, 133);
  display: flex;
  justify-content: center;
  align-items: center;
}

.darkGrey-color,
.dot-btn {
  background-color: rgb(82, 82, 82);
  display: flex;
  justify-content: center;
  align-items: center;
}

.current-calculation {
  background-color: black;
  grid-column: 1 / 5;
  display: flex;
  justify-content: flex-end;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.result {
  font-size: 1.5rem;
  background-color: black;
  grid-column: 1 / 5;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.clear-btn {
  background-color: rgba(6, 94, 17, 0.842);
  grid-column: 1 / 3;
  display: flex;
  justify-content: center;
  align-items: center;
}

.cero-btn {
  grid-column: 1 / 3;
}

.equal-btn {
  background-color: rgb(199, 119, 0);
  grid-column: 4 / 5;
  grid-row: 6 / 8;
  display: flex;
  justify-content: center;
  align-items: center;
}

.measuringDiv {
  visibility: hidden;
  font-size: 1.5rem;
  position: absolute;
  height: auto;
  width: auto;
  white-space: nowrap;
}

              
            
!

JS

              
                const numbers = document.querySelectorAll(".darkGrey-color");
const equal = document.querySelector(".equal-btn");
const result = document.querySelector(".result");
const clearBtn = document.querySelector(".clear-btn");
const doneMath = document.querySelector(".current-calculation");
const dotBtn = document.querySelector(".dot-btn");
const operations = document.querySelectorAll(".grey-color");

/* hidden div created to measure the pixels the numbers are occupying and avoid overflowing
 on the calculator main display */
const calculatorWidth = document.querySelector(".measuringDiv");

const rangeText = "Limit reached";
let finalValue = 0;
let limit = false;
let decimal = false;
let operator;

clearBtn.addEventListener("click", function () {
  result.textContent = 0;
  doneMath.textContent = "";
  calculatorWidth.textContent = "";
  finalValue = 0;
  decimal = false;
  operator = "";
});

numbers.forEach((item) => {
  item.addEventListener("click", function () {
    //check that limit was not reached and the alert is not being shown
    if (limit) {
      return;
    }
    // check for the length of the num to display comparing it with the hidden div
    if (calculatorWidth.clientWidth > 300) {
      limitReached(result.textContent);
      return;
    }
    //number cant start with cero unless a decimal is being created
    if (result.textContent == 0 && !decimal) {
      result.textContent = "";
    }
    //numbers pressed added as strings, checks that a result is not being shown
    if (operator == "=") {
      finalValue = 0;
      result.textContent = item.dataset.value;
      doneMath.textContent = "";
      operator = "";
    } else {
      result.textContent += item.dataset.value;
    }
    //adds items to the div to check the width of the calculator display
    calculatorWidth.textContent += item.dataset.value;
  });
});


operations.forEach((item) => {
  item.addEventListener("click", () => {
    // save the inputted parsing it to a float considering decimals
    let value = parseFloat(result.textContent);
    decimal = false;
    // check that limit was not reached
    if (limit) {
      return;
    }
    //cant operate with just cero
    if (result.textContent == 0 && operator != "=") {
      return;
    }
    operating(value, item.dataset.value);
  });
});

// takes the value inputted and the operation selected to modify the global finalValue(the result) variable
const operating = (value, currentOperation) => {
  //finalValue == 0, first number input case
  if (!finalValue && !operator) {
    doneMath.textContent = result.textContent + currentOperation;
    finalValue = value;
    calculatorWidth.textContent = "";
    // not the first input, operate with the number saved and the
  } else if (operator != "=") {
    doneMath.textContent += result.textContent + currentOperation;
    operateLastInput(value);
    // a result of an operation is being shown and must operate with that value
  } else {
    finalValue = value;
    doneMath.textContent = finalValue + currentOperation;
  }
  result.textContent = 0;
  //global variable to keep track of the last operation selected if any
  operator = currentOperation;
  calculatorWidth.textContent = result.textContent;
};

// modifies finalValue (the global var holding the result) with the new input and operation selected
const operateLastInput = (value) => {
  if (operator == "+") {
    finalValue += value;
  } else if (operator == "/") {
    finalValue /= value;
  } else if (operator == "-") {
    finalValue -= value;
  } else if (operator == "*") {
    finalValue *= value;
  }
};

dotBtn.addEventListener("click", () => {
  //check there is not a decimal already nor a result is being shown
  if (!decimal && operator != "=") {
    result.textContent = result.textContent + ".";
    decimal = true;
  } else {
    return;
  }
});

equal.addEventListener("click", () => {
  let value = parseFloat(result.textContent);
  // return if equal is pressed many times
  if ((finalValue == 0 && doneMath.textContent == "") || operator == "=") {
    return;
  }
  doneMath.textContent += result.textContent;
  // 0 in the result display
  if (!value) {
    result.textContent = finalValue;
    operator = "=";
    calculatorWidth.textContent = result.textContent;
    return;
  } 
  // process the last operation selected
  operateLastInput(value);
  //if result is integer, don't show decimals
  if (Number.isInteger(finalValue)) {
    result.textContent = finalValue;
  } else {
    result.textContent = finalValue.toFixed(4);
    decimal = true;
  }
  operator = "=";
  calculatorWidth.textContent = result.textContent;
  decimal = false;
});

// if calculator reaches digits limit on main display shows alert
const limitReached = (text) => {
  result.textContent = rangeText;
  limit = true;
  setTimeout(() => {
    result.textContent = text;
    limit = false;
  }, 500);
};

              
            
!
999px

Console