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.

            
              <link href='https://fonts.googleapis.com/css?family=Playfair+Display:700italic,400italic' rel='stylesheet' type='text/css'>

<div class="container text-center">
  
<div id="info">
  <h1>Javascript Calculator</h1>
  <i class="fa fa-calculator fa-4x"></i>
</div>
  
 <div id="calculator">
   <!-- to display results and user input-->
   <div id="result"></div>
   <!-- all the buttons-->
   <div class="keys">

   <button class="btn btn-default hover" id="clear" hovertext="Clear">C</button>
   <span id="design" hovertext="Company Name" class="hover">Loreum Ispum</span>
  <button class="btn btn-default hover" id="backspace" hovertext="Backspace">⬅</button><br>
   <button class="btn btn-default number hover" hovertext="#7">7</button>
   <button class="btn btn-default number hover" hovertext="#8">8</button>
   <button class="btn btn-default number hover" hovertext="#9">9</button>
   <button class="btn btn-default operator hover" hovertext="Division">/</button><br>
   <button class="btn btn-default number hover" hovertext="#4">4</button>
   <button class="btn btn-default number hover" hovertext="#5">5</button>
   <button class="btn btn-default number hover" hovertext="#6">6</button>
   <button class="btn btn-default operator hover" hovertext="Mulitiplication">*</button><br>
   <button class="btn btn-default number hover" hovertext="#1">1</button>
   <button class="btn btn-default number hover" hovertext="#2">2</button>
   <button class="btn btn-default number hover" hovertext="#3">3</button>
   <button class="btn btn-default operator hover" hovertext="Subtraction">-</button><br>
   <button class="btn btn-default number hover" hovertext="Decimal Point">.</button>
   <button class="btn btn-default number hover" hovertext="#0">0</button>
   <button class="btn btn-default hover" id="equal" hovertext="Evaluate">=</button>
   <button class="btn btn-default operator hover" hovertext="Addition">+</button>
     </div><!-- end keys-->
 </div>  <!-- end calculator-->
  
  <!-- Footer-->
  <footer>
   <p>&copy;Copyright <span>&hearts;</span> <a href="https://codepen.io/tushar_13/" target="_blank">Tushar Singh</a> 2016.</p>
  </footer>
</div> <!-- end container-->
            
          
!
            
              body {
  background-image: url('https://wallpaperscraft.com/image/points_cubes_background_light_91691_1920x1080.jpg');
  background-size: cover;
  background-attactment: fixed;
  color: white;
}
h1 {
  font-family: 'Playfair Display',sans-serif;
  font-weight: bold;
}
i{
  margin-top:30px;
  margin-bottom:30px;
}
#calculator{
  width:410px;
  height:100%;
  background-color:#fef2e4;
  padding-top:10px;
  padding-bottom:20px;
  margin: 10px auto;
  border-radius:5px;
}

#result {
  width:95%;
  background-color:#bccd95;
  height:50px;
  margin-left:10px;
  margin-bottom:20px;
  border-radius:3px;
  text-align:right;
  box-shadow: inset 0px 6px rgba(0,0,0,.2);
  font-size:25px;
  line-height:50px;
  padding-right:20px;
  text-shadow:1px 1px 3px rgba(0,0,0,.2);
  letter-spacing:2px;
  color: #444;
  
}
.btn{
  width:70px;
  margin-left:10px;
  margin-right:10px;
  margin-bottom:10px;
  background-color: #333;
  color: white;
  border-radius:5px;
  box-shadow: 0px 5px rgba(0,0,0,.2);
  border: none;
  outline: none;
  transition:all 0.2s ease;
}
.keys span {
  font-size:2em;
  font-family: 'Playfair Display',sans-serif;
  font-style:italic;
  padding-right:5px;
  padding-left:5px;
  color: black;
  cursor: pointer;
  text-shadow:4px 4px 3px rgba(0,0,0,.2);
 }
.operator{
  background-color:#ce5a57;
}
#clear, #backspace {
  background-color: teal;
}
/* f4cc70   8d2304   f0810f   cb0000*/
#equal {
  background-color:#f0810f;
}
.btn:hover{
  box-shadow: 0px 5px rgba(0,0,0,.2);
  background-color: white;
  color: black;
}
.btn:active, .btn:focus, .btn:active:focus{
  outline: none; 
  border: none;  
  box-shadow:0px 0px rgba(0,0,0,.2);
}
footer{
  margin-top:30px;
  margin-bottom:5px;
}
footer a {
  color: yellow;
  font-size:1.2em;
}
footer a:hover {
  color:yellow;
}
footer span {
  color: red;
}

            
          
!
            
              //1. get the text from buttons and store it in a variable and display it.
//2. Evaluate when button "equalTo" is pressed
//3. clear text when button "C" is pressed
//4. Use substr and remove the last value from the display when backspace is clicked(length-1)

$(document).ready(function() {
 // only numbers and operators
  $('.keys .number,.keys .operator').on('click', function(){
    //store the text of the button
     var value = $(this).text();
    //append it to the screen 
    $('#result').append(value);
  });// end button
  
  $('#equal').on('click', function(){
    // get text from the screen
    var calc = $('#result').text();
    // evaluate it using JS in-built function eval
    var ans = eval(calc);
    // set the text to ans
    $('#result').text(ans)
  });// end equal
  
  $('#clear').on('click', function(){
    // set the text to nothing when clear is pressed
    $('#result').text('');
  }); // end clear
  
  $('#backspace').on('click', function() {
    var calc =$('#result').text();
    // just want to remove element from last index
    var makeItShort = calc.substr(0, calc.length-1);
    $('#result').text(makeItShort);
  }); // end backspace
  
  
}); // end ready
            
          
!
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