cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <head>
  <title>JavaScript Calculator</title>
  <link href='https://fonts.googleapis.com/css?family=Orbitron' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="container-fluid pagination-centered">  
<table>
  <thead>
    <td>&nbsp;</td>
    <td colspan="3">
      <div id="flow">0</div>
    </td>
  </thead>
  <tbody>
    <tr>
      <td colspan="4">
        <div id="output">0</div>
      </td>
      <tr>
        <tr>
          <td><button id="allClear"  title="Clear All" onclick="allClear()">AC</button></td>
          <td><button id="clearLast"  title="Clear Last" onclick="clearLast()">CL</button></td>
          <td><button id="divide"  onclick="divide()">÷</button></td>
          <td><button id="multiply"  onclick="multiply()">×</button></td>
        </tr>
        <tr>
          <td><button id="seven"  onclick="seven()">7</button></td>
          <td><button id="eight"  onclick="eight()">8</button></td>
          <td><button id="nine"  onclick="nine()">9</button></td>
          <td><button id="subtract"  onclick="substract()">-</button></td>
        </tr>
        <tr>
          <td><button id="four"  onclick="four()">4</button></td>
          <td><button id="five"  onclick="five()">5</button></td>
          <td><button id="six"  onclick="six()">6</button></td>
          <td><button id="add"  onclick="add()">+</button></td>
        </tr>
        <tr>
          <td><button id="one"  onclick="one()">1</button></td>
          <td><button id="two"  onclick="two()">2</button></td>
          <td><button id="three"  onclick="three()">3</button></td>
          <td rowspan="2"><button id="equals"  onclick="equals()"><br>=<br></button></td>
        </tr>
        <tr>
          <td colspan="2"><button id="zero"  onclick="zero()"><span id="zero-button">0</span></button>
          </td>
          <td><button id="point"  onclick="point()">▪</button></td>
        </tr>
        </br>
        <tbody>
</table>
<footer>
Made by <a href="https://www.freecodecamp.com/radekjohn" target="_blank">Radek John</a>.
    </footer>  
</div>  
</body>
            
          
!
            
              body {
  background: black;
  font-family: 'Orbitron', sans-serif;
  font-size: 250%;
  color: green;
}


table {
  order-collapse: collapse;
  brder-style: hidden;  
}
td { 
    width: 3em;
}
thead {
   text-align: right;  
}

thead tr {  
  border-style: hidden;
}
button {
  width: 100%;
  height: 100%;
  color: green;
  background: black;
  border: black;
  font-family: 'Orbitron', sans-serif; 
}

button:hover {
  background: green;
  border-color: black;
  color: black;
}

button:active {
  background: lightgreen;
  border-color: lightgreen;
  color: green;
  border-color: black;
}

button:focus {
  outline:0;
}
.test{
 color: red; 
} 
  footer{
 font-size: 40%; 
}
  
#flow {
  font-size: 40%;
}

#flow:hover {
 background: black;
 color: green;
}

#output{
  text-align: right;
  font-size: 150%;
}

#output:hover {
 background: black;
  color: green;
}

#zero-button {
   margin-left: -50%;
}


a {
  color: green;
  text-decoration: underline;
}

a:hover {
  color: black;
  background: green;
  text-decoration: underline;
}
            
          
!
            
              var storage = [];
var numbers = [];
var str = "";
var res = "";

// Calculator controls
function allClear() {
  storage = [];
  $("#flow").html(0);
  $("#output").html(0);
}

function clearLast() {
  storage.pop();
  $("#flow").html(storage[storage.length - 1]);
  $("#flow").html(storage);
  str = storage.join(",").replace(/,/g, "");
  if (str.length == 0) {
    $("#flow").html(0);
    $("#output").html(0);
  } else {
    $("#output").html(str);
  }

}

// Operators
function add() {
  storage.push("+");
  $("#flow").html(storage);
  $("#output").html("+");
}

function substract() {
  storage.push("-");
  $("#flow").html(storage);
  $("#output").html("-");
}

function multiply() {
  storage.push("*");
  $("#flow").html(storage);
  $("#output").html("*");
}

function divide() {
  storage.push("/");
  $("#flow").html(storage);
  $("#output").html("/");
}

function point() {
  storage.push(".");
  $("#flow").html(storage);
  str = storage.join(",").replace(/,/g, "");
  $("#output").html(str);
}

// Digits
function zero() {
  storage.push(0);
  $("#flow").html(storage);
  if (str.length > 9) {
    $("#flow").html(str.slice(str.length - 9));
  }
  str = storage.join(",").replace(/,/g, "");
  if (str.length > 9) {
    $("#output").html(str.slice(str.length - 9));
  } else {
    $("#output").html(str);
  }
}

function one() {
  storage.push(1);
  $("#flow").html(storage);
  if (str.length > 9) {
    $("#flow").html(str.slice(str.length - 9));
  }
  str = storage.join(",").replace(/,/g, "");
  if (str.length > 20) {
    $("#output").html(str.slice(str.length - 20));
  } else {
    $("#output").html(str);
  }
}

function two() {
  storage.push(2);
  $("#flow").html(storage);
  if (str.length > 9) {
    $("#flow").html(str.slice(str.length - 9));
  }
  str = storage.join(",").replace(/,/g, "");
  if (str.length > 9) {
    $("#output").html(str.slice(str.length - 9));
  } else {
    $("#output").html(str);
  }
}

function three() {
  storage.push(3);
  $("#flow").html(storage);
  if (str.length > 9) {
    $("#flow").html(str.slice(str.length - 9));
  }
  str = storage.join(",").replace(/,/g, "");
  if (str.length > 9) {
    $("#output").html(str.slice(str.length - 9));
  } else {
    $("#output").html(str);
  }
}

function four() {
  storage.push(4);
  $("#flow").html(storage);
  if (str.length > 9) {
    $("#flow").html(str.slice(str.length - 9));
  }
  str = storage.join(",").replace(/,/g, "");
  if (str.length > 10) {
    $("#output").html(str.slice(str.length - 10));
  } else {
    $("#output").html(str);
  }
}

function five() {
  storage.push(5);
  $("#flow").html(storage);
  if (str.length > 9) {
    $("#flow").html(str.slice(str.length - 9));
  }
  str = storage.join(",").replace(/,/g, "");
  if (str.length > 9) {
    $("#output").html(str.slice(str.length - 9));
  } else {
    $("#output").html(str);
  }
}

function six() {
  storage.push(6);
  $("#flow").html(storage);
  if (str.length > 9) {
    $("#flow").html(str.slice(str.length - 9));
  }
  str = storage.join(",").replace(/,/g, "");
  if (str.length > 9) {
    $("#output").html(str.slice(str.length - 9));
  } else {
    $("#output").html(str);
  }
}

function seven() {
  storage.push(7);
  $("#flow").html(storage);
  if (str.length > 9) {
    $("#flow").html(str.slice(str.length - 9));
  }
  str = storage.join(",").replace(/,/g, "");
  if (str.length > 11) {
    $("#output").html(str.slice(str.length - 11));
  } else {
    $("#output").html(str);
  }
}

function eight() {
  storage.push(8);
  $("#flow").html(storage);
  if (str.length > 9) {
    $("#flow").html(str.slice(str.length - 9));
  }
  str = storage.join(",").replace(/,/g, "");
  if (str.length > 9) {
    $("#output").html(str.slice(str.length - 9));
  } else {
    $("#output").html(str);
  }
}

function nine() {
  storage.push(9);
  $("#flow").html(storage);
  if (str.length > 9) {
    $("#flow").html(str.slice(str.length - 9));
  }
  str = storage.join(",").replace(/,/g, "");
  if (str.length > 9) {
    $("#output").html(str.slice(str.length - 9));
  } else {
    $("#output").html(str);
  }
}

function equals() {
  str = storage.join(",").replace(/,/g, "");
  res = eval(storage.join(""));

  if (res.toString().length > 9) {
    res = res.toExponential(2);
  }

  if (str.indexOf("/0") !== -1 ||
    str.indexOf("//") !== -1 ||
    str.indexOf("*/") !== -1 ||
    str.indexOf("/-") !== -1 ||
    str.indexOf("/+") !== -1 ||
    str.indexOf("*/") !== -1 ||
    str.indexOf("**") !== -1 ||
    str.indexOf("*-") !== -1 ||
    str.indexOf("*+") !== -1 ||
    str.indexOf("*/") !== -1 ||
    str.indexOf("-/") !== -1 ||
    str.indexOf("-*") !== -1 ||
    str.indexOf("--") !== -1 ||
    str.indexOf("-+") !== -1 ||
    str.indexOf("+/") !== -1 ||
    str.indexOf("+*") !== -1 ||
    str.indexOf("+-") !== -1 ||
    str[str.length - 1] === "/" ||
    str[str.length - 1] === "*" ||
    str[str.length - 1] === "-" ||
    str[str.length - 1] === "+"
  ) {
    $("#output").html("-error-");
  } else {
    $("#output").html(res);
  }
  if (storage.length > 0) {
    storage.push("=");
  } else {
    storage.push(0);
    $("#flow").html(storage);
    $("#flow").html(eval(storage.join("")));
  }
  storage.pop();
}

// Keyboard input (used https://www.kirupa.com/html5/keyboard_events_in_javascript.htm)
window.addEventListener("keydown", moveSomething, false);

function moveSomething(e) {
  switch (e.keyCode) {
    // Calculator controls
    case 65:
      allClear();
      break;
    case 67:
      clearLast();
      break;
      // Operators
    case 107:
      add();
      break;
    case 109:
      substract();
      break;
    case 111:
      divide();
      break;
    case 106:
      multiply();
      break;
    case 111:
      divide();
      break;
    case 110:
      point();
      break;
    case 13:
      equals();
      break;
      // digits  
    case 96:
      zero();
      break;
    case 97:
      one();
      break;
    case 98:
      two();
      break;
    case 99:
      three();
      break;
    case 100:
      four();
      break;
    case 101:
      five();
      break;
    case 102:
      six();
      break;
    case 103:
      seven();
      break;
    case 104:
      eight();
      break;
    case 105:
      nine();
      break;
  }
}
            
          
!
999px
Loading ..................

Console