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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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">
  <div class="row"><div class="col-sm-12 text-center"> <h4>Electronic Calculator </h4></div></div>
  <div class="row"><div id="result" class="col-sm-12">  </div></div>
  <div class="row"><div id="calculate" class="col-sm-12"></div></div>
  <div class="row"><div class="col-sm-3 AC" id="AC" Value="AC">AC</div><div class="col-sm-3 operator" value="+/-">+/-</div><div class="col-sm-3 operator" value="%">%</div><div class="col-sm-3 operator" value="/">/</div></div>
  <div class="row"><div class="col-sm-3 operand" value="7">7</div><div class="col-sm-3 operand" value="8">8</div><div class="col-sm-3 operand" value="9">9</div><div class="col-sm-3 operator" value="*" >X</div></div>
  <div class="row"><div class="col-sm-3 operand" value="4">4</div><div class="col-sm-3 operand" value="5">5</div><div class="col-sm-3 operand" value="6">6</div><div class="col-sm-3 operator" value="-">-</div></div>
  <div class="row"><div class="col-sm-3 operand" value="1">1</div><div class="col-sm-3 operand" value="2">2</div><div class="col-sm-3 operand" value="3">3</div><div class="col-sm-3 operator" value="+">+</div></div>
  <div class="row"><div class="col-sm-6 operand zero" value="0">0</div><div class="col-sm-3 operand" value=".">.</div><div class="col-sm-3 equal" value="=">=</div></div>
  
</div>
<hr>
<footer class="footer"><br>
      <p>&copy; 2017 <a href="https://codepen.io/SonaliS/">sonali</a>.</p>
    </footer>
              
            
!

CSS

              
                body{
  width:  100%;
}
.container {
  width:300px;
  align:center;
  margin: auto;
  font-size: 30px;
}
#result {
  border:  solid 1px #000000;
   background-color: lightblue;
  height: 65px;
 
} 
#calculate {
  border:  solid 1px #000000;
  background-color: lightgray;
  color: #F7F7F7;
  height: 35px;
  font-size: 20px;
}
.operator, .equal , .AC{
  height: 60px;
  width:25%;
  border: solid black 1px;
  background-color:  #FF8C00;
  text-align: center;
 
}
.operand{
  height: 60px;
  width:25%;
  border: solid black 1px;
  background-color:  #f7f7f7;
  text-align: center;
 
}
.zero {
  width:50%;
}
              
            
!

JS

              
                

$(document).ready(function(){
  var result = 0;
  var prev ="";
  var operator;
  var operand;
  var reset = "F";
  
  function resetAll(){
  // reset the display, the input and result
    $("#calculate").empty();
    $("#result").empty();
    result = 0;
    prev ="";
    operator = "";
    operand = "";
    reset = "F";
 }
  
  $("#AC").click(function(){
    // reset the display, the input and result
    resetAll();
  });
  
  $(".operand").click(function(){
    if (reset ==="T"){
      resetAll();
    }
    $("#calculate").append($(this).attr("value"));
  });
  
  $(".operator, .equal").click(function(){
    //console.log("prev:"+prev);
     if (reset ==="T"){
       reset ="F";  // we will use the  result of the previous calculation as operand as the user has selected an operation to perform  on it.
     }
    if (prev == ""){
      prev = $("#calculate").html();     
      result = prev;
    }  else {
      operand = $("#calculate").html().slice(prev.length);
       console.log("operand:"+operand);
      if(operator === "+"){
        result = parseFloat(result) + parseFloat(operand);
      } else if(operator === "-"){
        result = parseFloat(result) - parseFloat(operand);
      }else if(operator ==="*"){
        result = parseFloat(result) * parseFloat(operand);
      } else if(operator === "/"){
        result = parseFloat(result) / parseFloat(operand);
      } else if(operator === "%"){
        result = parseFloat(result) % parseFloat(operand);
      } 
    }
    
    operator=$(this).attr("value");
    //console.log(operator);
    if(operator ==="+/-"){
        //console.log("in +/-");
        result = parseFloat(result) * -1;
     } 
    
    //display the new result
    $("#result").empty().append(result);
    if(operator != "="){      
        $("#calculate").append(operator);
         prev = $("#calculate").html();
    } else {  
       $("#calculate").empty().append(result);
       prev = result;
       reset = "T";
    }  
    
  });
});
              
            
!
999px

Console