123

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.

            
              <p id="title"> Calculator </p>
<div id="calculator">
  <div id="screen"><p id="text"> </p> </div>
  
    <div id="cola">
      <button class="btn" name="1"> 1 </button> 
      <button class="btn" name="2"> 2 </button> 
      <button class="btn" name="3"> 3 </button>
      <button class="operation" name="+"> + </button>
      <button class="btn" name="4"> 4 </button>
      <button class="btn" name="5"> 5 </button>
      <button class="btn" name="6"> 6 </button>
      <button class="operation" name="-"> - </button>
      <button class="btn" name="7"> 7 </button>
      <button class="btn" name="8"> 8 </button>
      <button class="btn" name="9"> 9 </button>
      <button class="operation" name="*"> * </button>
      <button class="clear" name="AC"> AC </button>
      <button class="btn" name="0"> 0 </button> 
      <button class="btn" name="."> . </button>
      <button class="operation" name="/"> / </button>
      <button class="equal" name="="> = </button>
  </div>
</div>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Indie+Flower');
#title{
  color: white;
  text-align: center;
  font-size: 7vmin;
  font-family: 'Indie Flower', cursive;
}
div#screen{
  width: 90%;
  height: 40px;
  position: relative;
  top: 20px;
  border-radius: 5px;
  background-color: black;
  margin: auto;
}
body{
  background-color: black;
}
#text{
  padding-top: 8px;
  padding-left: 20px;
  font-size: 20px;
  color: white;
}
#calculator{
  background-color: grey;
  width: 300px;
  height: 350px;
  margin: auto;
  margin-top: 110px;
  border-radius: 20px;
  position: relative;
}
#cola{
  margin-top: 30px;
  height: 270px;
}

#cola button{
  width: 45px;
  height: 34px;
  margin: 13px;
  margin-top: 20px;
  background-color: black;
  background-image: linear-gradient(135deg, white, white 10%, black 50%);
  border-radius: 10px;
  color: white;
  box-shadow: 0px 3px 4px 2px rgba(100,100,100,0.5);
}
button:hover{
  cursor: pointer;
}
button:focus{
  outline: none;
}
.operation:active, .btn:active, .clear:active{
  position: relative;
  top: 2px;
  box-shadow: none !important;
}
 
.equal{
  position: absolute;
  top: 0;
  right: -1px;
  height: 40px !important;
}
            
          
!
            
              /*
Array []									Array[20,+]
1. Click number >>> Create a variable where you store this number 		Number: 
                >>> Display 							Variable: 40
                >>> Add the specific "name" of the button to variable

2. Click operation >>> If there is no number displayed(beginning) do nothing
		   >>> IF the length of the array is 2 then
			add number to array
			asign to array >> operation(array)
			add the current sign to array
                   >>> ELSE add to array the number and the sign
		   >>> Assign number with ""

3. Click = >>> Transform the value into a number and add it to the array
	   >>> Call operation function()
	   >>> number = ""

Create operation function(array);
	
*/
$(document).ready(function() {
  //acumulator for the operation between any 2 numbers
  var acumulator = [];
  
  var number = "";
  
  //Used to determine if there was an '=' finished operation before
  var bool;
  
  //Adds to the current number displayed the digit you just pushed!:)
  //If there was any operation before, finished with an '=' this emptys the current number
  $(".btn").on("click", function(){
    if(bool){
      number = "";
    }
    bool = false;
    var digit = this.name;
    number += digit;
    $("#text").html(number);
  });
  
  //Does some MAGIC for the chain operations 
  $(".operation").on("click", function() {
    if (number !== "") {
      var sign = this.name;
      if(acumulator.length === 2){
        acumulator.push(number);
        acumulator = operate(acumulator);
        acumulator.push(sign);
        number = acumulator[0];
        $("#text").html(number);
      }
      else{
        acumulator.push(number, sign);
      }
    }
    number = "";
  });
  
  //Also MAGIC for the equal operator
  $(".equal").on("click",function(){
    if((number !== "") && (acumulator.length === 2)){
      acumulator.push(number);
      acumulator = operate(acumulator);
      number = acumulator[0].toString();
      $("#text").html(number);
      bool = true;
      acumulator = [];
    }
  });
  
  //All Clear Button
  $(".clear").on("click",function(){
    number = "";
    $("#text").html(number);
  });
  
  //Takes in an array of 3 elements, the sign being the middle one
  //Returns an array with only one element, the result of the operation beetwen the 2 in the beginning
  function operate(arr) {
    var result;
    arr[0] = parseFloat(arr[0]);
    arr[2] = parseFloat(arr[2]);
    switch (arr[1]) {
      case "+":
        result = arr[0] + arr[2];
        arr = [];
        arr[0] = result;
        break;
      case "*":
        result = arr[0] * arr[2];
        arr = [];
        arr[0] = result;
        break;
      case "/":
        result = arr[0] / arr[2];
        arr = [];
        arr[0] = result;
        break;
      case "-":
        result = arr[0] - arr[2];
        arr = [];
        arr[0] = result;
        break;
    }
    return arr;
  }
});
            
          
!
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