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

              
                <div class="container column">
  <div class="column">
     <div class="title">
       Calculator
       </div>
    <div id="footer"><a href="https://www.freecodecamp.com/alpha-oliveira" target="_blank">by &alpha;-oliveira
      </a></div>
    <div class="visor">
  <input type= "text" id = "visor" placeholder="0" disabled>
    </div>
    </div>

<div class="row keys">

  <div class="main column">
    <div class="row">
      <button class="btn btn-red clean" id="btnAC">    AC   </button>
      <button class="btn btn-red clean" id="btnCE">    CE   </button>
    </div>
    <div class="row">
      <button class="btn btn-default number" id="btn7">  7   </button>
      <button class="btn btn-default number" id="btn8">    8   </button>
    </div>
    <div class="row">
      <button class="btn btn-default number" id="btn4">  4   </button>
      <button class="btn btn-default number" id="btn5">    5  </button>
    </div>
    <div class="row">
      <button class="btn btn-default number" id="btn1">  1  </button>
      <button class="btn btn-default number" id="btn2">    2  </button>
    </div>
   
      <button class="btn btn-default btn-large number" id="btn0">    0  </button>
    

  </div>

  <div class="midddle column ">
    <button class="btn btn-default operation" id="btnDiv">   &divide;   </button>
    <button class="btn btn-default number" id="btn9">    9   </button>
    <button class="btn btn-default number" id="btn6">    6  </button>
    <button class="btn btn-default number" id="btn3">    3  </button>
    <button class="btn btn-default number" id='btnDot'>   .  </button>
  </div>


  <div class="right column">

    <button class="btn btn-default operation" id="btnMult">   &times;   </button>
    
    <button class="btn btn-default operation" id="btnSub">   &minus;   </button>
    
    <button class="btn btn-default operation" id="btnAdd"> +  </button>


    <button class="btn btn-default btn-large operation" id="btnEq">  =  </button>
    

  </div>
</div>
</div>

              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  height: 100%;
  background: grey;
  display: flex;
  justify-content: center;
   /* radial-gradient(circle, #fff 20%, #ccc); */
  background-size: cover;
  
}

.container {
  display: flex;
  justify-content: center;
  background-color: #ebebeb;
  
  
  margin: 5rem;
 
  
  width: 15rem;
  padding: 2rem;
  border-bottom-right-radius:1rem;
  border-bottom-left-radius:1rem;
  border-top-right-radius:1rem;
  border-top-left-radius:1rem;
  border:0.2rem solid #888;
  box-shadow: 0.2rem 0 1rem 0 rgba(0, 0, 0, .5), inset -0.1rem -0.25rem 0 0.1rem #888;
}
.title{
  display: flex;
  justify-content: center;
  font-weight:bold;
  font-family: 'Roboto Mono', monospace;
  font-size:110%;
  padding:.8rem;
  
}
#footer{
  display: flex;
  justify-content: flex-end;
  font-size:80%
}
a {
  text-decoration:none;
  color:inherit;
}
.visor{
  display: flex;
  justify-content: center;
  padding:.8rem;
  
}
#visor {
     text-align:right;
     border-radius: .3rem;
     width:11rem;
     height:3rem;
     padding:.3rem;
     font-family: 'Roboto Mono', monospace;
      font-size: 110%;
}
.keys{
  display: flex;
  justify-content: center;
}

.row {
  display: flex;
  flex-direction: row;
}

.column {
  display: flex;
  flex-direction: column;
  
}

.btn {
  
  position:relative;
  top:0;
  border-radius: .6rem;
  color: white;
  font-weight: bold;
  
  font-family: Arial, sans-serif;
  margin: .1rem;
  
  border: none;
  outline: none;
  transition: all .5s ease-out;
}

.btn:hover {
  cursor: pointer;
}

.btn:active {
  top:.1em;
  cursor: pointer;
  box-shadow: none;
  outline: none;
}

.btn-default {
  font-size: 120%;
  background-color: #202122;
  padding: .5rem 1rem;
  box-shadow: 0 0 0.28rem 0 rgba(0, 0, 0, .7), inset -0.09rem -0.28rem 0 0.09rem #333;
  background: linear-gradient( to left, #202122, #303030);
}

.btn-red {
  font-size:110%;
  background-color: #991414;
  padding: .5rem .5rem;
   box-shadow: 0 0 0.28rem 0  rgba(0, 0, 0, .7), inset -0.09rem -0.28rem 0 0.09rem #833;
  
}

.btn-large {
  flex-grow: 1;
 
}
              
            
!

JS

              
                $(function() {

    var visor = $('#visor');
  
    var amount = 0;
    var parcel = 0;
    var lastOp = 0;
    var wasOp = true;
    var wasEq = false;
    var number;
    var isDot= false;
    var dotPow;

    // onClick() number buttons
    function digit(ev){
        wasOp = false;
        //if the last click was = , initialize again vv
        if(wasEq){
            amount = 0;
            lastOp=0; // adition 
            wasEq=false;
          
        }
      if (ev.target.id == 'btnDot'){ 
        isDot=true;
        visor.val(parcel + '.');
        dotPow=1;
        return;
      
      }
        //identify which button was clicked
        switch(ev.target.id) {
            case 'btn0':
                number=0;
                
                break;
            case 'btn1':
                number=1;
                break;
            case 'btn2':
                number=2;
                break;
            case 'btn3':
                number=3;
                break;
            case 'btn4':
                number=4;
                break;
            case 'btn5':
                number=5;
                break;
            case 'btn6':
                number=6;
                break;
            case 'btn7':
                number=7;
                break;
            case 'btn8':
                number=8;
                break;
            case 'btn9':
                number=9;
                break;
     
        }
      var temp = parcel;
      if (!isDot){
        	//update parcel according to the sign
        	if(parcel<0)
                    parcel = (parcel * 10 - number);
                else
                    parcel = (parcel * 10 + number);
            }
            else{
            	parcel *= Math.pow(10,dotPow);
              dotPow++;
                if(parcel<0)

                    parcel = (parcel  - number )/ Math.pow(10,dotPow-1);
                else
                    parcel = (parcel  + number)/ Math.pow(10,dotPow-1);

            }
        //update display visor
      if (String(parcel).length >15) parcel = temp;
        
        visor.val(parcel);
    }

    function opera(ev){
      isDot=false;
        //calcular apenas uma vez (evitar vários clicks nos operadores)
        if(!wasOp) {
            compute();
        }
         wasOp=true;
        //atualizar variável de igual para poder utlizar o acumulado
        if(wasEq){
            wasEq=false;
        }
        //identificar qual o botão carregado
        switch(ev.target.id){
            case 'btnAdd':
                lastOp=0;
                break;
            case 'btnDiv':
                lastOp=1;
                break;
            case 'btnMult':
                lastOp=2;
                break;
            case 'btnSub':
                lastOp=3;
                break;
            case 'btnEq':
                wasEq=true;
                break;
        }
    }
// on click AC CE
    function clean(ev) {
      isDot=false;
        switch (ev.target.id) {

            case 'btnCE':
                //limpar parcel
                parcel = 0;
                break;
            case 'btnAC':
                //limpar tudo
                parcel = 0;
                amount = 0;
                lastOp=0;
                break;
        }
        //update display visor
        visor.val(parcel);
    }

    function compute(){

        switch(lastOp) {
            case 0:
                //add
                amount += parcel;
                break;
            case 1:
                //division
                //tratar da divisão por 0
                if(parcel==0) {
                    
                    amount= 0;
                    visor.val("Error");
                    return;
                }
                else
                    amount /= parcel;
                break;
            case 2:
                //multiplicação
                amount *= parcel;
                break;
            case 3:
                //subtração
                amount -= parcel;
                break;
        }
        //limpar parcela e apresentar o acumulado no visor
        parcel = 0;
        if (String(amount).length> 15){
          if (amount >=0)
          amount = amount.toExponential(9) ;
          else
           amount = amount.toExponential(8) ; 
        }
      
        visor.val(amount);
    }

 $('.number').on('click',digit);
  $('.operation').on('click',opera);
  $('.clean').on('click',clean);

});

              
            
!
999px

Console