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 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

              
                <h1 class="heading text-center">Javascript Calculator</h1>
<div id="calculator" class="center-block">
  <div id="screen"></div>
   
  <div class="row1">
      <button type="button" class="btn btn-info btn-lg" id="clear">AC</button>
      <button type="button" class="btn btn-info btn-lg" id="sign">+/-</button>
      <button type="button" class="btn btn-info btn-lg" id="percent">%</button>
      <button type="button" class="btn btn-success btn-lg" id="divide">/</button>
  </div>
      <div class="row2">
      <button type="button" class="btn btn-warning btn-lg" id="seven">7</button>
      <button type="button" class="btn btn-warning btn-lg" id="eight">8</button>
      <button type="button" class="btn btn-warning btn-lg" id="nine">9</button>
      <button type="button" class="btn btn-success btn-lg" id="multiply">*</button>
  </div>
   <div class="row3">
      <button type="button" class="btn btn-warning btn-lg" id="four">4</button>
      <button type="button" class="btn btn-warning btn-lg" id="five">5</button>
      <button type="button" class="btn btn-warning btn-lg" id="six">6</button>
      <button type="button" class="btn btn-success btn-lg" id="subtract">-</button>
  </div>
      <div class="row4">
      <button type="button" class="btn btn-warning btn-lg" id="one">1</button>
      <button type="button" class="btn btn-warning btn-lg" id="two">2</button>
      <button type="button" class="btn btn-warning btn-lg" id="three">3</button>
      <button type="button" class="btn btn-success btn-lg" id="add">+</button>
  </div>
  <div class="row5">
      <button type="button" class="btn btn-warning btn-lg" id="zero">0</button>
      
      <button type="button" class="btn btn-warning btn-lg" id="decimal">.</button>
      <button type="button" class="btn btn-danger btn-lg" id="equal">=</button>
  </div>
  </div>
  </div>
  

              
            
!

CSS

              
                body{
  background-image: url('');
}
.heading{
  color:black;
  text-shadow: 2px 2px 8px gray;
}
#calculator{
    width: 320px;
    height: 470px;
    position: fixed;
    top: 55%;
    left: 50%;
    transform: translate(-50%, -50%);
    border: 25px solid black;
    border-width: 2px;
    background-color: black;
    border-radius:25px
    
}
#screen{
    width: 300px;
    height: 70px;
    position: fixed;
    top: 12%;
    left: 50%;
    transform: translate(-50%, -50%);
    border: 25px solid black;
    border-width: 2px;
    background-color:#C0C0C0;
    border-radius:25px;
    text-align:right;
    font-size:46px;
}
.row1{
    top:30%;
    margin-top:30%;
    
    
}
.btn-lg{
  font-size:30px;
  margin:5px;
}
#seven,#eight,#four,#five,#one,#two,#zero{
  width:74px;
  
}
#nine,#six,#three{
  width:64px;
}
#decimal{
  width:74px;
}
#equal{
  width:124px;
}
#add{
  width:46px;
}
              
            
!

JS

              
                var firstNumber=0; //firstinput
var secondNumber=0;//secondinput
var operation=null; //set to false
var total=0;
//checks for length and changes it to exponential if needed
function displayscreen()
{
    if (parseFloat(firstNumber).toString().length<13){
        document.getElementById('screen').innerHTML=firstNumber;}
        
    else{
        document.getElementById('screen').innerHTML=firstNumber.toExponential(2);}
    
   // alert("Hello! I am an alert box!!");
}

//x being input
function checkInput(x)
{
  console.log("checkinput beggining:firstNumber: " + firstNumber);
  console.log("checkinput beggining:secondNumber: " + secondNumber);
  console.log("checkinput beggining:operation: " + operation);  
  
  if (parseFloat(firstNumber).toString().length < 13) 
    { 
        if (parseInt(x) >= 0 && parseInt(x) <= 9) 
        { 
             if (firstNumber.toString() == '0') 
             {
                 firstNumber = x.toString(); 
             }
       
        else 
        {
            firstNumber = firstNumber.toString() + x.toString(); 
        }
    }
    
    if (x == '.' && (firstNumber.toString().indexOf('.') == -1)) 
    { 
      firstNumber = firstNumber.toString() + x.toString();
    }
    displayscreen();
  }
console.log("checkinput end:firstnum: " + firstNumber)
console.log("checkinput end:secondnum: " + secondNumber)
console.log("checkinput end:operator: " + operation)

}

function signsclicked(y)
{
    
    if (operation == undefined)
    {
        console.log(y); 
        operation=y;
         secondNumber=firstNumber;
        firstNumber=0;
    }
    else
    {
        console.log(y);
        evalResult(operation);
        operation=y;
        secondNumber=firstNumber;
        firstNumber=0;
        
        
    }
    
}

function evalResult(math) 
{
  console.log("checkinput beginning:operator: " + math)
  if (math == '*') {
    firstNumber = firstNumber * secondNumber;
    secondNumber=0;
    operation= null;
    
  }
  if (math == '/') {
    firstNumber = secondNumber / firstNumber;
    secondNumber=0;
    operation= null;
  }
  if (math == '+') {
    firstNumber = parseFloat(firstNumber) + parseFloat(secondNumber);
    secondNumber=0;
    operation= null;
  }
  if (math == '-') {
    firstNumber = secondNumber - firstNumber;
    secondNumber=0;
    operation= null;
  }
  console.log("checkinput end:operator: " + math)
  displayscreen();
  
}

function clearall()
{
    firstNumber=0;
    operation=null;
    secondNumber=0;
    displayscreen();
    
}

function changesign()
{
    firstNumber=-firstNumber;
    displayscreen();
    
}

function changetodecimal(z)
{
    firstNumber=firstNumber/100;
    displayscreen();
}

//button properties
$(document).ready(function() {
  displayscreen();
  $("#zero").click(function() {
    checkInput(0);
  });
  $("#one").click(function() {
    checkInput(1);
  });
  $("#two").click(function() {
    checkInput(2);
  });
  $("#three").click(function() {
    checkInput(3);
  });
  $("#four").click(function() {
    checkInput(4);
  });
  $("#five").click(function() {
    checkInput(5);
  });
  $("#six").click(function() {
    checkInput(6);
  });
  $("#seven").click(function() {
    checkInput(7);
  });
  $("#eight").click(function() {
    checkInput(8);
  });
  $("#nine").click(function() {
    checkInput(9);
  });
  $("#decimal").click(function() {
    checkInput('.');
  });
  $("#number1").click(function() {
    (1);
  });
  $("#add").click(function() {
    signsclicked('+');
  });
  $("#subtract").click(function() {
    signsclicked('-');
  });
  $("#divide").click(function() {
    signsclicked('/');
  });
  $("#multiply").click(function() {
    signsclicked('*');
  });
  $("#equal").click(function() {
    evalResult(operation);
  });
  $("#sign").click(function() {
    changesign('+/-');
  });
  $("#clear").click(function() {
    clearall('AC');
  });
  $("#percent").click(function() {
    changetodecimal('%');
  });
  
  
});
  
              
            
!
999px

Console