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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div class="container">
		<h2 class="title">Javascript Calculator</h2>
		<div class="result-container">
			<h1 class="result"></h1>
			<p class="history"></p>
		</div>
		<div class="input-buttons">
			<button value="AC" class="clear-button">AC</button>

			<button value="/" class="operator-button">/</button>
			<button value="*" class="operator-button">*</button>

			<button value="7" class="number-button">7</button>
			<button value="8" class="number-button">8</button>
			<button value="9" class="number-button">9</button>
			<button value="-" class="operator-button">-</button>

			<button value="4" class="number-button">4</button>
			<button value="5" class="number-button">5</button>
			<button value="6" class="number-button">6</button>
			<button value="+" class="operator-button">+</button>

			<button value="1" class="third-row number-button">1</button>
			<button value="2" class="third-row number-button">2</button>
			<button value="3" class="third-row number-button">3</button>

			<button value="0" class="zero-button">0</button>
			<button value="." class="decimal-button">.</button>
			<button value="=" class="equals-button">=</button>
		</div>
	</div>
              
            
!

CSS

              
                html {
	background-color: #BABABA;
}

body,
button {
	font-family: 'Oxygen Mono', monospace;
}

button {
	width: 62px;
	-webkit-border-radius: 7px;
	border-radius: 7px;
	border-style: none;
	font-size: 30px;
	margin: 10px 0 5px 0;
	padding: 10px 5px;
	cursor: pointer;
	background-color: #818181;
	color: #fff;
	outline: none;
	-webkit-box-shadow: 0 5px #5E5353;
	box-shadow: 0 5px #5E5353;
	vertical-align: top;
}

button:active {
	-webkit-transform: translateY(4px);
	-ms-transform: translateY(4px);
	transform: translateY(4px);
	-webkit-box-shadow: none;
	box-shadow: none;
}

.container {
	margin: 75px auto;
	width: 350px;
	height: 525px;
	padding: 15px 0 0 0;
	-webkit-border-radius: 10px;
	border-radius: 10px;
	text-align: center;
	background-color: #E3A62D;
	-webkit-box-shadow: 10px 10px 19px 4px #42403a, inset 0px -2px 16px 6px #b38227;
	box-shadow: 10px 10px 19px 4px #42403a, inset 0px -2px 16px 6px #b38227;
}

.clear-button {
	background-color: #D24848;
	-webkit-box-shadow: 0 5px #A82424;
	box-shadow: 0 5px #A82424;
}

.third-row {
	float: left;
	margin-right: 7px;
	margin-left: 2px;
}

.zero-button,
.clear-button {
	width: 132px;
}

.operator-button {
	background-color: #32ac22;
	-webkit-box-shadow: 0 5px #1d8510;
	box-shadow: 0 5px #1d8510;
}

.equals-button {
	position: relative;
	bottom: 69px;
	height: 123px;
	background-color: #478dff;
	-webkit-box-shadow: 0 5px #125CD5;
	box-shadow: 0 5px #125CD5;
}

.input-buttons {
	width: 80%;
	margin: 15px auto;
}

.title {
	font-size: 1.5em;
	margin: 10px 0;
}

.result-container {
	font-family: 'Orbitron', sans-serif;
	-webkit-border-radius: 5px;
	border-radius: 5px;
	border: 1px solid #82817F;
	width: 270px;
	background-color: #D4D4AA;
	-webkit-box-shadow: inset 0px 0px 16px 0px #8c8c42;
	box-shadow: inset 0px 0px 16px 0px #8c8c42;
	height: 60px;
	text-align: right;
	margin: 0 auto;
	padding: 0 5px 0 0;
}

.result{
	font-size: 25px;
	margin: 5px 0 0 0;
}

.history {
	font-size: 10px;
	margin: 10px 0 0 0;
}
              
            
!

JS

              
                (function () {
  const display = document.querySelector(".result");
  const history = document.querySelector(".history");
  const number = document.getElementsByClassName("number-button");
  const operator = document.getElementsByClassName("operator-button");
  const zero = document.querySelector(".zero-button");
  const equals = document.querySelector(".equals-button");
  const clear = document.querySelector(".clear-button");
  const decimal = document.querySelector(".decimal-button");
  let decimalState = true;
  let zeroState = false;
  let evaluation = false;

  init();

  function init() {
    display.textContent = "0";
    history.textContent = "0";
    decimalState = true;
    zeroState = false;
    evaluation = false;
  }

  function digitLimit() {
    // Sets digit limit on display to 10 characters. Function discounts decimal from
    // number limit
    if (display.textContent.includes(".")) {
      if (display.textContent.length > 13) {
        display.textContent = "Digit Limit Met";
        history.textContent = "";
      }
    } else {
      if (display.textContent.length > 12) {
        display.textContent = "Digit Limit Met";
        history.textContent = "";
      }
    }
    // Sets digit limit on history to 32 characters.
    if (history.textContent.length > 32) {
      display.textContent = "Digit Limit Met";
      history.textContent = "";
    }
  }

  // Iterates through number elements and sets event listeners
  for (var i = 0; i < number.length; i++) {
    number[i].addEventListener("click", function () {
      const prevDigit = display.textContent.slice(-1);
      if (evaluation || display.textContent === "0") {
        display.textContent = this.value;
        history.textContent = this.value;
      } else if (isNaN(prevDigit) && prevDigit !== ".") {
        display.textContent = this.value;
        history.textContent += this.value;
      } else {
        display.textContent += this.value;
        history.textContent += this.value;
      }
      zeroState = true;
      evaluation = false;
      digitLimit();
    });
  }

  // Iterates through operator elements and sets event listeners
  for (var i = 0; i < operator.length; i++) {
    operator[i].addEventListener("click", function () {
      const prevIndex = display.textContent.slice(-1);
      // if previous value is an operator, a new clicked operator will replace it
      if (prevIndex === "*" || prevIndex === "+" || prevIndex === "-" || prevIndex === "/") {
        display.textContent = display.textContent.slice(0, -1);
        history.textContent = history.textContent.slice(0, -1);
        display.textContent += this.value;
        history.textContent += this.value;
      } else if (history.textContent.length > 0) {
        display.textContent = this.value;
        history.textContent += this.value;
      }
      decimalState = true;
      zeroState = false;
      evaluation = false;
      digitLimit();
    });
  }

  // clears display value
  clear.addEventListener("click", function () {
    init();
  });

  // adds decimal to display
  decimal.addEventListener("click", function () {
    const lastDigit = display.textContent.slice(-1);
    // if decimal button is pressed, then it is disabled unless decimal in current integer is deleted
    if (decimalState && isNaN(lastDigit)) {
      display.textContent = "0" + this.value;
      history.textContent += "0" + this.value;
      decimalState = false;
      zeroState = true;
    } else if (decimalState && !evaluation) {
      display.textContent += this.value;
      history.textContent += this.value;
      decimalState = false;
      zeroState = true;
    } else if (evaluation && decimalState) {
      display.textContent = "0" + this.value;
      history.textContent = "0" + this.value;
      decimalState = false;
    } else {
      display.textContent += "";
    }
    evaluation = false;
    digitLimit();
  });

  zero.addEventListener("click", function () {
    const prevItem = display.textContent.slice(-1);
    // Prevents first number between operators from being zero
    if (!zeroState && prevItem === "0") {
      display.textContent += "";
      decimalState = true
    } else if (evaluation) {
      display.textContent = this.value;
      history.textContent = display.textContent;
    } else {
      display.textContent += this.value;
      history.textContent += this.value;
    }
    digitLimit();
  })

  // Evaluates history.textContent on display and rounds to the nearest hundredths
  // if a decimal
  equals.addEventListener("click", function () {
    const lastIndex = display.textContent.slice(-1);

    // If last digit is not a number then "Bad Syntax!" will display otherwise it
    // will evaluate history.textContent
    if (isNaN(lastIndex)) {
      display.textContent = "Bad Syntax!";
    } else {
      // Rounds any decimal total to nearest hundredth
      display.textContent = parseFloat(eval(history.textContent));
      history.textContent = parseFloat(eval(history.textContent));

    }
    decimalState = true;
    evaluation = true;
    digitLimit();
  });
})()
              
            
!
999px

Console