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

              
                <div class="calculator">
      <div class="screen mb16">
        <div class="result">0</div>
      </div>
      <div class="buttons">
        <button data-digit="7" class="btn btn-primary blue">7</button>
        <button data-digit="8" class="btn btn-primary blue">8</button>
        <button data-digit="9" class="btn btn-primary blue">9</button>
        <button data-operation="+" class="btn btn-primary red">+</button>

        <button data-digit="4" class="btn btn-primary blue">4</button>
        <button data-digit="5" class="btn btn-primary blue">5</button>
        <button data-digit="6" class="btn btn-primary blue">6</button>
        <button data-operation="-" class="btn btn-primary red">-</button>

        <button data-digit="1" class="btn btn-primary blue">1</button>
        <button data-digit="2" class="btn btn-primary blue">2</button>
        <button data-digit="3" class="btn btn-primary blue">3</button>
        <button data-operation="*" class="btn btn-primary red">*</button>

        <button data-digit="0" class="btn btn-primary blue">0</button>
        <button data-clear class="btn btn-primary red-dark">C</button>
        <button data-equals class="btn btn-primary red">=</button>
        <button data-operation="/" class="btn btn-primary red">/</button>
      </div>
    </div>

    <!-- made in jssecrets.com -->
    <a class="jssecrets" href="https://jssecrets.com/" target="_blank"
      >jssecrets.com</a
    >
    <!-- made in jssecrets.com -->
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Urbanist:wght@500&display=swap");

*,
::before,
::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

ul {
  list-style: none;
}

.mb16 {
  margin-bottom: 16px;
}

@font-face {
  font-family: "Windows 95";
  src: url('https://ilyass.sg-host.com/w95fa.woff2') format('woff2');
  font-weight: normal;
  font-style: normal;
}
.red {
  color: #ff0000;
}

.blue {
  color: #0000ff;
}

.red-dark {
  color: #800000;
}

* {
  font-family: "Windows 95", sans-serif;
}

body {
  height: 100vh;
  background: #eaeaea;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.calculator {
  padding: 24px;
  background: #c0c0c0;
  border-width: 3px;
  border-style: solid;
  border-right-color: #606f6f;
  border-bottom-color: #606f6f;
  border-top-color: #f1f1f1;
  border-left-color: #f1f1f1;
  /* max-width: 427.5px; */
}
@media (max-width: 480px) {
  .calculator {
    padding: 12px;
    max-width: 240px;
  }
}
.calculator .screen .result {
  width: 100%;
  max-width: 100%;
  height: 68px;
  text-align: right;
  padding: 16px;
  font-size: 32px;
  font-weight: bold;
  color: #000;
  background-color: #fff;
  border-width: 3px;
  border-style: solid;
  border-right-color: #f1f1f1;
  border-bottom-color: #f1f1f1;
  border-top-color: #606f6f;
  border-left-color: #606f6f;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
@media (max-width: 480px) {
  .calculator .screen .result {
    font-size: 24px;
    height: 48px;
    line-height: 16px;
  }
}
.calculator .buttons {
  display: grid;
  gap: 8px;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
}
.calculator .buttons button {
  font-size: 32px;
  font-weight: bold;
}
.calculator .buttons button:hover {
  cursor: pointer;
}
@media (max-width: 480px) {
  .calculator .buttons button {
    font-size: 20px;
  }
}

.btn {
  position: relative;
  transition: all 0.2s ease;
  padding: 0.7rem 2.1rem;
}
@media (max-width: 480px) {
  .btn {
    padding: 0.35rem 1rem;
  }
}

.btn:before,
.btn:after {
  content: "";
  position: absolute;
  box-sizing: border-box;
  display: block;
  background: transparent;
  z-index: 9;
  top: 0;
  left: 0;
}

.btn:before {
  width: 100%;
  height: 100%;
  border-top: 2px solid #f1f1f1;
  border-left: 2px solid #f1f1f1;
  border-right: 2px solid #606f6f;
  border-bottom: 2px solid #606f6f;
}

.btn:not(:disabled):not(.disabled):active:before,
.btn:not(:disabled):not(.disabled):active:focus:before {
  border-color: transparent;
}

.btn-primary {
  background-color: #c0c0c0;
  border-color: #a0a0a0;
}

.btn-primary:hover {
  background-color: #c0c0c0;
  border-color: #a0a0a0;
}

.btn-primary:focus,
.btn-primary.focus {
  box-shadow: none;
}

.jssecrets {
  font-weight: 500;
  text-decoration: none !important;
  font-family: "Urbanist";
  padding: 16px;
  background: #ffc805;
  color: #000000;
  font-size: 1.2em;
  letter-spacing: 0.2px;
  border-radius: 8px;
  box-shadow: 0px 4px 12px rgba(184, 144, 0, 0.1882352941), 0px 8px 24px rgba(184, 144, 0, 0.3764705882);
  position: absolute;
  bottom: 24px;
  right: 24px;
  transition: all 0.3s;
}
.jssecrets:hover {
  transform: translateY(-4px);
}
              
            
!

JS

              
                // Variables
let a = '';
let b = '';
let currentOperation = '';
let isCompleted = false;

const result = document.querySelector('.result');
const digits = document.querySelectorAll('[data-digit]');
const operations = document.querySelectorAll('[data-operation]');
const equalsBtn = document.querySelector('[data-equals]');
const clearBtn = document.querySelector('[data-clear]');

// Functions

// function for edge cases
const edgeCase = (message) => {
  result.textContent = message;
  a = '';
  b = '';
  currentOperation = '';
  isCompleted = false;
};

const clearAll = () => {
  edgeCase('0');
};

const cleanScreen = () => {
  result.textContent = '';
};

const equalsFunction = () => {
  if (b === '') b = a;

  switch (currentOperation) {
    case '+':
      a = parseInt(a, 10) + parseInt(b, 10);
      break;
    case '-':
      a = parseInt(a, 10) - parseInt(b, 10);
      break;
    case '*':
      a = parseInt(a, 10) * parseInt(b, 10);
      break;
    case '/':
      if (b === '0') {
        edgeCase('Error');
        return;
      }
      a = parseInt(a, 10) / parseInt(b, 10);
      break;
  }
  isCompleted = true;

  // overflow check
  if (a >= 1e20 || a <= -1e20) {
    edgeCase('Overflow');
    a = 0;
    return;
  }

  result.textContent = a;

  // non-standard behaviour
  if (a === '' && b === '') {
    edgeCase('0');
  }
};

const enterOperation = (event) => {
  cleanScreen();

  // if currentOperation entered before first number
  if (a === '' && b === '') {
    a = 0;
  }

  // if equals btn is omited and operation btn used
  if (a !== '' && b !== '' && currentOperation !== '' && !isCompleted) {
    equalsFunction();
    currentOperation = event.currentTarget.dataset.operation;
    return;
  }

  currentOperation = event.currentTarget.dataset.operation;
  result.textContent = currentOperation;
};

const enterDigit = (event) => {
  //
  let symbol = event.currentTarget.dataset.digit;

  cleanScreen();

  if (b === '' && currentOperation === '') {
    a += symbol;
    result.textContent = parseInt(a, 10);

    if (a >= 1e20 || a <= -1e20) {
      result.textContent = 'Overflow';
      a = '';
      return;
    }
  }
  /////////////////
  else if (a !== '' && b !== '' && isCompleted) {
    b = symbol;
    isCompleted = false;
    result.textContent = parseInt(b, 10);
  }
  /////////////////
  else {
    b += symbol;
    result.textContent = parseInt(b, 10);

    if (b >= 1e20 || b <= -1e20) {
      result.textContent = 'Overflow';
      b = '';
      return;
    }
  }
};

// Event Listeners
clearBtn.addEventListener('click', clearAll);

digits.forEach((number) => {
  number.addEventListener('click', enterDigit);
});

operations.forEach((operation) => {
  operation.addEventListener('click', enterOperation);
});

equalsBtn.addEventListener('click', equalsFunction);

              
            
!
999px

Console