css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <body>
<div>
  <div id="calculator"><!-- calculator START -->
      
    <!-- Title -->
    <div id="title">
     <h5><b>CALCULATOR</b></h5>
    </div>
                  
   <!-- Show dialog START -->
   <div id="showDialog">
     <p id="screen"></p>   
   </div>
   <!-- Show dialog STOP -->
    
  <div id="buttons"><!-- buttons START -->
        
    <div class="container buttons">
      <div class="row"><!-- NEW ROW -->
        <input type="button" class="realButtons clean" value="C" id="C"/>
        <input type="button" class="realButtons clean" value="CE" id="CE"/>
        <input type="button" class="realButtons oper" value="/" id="divide"/>
        <input type="button" class="realButtons oper" value="*" id="product"/>
      </div>
      <div class="row"><!-- NEW ROW -->
        <input type="button" class="realButtons number" value="7" id="7"/>
        <input type="button" class="realButtons number" value="8" id="8"/>
        <input type="button" class="realButtons number" value="9" id="9"/>
        <input type="button" class="realButtons oper" value="-" id="subtract"/>
      </div>
      <div class="row"><!-- NEW ROW -->
        <input type="button" class="realButtons number" value="4" id="4"/>
        <input type="button" class="realButtons number" value="5" id="5"/>
        <input type="button" class="realButtons number" value="6" id="6"/>
        <input type="button" class="realButtons oper" value="+" id="plus"/>
      </div>
      <div class="row"><!-- NEW ROW -->
        <input type="button" class="realButtons number" value="1" id="1"/>
        <input type="button" class="realButtons number" value="2" id="2"/>
        <input type="button" class="realButtons number" value="3" id="3"/>       
      </div>
    <div class="row"><!-- NEW ROW -->
      <input type="button" class="realButtons number" value="0" id="zero"/>
      <input type="button" class="realButtons number" value="." id="dot"/>
      <input type="button" class="realButtons oper" value="=" id="equal"/>
    </div>      
  </div>
    
  
  </div> <!-- buttons STOP -->
  </div> <!-- buttons calculator -->
</div>
</body>
            
          
!
            
              body {
  margin: 10% 30% 30% 30%;
  transition: background 2s;
  font-family: 'Orbitron', sans-serif;
  background-color: #818181;
}

#title{
  padding-top: 10px;
  padding-bottom: 10px;
  text-align: center;
}

#calculator {
  height: 440px;
  width: 300px;
  margin-top: 10%;
  margin-left: auto;
  margin-right: auto;
  background-color: #dfd8d0;
  border: 2px solid #908b85;
  border-radius: 20px;
  box-shadow: 7px 10px 34px 1px rgba(0, 0, 0, 0.68), inset -1px -6px 12px 0.1px #89847e;
}

#showDialog{
  text-align: right;
  border-style: groove;
  width: 85%;
  height: 65px;
  margin-left: auto;
  margin-right: auto;
  background-color: #c3c2ab;
  border: 2px solid #908b85;
  border-radius: 8px;
  font-size: 22px;
  position: relative;
}

.realButtons{
  cursor: pointer;
  height: 50px;
  width: 50px;
  margin: 2px 7px;
  padding: 14px 14px;
  color: white;
  background-color: #3a3a3a;
  border: none;
  border-radius: 5px;
  text-align: center;
}

.buttons{
  font-size: 16px;
  font-weight: bold;
  position: absolute;
  display: inline-block;
  margin-top: 15px;
  margin-left: 15px;
  height: auto;
  width: 300px;
  
}

.clean{
  background-color: #b32d00;
}

#screen{
  color: black;
  font-size: 30px;
  margin-right: 10px;
  margin-top: 5px;
}

#zero{
  width: 114px;
  margin-left: 8px;
}

#equal{
  margin-left: 6px;
  margin-top: -52px;
  height: 104px;  
}
            
          
!
            
              //magic to be.
$(document).ready(function() {
  var buttonPressed; //current button pressed.
  var firstNum = []; //first array value.
  var secondNum = []; //second array value.
  var first = 0; //first number value.
  var second = 0; //second number value.
  var operator = ""; // value of the operator( /, *, -, + or  =).
  var array = ["/", "*", "-", "+"]; //array with oparator values;
  var lockedButton = true; //"switch" to change from puting values from the first array to the second.

  //real calculator.
  var performCalc = function(one, two, opera) {
    
    //if(one.indexOf(".")>=0){
      //one = one * 10;
      //console.log("inside perfcalc");
    //}
    //else if(two.indexOf(".")>=0){
      //two = two * 10;
    //}
    switch (opera) {
      case '*':
        first = one * two;
        secondNum = [];
        break;
      case '/':
        first = one / two;
        secondNum = [];
        break;
      case '+':
        first = one + two;
        secondNum = [];
        break;
      case '-':
        first = one - two;
        secondNum = [];
        break;
    }
    console.log("x is: " + x);
    var x = first.toString();
    //makes total result equal to 15 digits.
    if(x.length > 14){
      first = x.substring(0,14);
      $("#screen").text(first);
      console.log("total is = " + first);
    }
    else{
      $("#screen").text(first);
      console.log("total is = " + first);
    }
  };

  //on C or CE click.
  $('.clean').on('click', function(evt) {
    // reseting the locked button in order to start the firstNum again.
    buttonPressed = $(this).val();
    if (buttonPressed == "C") {
      lockedButton = true;
      firstNum = [];
      secondNum = [];
      operator = "";
      $("#screen").text(0);
    } else if (buttonPressed == "CE") {
      //if the user didnt gave the operator a value it clears the first number.
      if (operator === "") {
        firstNum = [];
        $("#screen").text(0);
      } else {
        secondNum = [];
        $("#screen").text(0);
      }
    }
  });

  //on number click.
  $('.number').on('click', function(evt) {
    buttonPressed = $(this).val();
    console.log("button pressed = " + buttonPressed);

    // every time the locked button equal true    
    if (lockedButton) {
      //makes unable for a value to start with 0(05) or the screen to show more than one.
      if (buttonPressed === "0" && firstNum.length === 0) {
        $("#screen").text(buttonPressed);
      }
      else {
        //doesnt allow the user to press more than 1 dots (.).
        if (buttonPressed == "."){
          if (firstNum.indexOf(".") == -1) {
            secondNum = [];
            //if the first button the user is pressing is '.' then it turns it to '0.'
            if(firstNum.length === 0){
              firstNum.push("0");
            }
            //sets max length of characters on the screen to 15.
            if(firstNum.length < 15){
              firstNum.push(buttonPressed);
            }
            $("#screen").html(firstNum.join(""));
            //console.log("firstNum is equal = " + firstNum);
          }
        }
        else{
          secondNum = [];
          //sets max length of characters on the screen to 15.
          if (firstNum.length < 15) {
            firstNum.push(buttonPressed);
          }
          $("#screen").html(firstNum.join(""));
          //console.log("firstNum is equal = " + firstNum);
        }
      }
    }
    // Locked Button equal to false when user press an operator
    if (lockedButton === false) {
      //makes unable for a value to start with 0(05) or the screen to have more than 1 0.
      if (buttonPressed === "0" && firstNum.length === 0) {
        $("#screen").text(buttonPressed);
      }
      else {
        //doesnt allow the user to press more than 1 dots (.).
        console.log("im in second num");
        if (buttonPressed == ".") {
          if (secondNum.indexOf(".") == -1) {
            //if the first button the user is pressing is '.' then it turns it to '0.'
            if(secondNum.length === 0){
              secondNum.push("0");
            }
            //sets max length of characters on the screen to 15.
            if (secondNum.length < 15) {
              secondNum.push(buttonPressed);
            }
            $("#screen").html(secondNum.join(""));
            //console.log("secondNum is equal = " + secondNum);
          }
        } else {
          //sets max length of characters on the screen to 15.
          if (secondNum.length < 15) {
            secondNum.push(buttonPressed);
          }
          $("#screen").html(secondNum.join(""));
          //console.log("secondNum is equal = " + secondNum);
        }
        console.log("second num: " + secondNum);
      }
    }
  });

  //on ( /, *, -, +, =) click.
  $('.oper').on('click', function(evt) {
    lockedButton = false; //the moment an operator is pressed.That means first number has a value.
    buttonPressed = $(this).val();

    //if buttonPressed is an operator.
    if (array.indexOf(buttonPressed) != -1) {
      //if operator wasnt used.
      if (operator === "") {
        first = parseFloat(firstNum.join(""));
        console.log("first is equal = " + first);
        operator = buttonPressed;
        console.log("operator is equal = " + operator);
        $("#screen").text(operator);
      } else {
        //will run only if secondNum has a value.
        if (secondNum.length !== 0) {
          second = parseFloat(secondNum.join(""));
          console.log("second is equal = " + second);
          performCalc(first, second, operator);
        }
        operator = buttonPressed;
        console.log("first: " + first + " second: " + second + " operator: " + operator);
        $("#screen").text(operator);
      }
    }
    //if buttonPressed is =.
    else if (buttonPressed == "=") {
      if (secondNum.length !== 0) {
        second = parseFloat(secondNum.join(""));
        console.log("second is equal = " + second);
      }
      performCalc(first, second, operator);
      console.log("first: " + first + " second: " + second + " operator: " + operator);
    }
  });

  //shows 0 on screen when nothing else is pressed.
  $("#screen").text(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.
Loading ..................

Console