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

              
                <link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">

<body>
  <div class="container panel panel-default" id="calc">
      <div class="row">
        <div class="xs-col-12" id="title">FreeCodeCalc</div>
        <div class="col-xs-2" id="sign"></div>
        <div class="col-xs-10" id="digits"></div>
        <div class="col-xs-12" id="input"></div>
        
        <div class="col-xs-3 btn btn-primary raised">7</div>
        <div class="col-xs-3 btn btn-primary raised">8</div>
        <div class="col-xs-3 btn btn-primary raised">9</div>
        <div class="col-xs-3 btn btn-default raised">÷</div>
        
        <div class="col-xs-3 btn btn-primary raised">4</div>
        <div class="col-xs-3 btn btn-primary raised">5</div>
        <div class="col-xs-3 btn btn-primary raised">6</div>
        <div class="col-xs-3 btn btn-default raised">×</div>

        <div class="col-xs-3 btn btn-primary raised">1</div>
        <div class="col-xs-3 btn btn-primary raised">2</div>
        <div class="col-xs-3 btn btn-primary raised">3</div>
        <div class="col-xs-3 btn btn-default raised">-</div>

        <div class="col-xs-3 btn btn-warning raised">C</div>
        <div class="col-xs-3 btn btn-primary raised">0</div>
        <div class="col-xs-3 btn btn-default raised">.</div>
        <div class="col-xs-3 btn btn-default raised">+</div>

        <div class="col-xs-3 btn btn-warning raised">BKSP</div>
        <div class="col-xs-9 btn btn-default raised">=</div>
        <div>&nbsp</div>
      </div>
    </div>
  
</body>

              
            
!

CSS

              
                @media (max-width:320px) {
  #calc {
    width: 100% !important;
  }
}

.btn {
  -webkit-border-radius: 25px !important;
  -moz-border-radius: 25px !important;
  border-radius: 25px !important;
}

body {
  font-family: "Press Start 2P";
  background: darkgreen;
}

#calc {
  width: 340px;
  /*margin-top: 30px;*/
  border-radius: 10px;
  border-width: 10px;
  border-style: outset;
    position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

#digits {
  font-size: 24px;
  text-align: right;
  height: 30px;
  margin-bottom: 10px;
  margin-top: 10px;
}

#sign {
  font-size: 24px;
  text-align: left;
  height: 30px;
  margin-bottom: 10px;
  margin-top: 10px;
}

#input {
  font-size: 8px;
  height: 16px;
}

#title {
  font-size: 8px;
  text-align: center;
}

/*button style ideas from
https://bootstrapbay.com/blog/bootstrap-button-styles/ 
*/
.btn-primary.raised {
	box-shadow: 0 3px 0 0 #a6a6a6;
}

.btn-primary.raised:active, .btn-primary.raised.active {
	box-shadow: none;
	margin-bottom: -3px;
	margin-top: 3px;
}

.btn-default.raised {
	box-shadow: 0 3px 0 0 #a6a6a6;
}

.btn-default.raised:active, .btn-default.raised.active {
	box-shadow: none;
	margin-bottom: -3px;
	margin-top: 3px;
}

.btn-warning.raised {
	box-shadow: 0 3px 0 0 #a6a6a6;
}

.btn-warning.raised:active, .btn-warning.raised.active {
	box-shadow: none;
	margin-bottom: -3px;
	margin-top: 3px;
}
              
            
!

JS

              
                /*The current result is stored in the digits div. The input string since 
  equals was last pushed is stored in the input div. The last operator
  entered is stored in #sign.
  Two copies of the current input are stored: the one in #input and the
  one stored in toEval. The second one is not displayed. The reason for having
  two copies is so that the 'times' and 'division' symbols could be used.
  This could probably be simplified by setting the id of those divs to '*'
  and '/'.
  When a button is pushed, the innerHTML is sent to a switch statement
  for the appropriate action.
  
  The exponential notation feature is unfinished.
*/
var lastChar = "";
var decPoint = false;
var operator = false;
var answer = false;
var clear = true;
var num = /[0-9]/;
var ops = ["+", "-", "×", "÷"];
var toEval = ""; //This is what is sent to eval(). 
var temp = 0.0;
var isExp = false;

$(".btn").click(function () {
  lastChar = event.target.innerHTML;
  switch (lastChar) {
    case "0":
      if(document.getElementById("digits").innerHTML == "0") return;
    case "1":
    case "2":
    case "3":
    case "4":
    case "5":
    case "6":
    case "7":
    case "8":
    case "9":
      if(isExp) CLEAR();
      if(testLen() && !operator) return;
      if(answer) CLEAR();
      if(operator) document.getElementById("digits").innerHTML = "";
      document.getElementById("digits").innerHTML += lastChar;
      document.getElementById("input").innerHTML += lastChar;   
      toEval += lastChar;
      operator = false;
      clear = false;
      answer = false;
      break;

    case "BKSP":
      if(isExp) CLEAR();
      if(answer) {
        document.getElementById("digits").innerHTML = "";
        answer = false;
        return;
      }
      if(document.getElementById("digits").innerHTML.length > 0) {
        document.getElementById("digits").innerHTML = 
          document.getElementById("digits").innerHTML.slice(0, -1);
          document.getElementById("input").innerHTML = document.getElementById("input").innerHTML.slice(0, -1);
          toEval = toEval.slice(0, -1);
      } else {
        //if(document.getElementById("sign").innerHTML != "") {
        //if(ops.test(toEval.slice(-1))) {

        //}
        if(ops.includes(document.getElementById("input").innerHTML.slice(-1))) {     
          operator = false;
          document.getElementById("sign").innerHTML = "";
        }        
        document.getElementById("input").innerHTML = 
          document.getElementById("input").innerHTML.slice(0, -1);
        toEval = toEval.slice(0, -1);

      }
      if(!document.getElementById("digits").innerHTML.includes(".")) {
        decPoint = false;
      }
      break;
      
    case ".":
      if((decPoint || testLen()) && !answer) return;
      if(isExp) CLEAR();
      if(answer) {
        CLEAR();
      }
      if(operator) {
        document.getElementById("digits").innerHTML = "";
      }
      document.getElementById("digits").innerHTML += lastChar;
      document.getElementById("input").innerHTML += lastChar;
      toEval += ".";
      operator = false;
      decPoint = true;
      break;
      
    case "C":
      CLEAR();
      break;
      
    case "×":
    case "÷":
    case "+":
    case "-":
      //need for backspace
      if(ops.includes(document.getElementById("input").innerHTML.slice(-1))) return;
      if(operator) return;
      //todo
      if(isExp) CLEAR();
      if(!(/[0-9]/.test(document.getElementById("digits").innerHTML)) && document.getElementById("input").innerHTML == "") return;
      if(answer) {
        document.getElementById("input").innerHTML = temp;
        answer = false;
      }
      document.getElementById("sign").innerHTML = lastChar;
      document.getElementById("input").innerHTML += lastChar;
      if(lastChar == "×") {
        toEval += "*";
      } else if(lastChar == "÷") {
        toEval += "/";
      } else {
        toEval += lastChar;
      }
      operator = true;
      clear = false;
      decPoint = false;
      break;
      
    case "=":
      if(clear || operator) return;
      if(ops.includes(document.getElementById("input").innerHTML.slice(-1))) return;
      document.getElementById("sign").innerHTML = lastChar;
      temp = String(eval(toEval));
      if(!temp.includes(".") && temp.length > 9) {
        temp = toSci(temp);
      } else {
        temp = temp.slice(0, 9);
      }
        document.getElementById("digits").innerHTML = temp;
      answer = true;
      decPoint = false;
      break;
  }
});

function CLEAR() {
  document.getElementById("digits").innerHTML = "";
  document.getElementById("sign").innerHTML = "";
  document.getElementById("input").innerHTML = "";
  toEval = "";
  decPoint = false;
  operator = false;
  answer = false;
  clear = true;
  temp = 0.0;
  isExp = false;
}

//only goes one-way right now
function toSci(big) {
  isExp = true;
  if(big[0] == ".") {
    //todo
  }
  return big[0] + "." + big.slice(1, 3) + "×10^" + (big.length - 1);
}

function testLen() {
  //console.log(lastChar);
  if(document.getElementById("digits").innerHTML.length >= 9) return true;
  if(document.getElementById("input").innerHTML.length >= 33) return true;
  return false;
}
              
            
!
999px

Console