Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <!DOCTYPE html>
<html lang="pt-br">

<head>
    <title>Calculadora</title>
    <meta charset="UTF-8">
    <meta http-equiv="content-type" content="text/html;" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- BOOTSTRAP -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <!-- google fonts -->
    <link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet">

    <!-- javaScript -->
    
    <script type="text/javascript" src="js/calculadora.js"></script>

</head>

<body>
    <style>
        
    </style>
    <div class="container">
        <div class="row">
            <div class="col-xs-12" id="titulo">
                <h3 >Calculadora Básica</h3>
            </div>
        </div>
    </div>
    <div class="container">
        <div class="row align-items-center">
            <div class="col-xs-12 col-sm-6 col-md-3 fundo-mod margem borda">
                    
                <div class="justify-content-center centro">
                    
                    <table class="table table-dark">
                        <tr>
                            
                            <td colspan="4" ><div id="historico" onclick="mostrarHora();"></div></td>
                        </tr>
                        <tr>
                            
                            <td colspan="4"><input type="text" name="visor" id="visor" class="col-xs-12 col-sm-12 col-md-12" /></td>
                        </tr>
                        <tr>
                            <td><input type="button" id="botao7" value="7" class="btn btn-secondary mesmo-tamanho" onclick="botao(value);"/></td>
                            <td><input type="button" id="botao8" value="8" class="btn btn-secondary mesmo-tamanho" onclick="botao(value);"/></td>
                            <td><input type="button" id="botao9" value="9" class="btn btn-secondary mesmo-tamanho" onclick="botao(value);"/></td>
                            <td><input type="button" id="botao/" value="/" class="btn btn-info mesmo-tamanho" onclick="btn_divide(value);" /></td>
                        </tr>
                        <tr>
                            <td><input type="button" id="botao4" value="4" class="btn btn-secondary mesmo-tamanho" onclick="botao(value);"/></td>
                            <td><input type="button" id="botao5" value="5" class="btn btn-secondary mesmo-tamanho" onclick="botao(value);"/></td>
                            <td><input type="button" id="botao6" value="6" class="btn btn-secondary mesmo-tamanho" onclick="botao(value);"/></td>
                            <td><input type="button" id="botao*" value="*" class="btn btn-info mesmo-tamanho" onclick="btn_multiplica(value);"/></td>
                        </tr>
                        <tr>
                            <td><input type="button" id="botao1" value="1" class="btn btn-secondary mesmo-tamanho" onclick="botao(value);"/></td>
                            <td><input type="button" id="botao2" value="2" class="btn btn-secondary mesmo-tamanho" onclick="botao(value);"/></td>
                            <td><input type="button" id="botao3" value="3" class="btn btn-secondary mesmo-tamanho" onclick="botao(value);"/></td>
                            <td><input type="button" id="botao-" value="-" class="btn btn-info mesmo-tamanho" onclick="btn_subtrai(value);" /></td>
                        </tr>
                        <tr>
                            <td><input type="button" id="botao0" value="0" class="btn btn-secondary mesmo-tamanho" onclick="botao(value);"/></td>
                            <td><input type="button" id="botao=" value="=" class="btn btn-info mesmo-tamanho" onclick="btn_igual(value);"/></td>
                            <td><input type="button" id="botaoC" value="C" class="btn btn-danger mesmo-tamanho" onclick="reset(value);"/></td>
                            <td><input type="button" id="soma" value="+" class="btn btn-info mesmo-tamanho" onclick="btn_soma(value);"/></td>
                        </tr>
                    </table>
                </div>
                
            </div>
        </div>
    </div>
</body>

</html>
              
            
!

CSS

              
                
  .fundo-mod {
    background-color: rgb(23, 162, 184);
        
  }

  .margem {
    padding: 1%;
    border-radius: 5px;
    
  }
  .mesmo-tamanho {
    border-radius: 1px;
    
}

.display {
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-right: -50%;
    transform: translate(-50%, -50%);
}
#visor{
  font-family: 'Press Start 2P', cursive;
}
#historico{
  font-family: 'Press Start 2P', cursive;
}
#titulo{
  font-family: 'Lobster', cursive;
}
.borda{
  -webkit-box-shadow: 0 3px 9px black;
  box-shadow: 0 3px 9px black;
  border-radius: 3px;
  -webkit-transition: border .2s ease-in-out;
  transition: border .2s ease-in-out;
}

.centro {
  width:100%;
  height:100%;
  }
              
            
!

JS

              
                var valorVisor = 0;
var numeroA;
var numeroB;
var operacao;
var agora = new Date;


function botao(dado) {
    var auxiliar = document.getElementById("visor").value; // auxiliar recebe o valor pressionado no visor
    document.getElementById("visor").value = auxiliar + dado; // visor recebe o valor de auxiliar e concatena com dado


    valorVisor = document.getElementById("visor").value = auxiliar + dado;
    //document.getElementById("historico").innerHTML = agora.getHours();
    
    // mostrar a saudação acima do visor
    var hora = agora.getHours();
    
    if(hora >= 0 && hora <= 12){
      document.getElementById("historico").textContent = "Bom dia";
    }
    if(hora >= 13 && hora <= 17){
      document.getElementById("historico").textContent = "Boa tarde";
    }
    if(hora >= 18 && hora <= 23){
      document.getElementById("historico").textContent = "Boa noite";
    }
}


function btn_soma(caracter){
    numeroA = valorVisor;
    operacao = "+";
    
    limpar();
    //document.getElementById("historico").innerHTML += operacao;
}

function btn_subtrai(caracter){
  numeroA = valorVisor;
  operacao = "-";
  
  limpar();
}

function btn_multiplica(caracter){
  numeroA = valorVisor;
  operacao = "*";
  
  limpar();
}

function btn_divide(caracter){
  numeroA = valorVisor;
  operacao = "/";
  
  limpar();
}

function reset() {
    // limpar visor
    document.getElementById('visor').value = '';
    valorVisor = 0;
    operacao = "";
}
function limpar(){
    document.getElementById('visor').value = '';
    
}

function btn_igual(){
    numeroB = valorVisor;
    calcular();
}

function calcular() {
    // faz o calculo, pega o resultado e colocar no visor
    //document.getElementById('visor').value = eval(resultado);

    //document.getElementById('visor').value = resultado;
    //document.getElementById('visor').value = eval(valorVisor);

    var total = 0;
    var ultimoTotal = 0;
      switch(operacao){
        case "+":
          total = parseFloat(numeroA) + parseFloat(numeroB);
          break;
        case "-":
            total = parseFloat(numeroA) - parseFloat(numeroB);
            break;
        case "*":
          total = parseFloat(numeroA) * parseFloat(numeroB);
          break;
        case "/":
          total = parseFloat(numeroA) / parseFloat(numeroB);
          break;
      }
      ultimoTotal = total;
      reset();
      document.getElementById('visor').value = total;
      valorVisor = ultimoTotal;

}



              
            
!
999px

Console