Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <html>
<head>
  <title>Calculator</title>
</head>
<body onload="zeroStart()">  
  <div id="calcBase" class= "container">   
    <div id="padDiv">
      <h1 class="text-center">CALCULATOR</h1>
      <div id="screen" class="text-right">
          <p id="disNum"></p>
          <p id="disString"></p>
      </div>
        <button onclick="ACFunc()" id="AC">AC</button>
        <button onclick="CEFunc()" id="CE">CE</button>
        <button onclick="dviFunc()" id="dvi">&#247;</button>
        <button onclick="multFunc()" id="mult">&#215;</button>
      
        <button onclick="sevenFunc()" id="seven">7</button>
        <button onclick="eightFunc()" id="eight">8</button>
        <button onclick="nineFunc()" id="nine">9</button>
        <button onclick="minusFunc()" id="minus">-</button>
      
        <button onclick="fourFunc()" id="four">4</button>
        <button onclick="fiveFunc()" id="five">5</button>
        <button onclick="sixFunc()" id="six">6</button>
        <button onclick="plusFunc()" id="plus">+</button>
        
        <button onclick="oneFunc()" id="one">1</button>
        <button onclick="twoFunc()" id="two">2</button>
        <button onclick="threeFunc()" id="three">3</button>
      
        <button onclick="equalFunc()" id="equal">=</button>     
        <button onclick="zeroFunc()" id="zero">0</button>
        <button onclick="decPFunc()" id="decP">.</button>   
    </div><!--padDiv-->             
  </div><!--calcBase--> 
</body>   
</html>  
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Orbitron);

body{
  background: grey;
}

#calcBase{
  margin-top:80px;
  margin-bottom:80px;
  background:white;
  height:445px;
  width:300px;
  border-radius:30px;
}

#padDiv{
  height:390px;
  width:250px;
  margin:auto;
}

h1,p,button {
  font-family: 'Orbitron', sans-serif;
}

h1{
  font-size:20px;
  font-weight:bold;
}

#screen{
  background: lightgrey;
  width:243px;
  height:55px;
  margin: 0px auto 10px auto;
  border-radius:5px;
  border-style:solid;
  border-width:2px;
  border-color:grey;
}

p{
  font-size:30px;
}

#disNum{
  padding-bottom:0px;
  margin-bottom:0px;
  padding-right:5px;
}
#disString{
  padding-top:0px;
  padding-right:8px;
  margin-top:0px;
  line-height:4px;
  font-size:12px;
  color:darkestgrey;
}

button{
  float:left;
  margin: 5px 5px 5px 5px;
  padding: 5px 0px 5px 0px;
  height:50px;
  width:52.5px;
  line-height:1px;
  font-weight:bold;
  font-size:20px;
  appearance: none;
  box-shadow:none;
  border-radius:0;
  color: #fff;
  background-color: #6496c8;
  border: none;
  border-radius: 7px;
  box-shadow: 0 5px #27496d;
}

button:active{
  background-color: #6496c8;
  box-shadow: 0 0px #27496d;
  transform: translateY(5px);
}

button:focus {
  outline:none;
}

#zero{
  width: 115px;
}

#equal{
  float:right;
  height:110px;
}

            
          
!
            
              //Stores the first string for the display screen
var entry= "0";
//Stores the second string for the display screen
var sum = "0";
//Stores calcuation result
var calc = 0;
//For checking if certain buttons have been pressed or not
var opPressed = false;
var cePressed = false;
var decPPressed = false;
var eqPressed = false;

//Displays 0 on the screen when the document loads
function zeroStart(){document.getElementById("disNum").innerHTML = entry; document.getElementById("disString").innerHTML = sum;}

//Checks if the strings have reached a maximum length before being too long to fit on the display screen
function max(){
  if (entry.length > 8 || sum.replace(/\&#247;|\&#215;/g, " ").length >= 23){
    console.log(sum);
    return true;
  }else{
    return false;
  }
}

//For clearing the screen
function limit(){
      entry = "0";
      sum = "0"
      calc = 0;
      document.getElementById("disNum").innerHTML = "0";
      document.getElementById("disString").innerHTML ="Limit";
}

//Resets the calculator
function ACFunc(){
  //Resets final result  
  calc = 0;
  //Turns the operator switch off
  opSwitch = false;
  //Turns the decimal point switch off
  decPPressed = false;
  //Turns the equal button swith off
  eqPressed = false;
  //Clears strings
  entry = "0";
  sum = "0";
  document.getElementById("disNum").innerHTML = entry;
  document.getElementById("disString").innerHTML = sum;
}

//Removes last entry
function CEFunc(){
  //Turns the button pressed switches off
  opPressed = false;
  cePressed = true;
  decPPressed = false;
  entry = "0";    
  //Removes an operator or group of numbers from display string 2
  sum = sum.replace(/\+$|\-$|\&#247;$|\&#215;$|\d+\.\d+$|\d+$|\.$|\d+\.$|^\-\d+$|^\-\d+\.\d+$/,"");
  //A zero remains if CE removes everything from display string 2  
  if(sum == ""){
      sum = "0";
    }
  document.getElementById("disNum").innerHTML = entry;
  document.getElementById("disString").innerHTML = sum;
}

//Activates functions when their buttons are pressed
function oneFunc(){numFunc("1");}
function twoFunc(){numFunc("2");}
function threeFunc(){numFunc("3");}
function fourFunc(){numFunc("4");}
function fiveFunc(){numFunc("5");}
function sixFunc(){numFunc("6");}
function sevenFunc(){numFunc("7");}
function eightFunc(){numFunc("8");}
function nineFunc(){numFunc("9");}
function zeroFunc(){naughtFunc("0");}

function plusFunc(){operFunc("+");}
function minusFunc(){operFunc("-");}
function dviFunc(){operFunc("&#247;");}
function multFunc(){operFunc("&#215;");}

//Adds numbers to both strings on the screen
function numFunc(num){  
  if (max() == false){    
    //Clears zeros from the display screen when a number is pressed or after the equal button has been pressed
    if(entry == "0" && sum == "0" || calc === "0" && eqPressed == true){
     entry="";
     sum="";
     calc = 0;
     eqPressed = false; 
    } 
    //Clears zero from string 1 on the screen if the CE button has been pressed
    if(entry == "0" && cePressed == true){entry=""};
    //Turns the operator switch off
    opPressed = false; 
    //Removes operator if on string 1
    entry = entry.replace(/\+|\-|\&#247;|\&#215;/g, "");
    //Adds entered number onto the display strings
    if (cePressed == false){
      entry += num;
      sum += num;
      {document.getElementById("disNum").innerHTML = entry;}
      {document.getElementById("disString").innerHTML = sum;}
    } 
    //Only adds numbers after CE has been pressed if the last character is not a number
    else if (/\d$/.test(sum) !== true && cePressed == true){
      cePressed = false;
      entry += num;
      sum += num;
      {document.getElementById("disNum").innerHTML = entry;}
      {document.getElementById("disString").innerHTML = sum;}
    }
  }else{
    limit();
  }  
}

//Inserts operators into display strings 1 and 2
function operFunc(op){
  //For being able to insert calculations after the equal button has been pressed
  if (eqPressed == true){
    eqPressed = false;
    sum = calc;
    calc = 0;
   }
  
  if (max() == false){
    decPPressed = false;
    //Removes decimal point if there are no numbers after it 
    if (/\.$/.test(sum) == true){
      sum = sum.slice(0, -1);
    } 
    //Removes all zero's as well as the decimal point if there are no decimal numbers above 0 
    if (/\.0+$/.test(sum) == true){  
      sum = sum.replace(/\.0+$/, "");
    } 
    //Removees extra 0's if there are any after decimal numbers
    if (/\./g.test(sum) == true && /0+$/.test(sum) == true){
      sum = sum.replace(/0+$/, "");
    } 
    //Only adds an operator to string 1 during a calculation if a zero and an operator is not already on the display screen
    if(entry !== "0" && opPressed == false && sum !== "0"){
      //Inserts an operator into display string 1
      entry = op;      
      {document.getElementById("disNum").innerHTML = entry;}
      //Only adds one operator to string 2 if string 2 is not 0 and the operator switch is off 
     if(opPressed == false && sum !== "0"){
       opPressed = true;
       sum += op;
       {document.getElementById("disString").innerHTML = sum;}
     }      
      //If the CE button has been pressed with 0 now on display string 1 and there isn't already an operator on the display string 2, an operator should still be able to be entered    
    }
    if(cePressed == true && opPressed == false && sum !== "0" && /\d$/.test(sum) == true){
      opPressed = true;
      entry = op;      
      {document.getElementById("disNum").innerHTML = entry;}
      sum += op; 
      {document.getElementById("disString").innerHTML = sum;}
    }
  }else{
    limit();
  }
}

//For when the zero button is pressed
function naughtFunc(zero){
if (max() == false){    
  //Only if there a number is pressed already should zero be able to be entered on the screen
  if(entry !== "0" && opPressed == false){
      entry += zero;
      {document.getElementById("disNum").innerHTML = entry;}
      sum += zero;
      {document.getElementById("disString").innerHTML = sum;}
    }
    //Adds more zeros after the decimal point button has been pressed
    else if(decPPressed == true){   
      entry += "0";
      {document.getElementById("disNum").innerHTML = entry;}    
       sum += "0";
      {document.getElementById("disString").innerHTML = sum;}    
     }
  }else{
    limit();
  }
}

function decPFunc(){
  if (max() == false){ 
    //Inserts a decimal point into the display strings
    if (decPPressed == false && opPressed == false && cePressed == false){    
      decPPressed = true;    
      entry += ".";
      {document.getElementById("disNum").innerHTML = entry;}    
       sum += ".";
      {document.getElementById("disString").innerHTML = sum;}   
     }
    //if there is an operator on the screen, place zero in front of decimal point when its button is pressed
    else if (decPPressed == false && opPressed == true && cePressed == false){
        opPressed =false;
        decPPressed = true;    
        entry = "0.";
        {document.getElementById("disNum").innerHTML = entry;}    
         sum += "0.";
        {document.getElementById("disString").innerHTML = sum;}     
      } 
    //A decimal point can't be entered next to an already entered number after the CE button has been pressed
    else if (decPPressed == false && /\d$/.test(sum) !== true && cePressed == true){
      cePressed = false;
      decPPressed = true;
      entry = "0.";
      {document.getElementById("disNum").innerHTML = entry;}    
       sum += "0.";
      {document.getElementById("disString").innerHTML = sum;}    
    }
    
    //Decimal point button works after the screen is cleared with CE
    else if (decPPressed == false && opPressed == false && sum =="0" && cePressed == true){
      cePressed = false;
      decPPressed = true;
      entry += ".";
      {document.getElementById("disNum").innerHTML = entry;}    
       sum += ".";
      {document.getElementById("disString").innerHTML = sum;}    
    }
  }else{
    limit();
  }
}

//Converts the sum string into intergers and arithmetic opertators, returns the result of the calculation back as a string to display screen one. The result can still then be used for further calculations
function equalFunc(){
  if (/\d$/.test(sum) == true){
  //Replaces html operator codes with js operator strings, inserts spaces beside operators and then splits the string into an array from those spaces.
   sum = sum.replace(/\&#247;/g, "/").replace(/\&#215;/g, "*").replace(/(\/)/g, " $1 ").replace(/(\*)/g, " $1 ").replace(/(\+)/g, " $1 ").replace(/((?!^)\-)/g, " $1 ").split(" ");
   
      for(var i = 0; i < sum.length; i++){
          //Adds the first number into the calculation
          if (i == 0 ){
            calc += parseFloat(sum[i]);
          }
          //Divide calculation by next number
          if (/\//.test(sum[i]) == true){
            calc /= parseFloat(sum[i + 1]);
          }  
          //Multiply calculation by next number
          if (/\*/.test(sum[i]) == true){
            calc *= parseFloat(sum[i + 1]);
          }  
         //Add calculation by next number
          if (/\+/.test(sum[i]) == true){
            calc += parseFloat(sum[i + 1]);
          }
          //Minus calculation by next number
          if (/^\-$/.test(sum[i]) == true){
            calc -= parseFloat(sum[i + 1]);
          }  
      }
      //Rounds off the calcuation into a string with limited decimals and removes excess zeros
      calc = calc.toFixed(2).replace(/\.0+$|0+$/,"");
      //Clears the screen of the result is too long
      if (calc.length > 8){ 
        limit();        
      }
      //Otherwise inserts the result onto the display screen
      else{
        document.getElementById("disNum").innerHTML = calc;
        document.getElementById("disString").innerHTML +="="+calc;
        eqPressed = true;
        //Resets the display strings if the result is zero
        if (calc ==="0"){
            entry = "0";
            sum = "0";
        }
    }  
  }
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console