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

              
                <div class="main container">
  <div class="wrapper row">
    <!-- Vertically centered content-->
    <main class="col-md-4 col-md-offset-4">
      <header>
        <h1 class="text-center">JavaScript Calculator</h1>
      </header>

      <div class="screen">
        <h2 class="text-right result">0</h2>
      </div>

      <div class="keypad text-center">
        <div class="row">
          <div class="col-xs-3">
            <button class="btn btn-danger" id="AC">AC</button>
          </div>
          <div class="col-xs-3">
            <button class="btn btn-danger" id="CE">CE</button>
          </div>
          <div class="col-xs-3">
            <button class="btn btn-primary operand">/</button>
          </div>
          <div class="col-xs-3">
            <button class="btn btn-primary operand">*</button>
          </div>  
        </div>
        
        <div class="row">
          <div class="col-xs-3">
            <button class="btn btn-primary number">7</button>
          </div>
          <div class="col-xs-3">
            <button class="btn btn-primary number">8</button>
          </div>
          <div class="col-xs-3">
            <button class="btn btn-primary number">9</button>
          </div>
          <div class="col-xs-3">
            <button class="btn btn-primary operand">-</button>
          </div>
        </div>
        
        <div class="row">
          <div class="col-xs-3">
            <button class="btn btn-primary number">4</button>
          </div>
          <div class="col-xs-3">
            <button class="btn btn-primary number">5</button>
          </div>
          <div class="col-xs-3">
            <button class="btn btn-primary number">6</button>
          </div>
          <div class="col-xs-3">
            <button class="btn btn-primary operand">+</button>
          </div>  
        </div>
        
        <div class="row">
          <div class="col-xs-3">
            <button class="btn btn-primary number">1</button>
          </div>
          <div class="col-xs-3">
            <button class="btn btn-primary number">2</button>
          </div>
          <div class="col-xs-3">
            <button class="btn btn-primary number">3</button>
          </div>
          <div class="col-xs-3" id="equals">
            <button class="btn btn-primary">=</button>
          </div>
        </div>
        
        <div class="row" id="lastRow">    
          <div class="col-xs-6" id="zero">
            <button class="btn btn-primary number">0</button>
          </div>
          <div class="col-xs-3" id="decimal">
            <button class="btn btn-primary">.</button>
          </div>
        </div>
        
      </div>
    </main>

    <footer class="col-md-6 col-md-offset-3 col-xs-10 col-xs-offset-1">
      <p class="text-center">by John Alcher</p>
    </footer>

    <!-- End of vertically centered content-->
  </div>
</div>
              
            
!

CSS

              
                /*Vertically centers the content*/

html,
body {
  height: 100%;
  background-color: #00ffd4;
  font-family: 'Roboto Slab', serif;
}

.main {
  height: 100%;
  width: 100%;
  display: table;
}

.wrapper {
  display: table-cell;
  height: 100%;
  vertical-align: middle;
}


/*Design*/

h1,
h2,
h3,
h4,
h5,
h6 {
  font-family: 'Lato', sans-serif;
  font-weight: bolder;
}

main,
.screen {
  margin-bottom: 20px;
}

.screen {
  border: 1px solid black;
  padding: 10px;
  background-color: #f2f2f2;
}

main {
  padding-bottom: 20px;
  background-color: #f3f9bb;
  box-shadow: 10px 10px 5px 0px rgba(0, 0, 0, 0.75);
  border-radius: 2.5%;
}



button {
  font-weight: bold;
  font-size: 18px;
  height: 50px;
  width: 100%;
}

footer {
  font-family: 'Lato', sans-serif;
  font-weight: bolder;
}

footer,
.history {
  color: white;
  text-shadow: 1px 1px 1px rgba(0, 0, 0, 1);
}

.row .row {
  margin-bottom: 10px;
}

/*Adjustments to make the equal sign cover two rows*/
#lastRow {
  margin-top: -60px;
}

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

#equals button {
  height: 100%;
}

/*Media queries*/

@media (max-width:720px){
  html{
    font-size: 12px;
  }
  body{
    margin-top: 10px;
    margin-bottom: 10px;
  }
  main {
    padding:5px;
    margin-bottom: 10px;
  }
  h1{
    font-size: 24px;
  }
}
              
            
!

JS

              
                $(document).ready(function() {
  startApp();
});

function startApp() {
   sequence = []; // Used to store the 
   lastAction = undefined;
  
  /** Clears the previous operations and clicks CE to clear the result. */
  $("#AC").on("click", function() {
    $("#CE").click();
    sequence = [];
    
    return;
  });

  /** Clears current result */
  $("#CE").on("click", function() {
    $(".result").text("0");
    
    return;
  });

  /** Updates the result based on the number pressed */
  $(".number").on("click", function() {
    var number = $(this).text(),
        result = $(".result").text();
    
    
    if (result === "0" || isOperand(result) || lastAction == "equals"){
      $(".result").text(number);
      lastAction = "number";

      return;
    } 
    $(".result").text(result += number);

    return;
            
  });

  /** Handles decimal placements*/
  $("#decimal").on("click", function(){
    var resultElement = $(".result"),
        result = resultElement.text();
    
    if (/\./.test(result) || isOperand(result)){
      return;
    }
    
    result += ".";
    resultElement.text(result);
  });
  
  /** Populates the sequence array with the current result and the operand clicked*/
  $(".operand").on("click", function(){
    var operand = $(this).text(),
        resultElement = $(".result"),
        result = resultElement.text();
      
    if (isOperand(result) || result === "0" ){
      return;
    }
    
    else if (lastAction == "equals"){
      sequence[1] = operand;     
      resultElement.text(operand);
      lastAction = "operand";
      
      return;
    }
    
    sequence.push(resultElement.text(), operand);
    resultElement.text(operand);
    
    return;
    
  });
  
  /** 
   * Applies .eval() to sequence, reducing it to a single value
   * Displays the value as the result
   */
  $("#equals").on("click",function(){
    var resultElement = $(".result"),
        result = resultElement.text(),
        total;
    
    if (isOperand(result)){
      return;
    }
    sequence.push(result);
    total = eval(sequence.join(""));
    resultElement.text(total);
    sequence = [total, "+"];
    
    lastAction = "equals";
    
    return;
  });
  
  /** returns a {boolean}, determining if @result {string} is an arithmetic operator */
  function isOperand(result){
    return( 
          result === "+" ||
          result === "-" ||
          result === "*" ||
          result === "/" );
  }
}
              
            
!
999px

Console