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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>

<div class="container">
  <div class="row mt-5">
    <div class="col-md-12 text-right bg-dark text-success border">
      <h1 id="display" style="height:4rem">0</h1>
    </div>
    <div class="col-md-10">
      <div class="row">
        <button class="btn col-md-10 bg-danger text-center border" id="clear">
          AC
        </button>
        <button class="btn col-md-2 btn-light text-center border" id="divide">/</button>
      </div>
    </div>
    <button class="btn col-md-2 btn-light text-center border" id="multiply">*</button>
    <div class="col-md-10 text-center">
      <div class="row">
        <button class="btn btn-secondary border col-md-4" id="seven">7</button>
        <button class="btn btn-secondary border col-md-4" id="eight">8</button>
        <button class="btn btn-secondary border col-md-4" id="nine">9</button>
      </div>
    </div>
    <button class="col-md-2 text-center btn btn-light border" id="add">+</button>
    <div class="col-md-10 text-center">
      <div class="row">
        <button class="btn btn-secondary border col-md-4" id="four" number="4">4</button>
        <button class="btn btn-secondary border col-md-4" id="five">5</button>
        <button class="btn btn-secondary border col-md-4" id="six">6</button>
      </div>
    </div>
    <button class="col-md-2 text-center btn btn-light border" id="subtract">-</button>
    <div class="col-md-10">
      <div class="row">
        <button class="btn btn-secondary border col-md-4" id="one">1</button>
        <button class="btn btn-secondary border col-md-4" id="two">2</button>
        <button class="btn btn-secondary border col-md-4" id="three">3</button>
      </div>
      <div class="row">
        <button class="btn btn-secondary border col-md-8" id="zero">0</button>
        <button class="btn btn-info border col-md-4" id="decimal">.</button>
      </div>
    </div>
    <div class="col-md-2">
      <div class="row">
        <button class="btn btn-primary col-md-12 text-center py-4 border" id="equals">=</button>
      </div>
    </div>

  </div>
</div>

              
            
!

CSS

              
                body { 
  background: beige;
  font-family: 'courier new';
}
              
            
!

JS

              
                $(document).ready(()=>{
  var eqString = '';
  var result;
  var operation = '';
  var firstNumber = '';
  var secondNumber = '';
  var isInputFinished = false;
  
  $(".btn-secondary").click( function(){
    if (!isInputFinished){
      if ($(this).text() == 0 && firstNumber[0] == 0 && firstNumber.length == 1){
        return console.log(eqString);
      }
      else {
        firstNumber += $(this).text();
      }
    }
    else if (isInputFinished){
      if ($(this).text() == 0 && secondNumber[0] == 0 && secondNumber.length == 1){
        return console.log(eqString);
      }
      else {
        secondNumber += $(this).text();
      }
    }
    eqString += $(this).text();
    $("#display").text(eqString);
  });
  
  $(".btn-light").click(function(){
    if (operation != '' && $(this).text() == '-' && isInputFinished && secondNumber[0] != '-' && secondNumber.length == 0){
      secondNumber = '-'.concat(secondNumber);
      eqString += secondNumber;
    }
    else if (firstNumber == result && secondNumber == ''){
      operation = $(this).text();
      eqString += operation;
    }
    else if (operation != '' && firstNumber != '' && secondNumber != ''){
      isInputFinished = true;
      calculateIt(firstNumber, operation, secondNumber);
      operation = $(this).text();
      eqString += operation;
    }
    else if (isInputFinished && operation != '' &&  $(this).text() != '-'){
      operation = $(this).text();
      eqString += operation;
    }
    else if (!isInputFinished && firstNumber != '' && firstNumber != '-'){
      isInputFinished = true;
      operation = $(this).text();
      eqString += operation;
    }
    else if (!isInputFinished && $(this).text() == '-' && firstNumber == ''){
      firstNumber = '-';
      eqString += firstNumber;
    }
    $("#display").text(eqString);
  });
  
  // Decimal functionality
  $(".btn-info").click(function(){
    if (isInputFinished && !secondNumber.includes(".")){
      eqString += ".";
      secondNumber += ".";
      $("#display").text(eqString);
    }
    else if (!isInputFinished && !firstNumber.includes(".") ){
      eqString += ".";
      firstNumber += ".";
      $("#display").text(eqString);
    }
  })
  
  //Calculation
  $(".btn-primary").click(function(){
    if (firstNumber == '' || secondNumber == '' || operation == ""){
      console.log("Error");
    }
    else {
      calculateIt(firstNumber, operation, secondNumber);
    }
  })
  
  function calculateIt(one, op, two){
      switch (op){
        case '+':
          result = parseFloat(one) + parseFloat(two);
          break;
        case '-':
          result = parseFloat(one) - parseFloat(two);
          break;
        case '/':
          result = parseFloat(one)/parseFloat(two);
          break;
        case '*':
          result = parseFloat(one) * parseFloat(two);
          break;
        default:
          break;
      }
      firstNumber = result;
      eqString = result;
          $("#display").text(eqString);

      secondNumber = '';
          operation = '';

    }
  
  $("#clear").click(function(){
    eqString = '';
    operation = '';
    firstNumber = '';
    secondNumber = '';
    isInputFinished = false;
    $("#display").text('0');
  })
})
              
            
!
999px

Console