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.

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

              
                <main class="main">
  <section class="calculadora">
    <table>
      <tbody>
        <tr>
          <td colspan="4">
            <input id="tela" maxlength="30" readonly="readonly" type="text" />
          </td>
        </tr>
        <tr>
          <td><input id="apagarAnterior" type="button" value="&#8678;" /></td>
          <td><input id="limparTela" type="button" value="C" /></td>
        </tr>
        <tr>
          <td><input id="num7" type="button" value="7" /></td>
          <td><input id="num8" type="button" value="8" /></td>
          <td><input id="num9" type="button" value="9" /></td>
          <td><input id="divisao" type="button" value="/" /></td>
        </tr>
        <tr>
          <td><input id="num4" type="button" value="4" /></td>
          <td><input id="num5" type="button" value="5" /></td>
          <td><input id="num6" type="button" value="6" /></td>
          <td><input id="multiplicacao" type="button" value="*" /></td>
        </tr>
        <tr>
          <td><input id="num1" type="button" value="1" /></td>
          <td><input id="num2" type="button" value="2" /></td>
          <td><input id="num3" type="button" value="3" /></td>
          <td><input id="subtracao" type="button" value="-" /></td>
        </tr>
        <tr>
          <td><input id="num0" type="button" value="0" /></td>
          <td><input id="ponto" type="button" value="." /></td>
          <td><input id="resultado" type="button" value="=" /></td>
          <td><input id="soma" type="button" value="+" /></td>
        </tr>
      </tbody>
    </table>
  </section>
</main>

              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Roboto');

*{
  margin: 0 auto;
  padding: 0;
  font-family:Roboto, Arial, sans-serif;
  box-sizing: border-box;
  font-size: 18px;
  border: 0;
}

.main {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  background-color: #8EC5FC;
  background-image: linear-gradient(62deg, #8EC5FC 0%, #E0C3FC 100%);
  background-image: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="8" height="8" viewBox="0 0 8 8"%3E%3Cg fill="%239C92AC" fill-opacity="0.4"%3E%3Cpath fill-rule="evenodd" d="M0 0h4v4H0V0zm4 4h4v4H4V4z"/%3E%3C/g%3E%3C/svg%3E');
  align-items: center;
}

.calculadora{
  text-align: center;
  background-color: #7f8c8d;
  padding: 0.5em;
  border: 0;
}

.calculadora input {
  width: 3em;
  height: 3em;
  cursor: pointer;
}

.calculadora input:hover {
  color: #fff;
  background-color: #546E7A;
}

.calculadora input#tela {
  text-align: right;
  width: 100%;
  cursor: auto;
  padding: 0.3em;
}

.calculadora input#tela:hover {
  background-color: #fff;
  color: #000;
}
              
            
!

JS

              
                var tela = document.getElementById("tela");

//Array de elementos que receber uma listener
var listenerBtn = [];

//teclas adicionais do teclado
var btnResultado = document.getElementById("resultado");
var btnLimparTela = document.getElementById("limparTela");
var btnApagarAnterior = document.getElementById("apagarAnterior");

listenerBtn.push(document.getElementById("ponto"));

//teclas dos operadores
listenerBtn.push(document.getElementById("soma"));
listenerBtn.push(document.getElementById("subtracao"));
listenerBtn.push(document.getElementById("divisao"));
listenerBtn.push(document.getElementById("multiplicacao"));

//teclas númericas da calculadora
listenerBtn.push(document.getElementById("num0"));
listenerBtn.push(document.getElementById("num1"));
listenerBtn.push(document.getElementById("num2"));
listenerBtn.push(document.getElementById("num3"));
listenerBtn.push(document.getElementById("num4"));
listenerBtn.push(document.getElementById("num5"));
listenerBtn.push(document.getElementById("num6"));
listenerBtn.push(document.getElementById("num7"));
listenerBtn.push(document.getElementById("num8"));
listenerBtn.push(document.getElementById("num9"));

//Adicionando evento de click
for (var i = 0; i < listenerBtn.length; i++) {
  listenerBtn[i].addEventListener("click", passarValorTela);
}

btnResultado.onclick = function () {
  verificarResulatado();
}

function verificarResulatado() {
  try {
    var aux = tela.value.substring(tela.value.length - 1, tela.value.length);
    if (verificarOperador(aux)) {
      apagarAnterior();
    }

    var valorCalculado = eval(tela.value); //calcular o conteúdo da string
    if (valorCalculado || valorCalculado == "0") {
      tela.value = valorCalculado;
    } else {
      throw "erro";
    }
  } catch (e) {
    console.error(e);
  }
}

function passarValorTela() {

  if (verificarOperador(this.value)) {
    var aux = tela.value.substring(tela.value.length - 1, tela.value.length);
    //subtituir o valor do operador pelo atual
    if (verificarOperador(aux)) {
      apagarAnterior();
    }
  }
  if (this.value) {
    tela.value += this.value;
  }

}

btnApagarAnterior.onclick = function () {
  apagarAnterior();
}

function apagarAnterior() {
  if (tela.value.length > 0) {
    var aux = tela.value.substring(0, tela.value.length - 1);
    tela.value = aux;
  }
}

btnLimparTela.onclick = function () {
  tela.value = "";
}

function verificarOperador(valor) {
  switch (valor) {
    case "+":
      return true;
    case "-":
      return true;
    case "*":
      return true;
    case "/":
      return true;

    default:
      return false;
  }
}

              
            
!
999px

Console