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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <!-- 
IMPROVEMENTS:
  1. do more testing to make sure floating point rounding is handled and scientific if fully disabled.
-->


<div class="container">
  <div id="main">
    <div id="display-outline">
      <div id="display">
        <img id="sticker1" src="https://vignette3.wikia.nocookie.net/mlp/images/4/45/FANMADE_Pinkie_Pie_Best_Pony_Sticker.png/revision/latest?cb=20130723022841">
        <img src="https://cdn3.iconfinder.com/data/icons/online-marketing-vol-3/72/106-512.png"> Imperial
        <div id="entry">0
        </div>
      </div>
    </div>

    <!-- row1 -->
    <table>
      <tr>
        <td>
          <button id="allClear" value="AC"> AC
    </button></td>
        <td>
          <button class="number" value="7"> 7
    </button></td>
        <td>
          <button class="number" value="8"> 8
    </button></td>
        <td>
          <button class="number" value="9"> 9
    </button></td>
        <td>
          <button class="operator" value="/"> &divide
    </button></td>
      </tr>

      <!-- row2 -->
      <tr>
        <td>
          <button id="clear"> C
    </button></td>
        <td>
          <button class="number" value="4"> 4
    </button></td>
        <td>
          <button class="number" value="5"> 5
    </button></td>
        <td>
          <button class="number" value="6"> 6
    </button></td>
        <td>
          <button class="operator" value="*"> x
    </button></td>
      </tr>

      <!-- row3 -->
      <tr>
        <td>
          <button id="negative"> +/-
    </button></td>
        <td>
          <button class="number" value="1"> 1
    </button></td>
        <td>
          <button class="number" value="2"> 2
    </button></td>
        <td>
          <button class="number" value="3"> 3
    </button></td>
        <td>
          <button class="operator" value="-"> -
    </button></td>
      </tr>
      <!-- row4 -->
      <tr>
        <td>
          <button id="percent"> %
    </button></td>
        <td>
          <button class="number" value="0"> 0
    </button></td>
        <td>
          <button id="decimal" value="."> <sup>.</sup>
    </button></td>
        <td>
          <button id="equate"> =
    </button></td>
        <td>
          <button class="operator" value="+"> +
  </button></td>
      </tr>
    </table>
  </div>
</div>
            
          
!

CSS

            
              body {
  background-image: url("https://raw.githubusercontent.com/jjspetz/FCC-Calculator/master/assets/background.jpg");
  background-size: cover;
}

button {
  display: inline-block;
  margin: 8px 0 0 20px;
  padding: 2px;
  font-size: 36pt;
  background-color: rgb(209, 7, 54);
  color: #fcfcfc;
  text-shadow: 1px 1px 0 rgb(255, 48, 106), -1px -1px 0 rgb(255, 48, 106), 2px 2px 3px #444;
  border: none;
}

button:active {
  transform: translate(2px, 2px);
  text-shadow: 1px 1px 0 rgb(255, 48, 106), -1px -1px 0 rgb(255, 48, 106);
  outline: none;
}
button:focus {
  outline: none;
}

img {
  width: 15px;
  position: relative;
  top: -2px;
}

sup {
  position: relative;
  top: -10px;
  font-size: 65pt;
}

table {
  margin-top: 10px;
  text-align: center;
}

#display {
  position: relative;
  font-family: 'Permanent Marker', cursive;
  font-size: 11pt;
  padding-top: 0.5em;
  margin: 0.2em auto;
  text-align: center;
  width: 21em;
  height: 10em;
  border-radius: 8px;
  background-color: rgb(148, 152, 161);
  border: 1px solid white;
}

#display-outline {
  border: 2px solid rgb(255, 48, 106);
  border-radius: 10px;
  box-shadow: 1px 1px 5px #666;
}

#decimal-btn {
  font-size: 30pt;
}

#entry {
  font-family: 'Patua One', cursive;
  text-align: right;
  font-size: 14pt;
  background-color: #999080;
  max-width: 80%;
  height: 2.5em;
  margin: 1em auto;
  padding: 0.5em;
  border-radius: 2px;
  border: 1.2px solid white;
  box-shadow: inset 1px 1px 2px #555; 
}

#main {
  margin: 5em auto;
  padding: 10px;
  width: 24em;
  height: 36em;
  background-color: rgb(209, 7, 54);
  box-shadow: 2px 3px 8px #555;
  border-radius: 8px;
}

#sticker1 {
  width: 60px;
  position: absolute;
  top: -4px;
  left: -2px;
  transform: rotate(-24deg);
}
            
          
!

JS

            
              var current = "";
var lastNum = "";

//numbers
$(".number").click(function() {
  current += $(this).attr("value");
  lastNum += $(this).attr("value");
  display(current);
})

//operators
$(".operator").click(function() {
  var patt = /[0-9.]$/;
  if (patt.test(current)) { // test to not allow double operators.
  current += $(this).attr("value");
  lastNum = "";
  display(current);
}
})
// percents
$("#percent").click(function() {
  var newLength = current.length - lastNum.length;
  
  if (current) {
    if (lastNum.length > 19) {
      current = 'OVERFLOW';
    } else {
    let ans = roundoff(lastNum + '/'+ '100');
  
    current = current.slice(0, newLength) + ans;
    lastNum = ans.toString();
    }
  }
  
  display(current);
})

// decimal
$("#decimal").click(function() {  
if (lastNum.indexOf(".") < 0) {
  if (current.length < 1) {
    current ="0.";
    lastNum ="0.";
  } else {
      current += $(this).attr("value");
      lastNum += $(this).attr("value");
  }
    display(current);
  }
})

// negative numbers
$("#negative").click(function() {
  var newLength = current.length - lastNum.length;
  
  if (/^-/.test(lastNum)) {
    lastNum = lastNum.slice(1, lastNum.length)
  } 
  else if (lastNum === "") {
     lastNum = "";
  } else {
    lastNum = "-"+ lastNum;
  }
  
  current = current.slice(0, newLength) + lastNum;
  
  display(current);
})

// clears
$("#clear").click(function() {
  if (current.length > 0) {
    current = current.slice(0, (current.length - 1));
    lastNum = lastNum.slice(0, (lastNum.length-1));
  if (lastNum === "-") {
    lastNum = "";
    current = current.slice(0, (current.length - 1));
  }
    display(current);
  } else {
    allClear();
  }
})

function allClear() {
  current = "";
  lastNum = "";
  display(current);
}
$("#allClear").click(function() {
  allClear();
})

//equate
$("#equate").on('click', function() {
    
    //checks for double negatives which the eval Method can't parse and replaces them with plus.
    current = current.replace("--","+");
    
    let ans = roundoff(current);

    current = ans.toString();
    lastNum = current;
    display(current);
})

//display function
function display(string) {
  if (string.length > 20) {
      $("#entry").html('<p>OVERFLOW</p>');
  } else {
    var displayStr = string.replace("/","&divide");
    displayStr = displayStr.replace("*","x")
    $("#entry").html('<p>' + displayStr + '</p>');
  }
}

// format fucntion
// this function tries to limit some of the wackiness of floating
// point math in javascript.
function roundoff(string) {
  let ans = string;
  ans = ans.replace(/[+-/*//.]/, "");

  let sum = ans.length - 1;
  
  if (string)
  return (eval(string).toFixed(sum)).replace(/0+$/, '');
}
            
          
!
999px

Console