cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <body>
  <content>

      <div class="container" id="calc">




          <section class="row">

            <div class="col-md-9 col-sm-9 col-xs-9">
                <input type="text" class="form-control" id="display" value="0">
            </div>
            <div class="col-md-3 col-sm-3 col-xs-3">
                <button type="button" class="btn btn-danger btn-md btn-block" id="reset">C</button>
            </div>


          </section>


          <section class="row">

                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-info btn-md btn-block" id="char7" value="7">7</button>
                  </div>
                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-info btn-md btn-block" id="char8" value="8">8</button>
                  </div>
                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-info btn-md btn-block" id="char9" value="9">9</button>
                  </div>
                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-warning btn-md btn-block" id="mul" value="mul">x</button>
                  </div>

          </section>

          <section class="row">

                  <div class="col-md-3 col-sm-3 col-xs-3">
                    <button type="button" class="btn btn-info btn-md btn-block" id="char4" value="4">4</button>
                  </div>
                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-info btn-md btn-block" id="char5" value="5">5</button>
                  </div>
                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-info btn-md btn-block" id="char6" value="6">6</button>
                  </div>
                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-warning btn-md btn-block" id="div" value="div">/</button>
                  </div>

          </section>

          <section class="row">

                  <div class="col-md-3 col-sm-3 col-xs-3">
                    <button type="button" class="btn btn-info btn-md btn-block" id="char1" value="1">1</button>
                  </div>
                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-info btn-md btn-block" id="char2" value="2">2</button>
                  </div>
                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-info btn-md btn-block" id="char3" value="3">3</button>
                  </div>
                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-warning btn-md btn-block" id="sub" value="sub">-</button>
                  </div>

          </section>

          <section class="row">

                  <div class="col-md-3 col-sm-3 col-xs-3">
                    <button type="button" class="btn btn-info btn-md btn-block" id="char0" value="0">0</button>
                  </div>
                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-info btn-md btn-block" id="komma" value=".">.</button>
                  </div>
                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-success btn-md btn-block" id="result" value="=">=</button>
                  </div>
                  <div class="col-md-3 col-sm-3 col-xs-3">
                      <button type="button" class="btn btn-warning btn-md btn-block" id="add" value="add">+</button>
                  </div>

          </section>

      </div>

  </content>


  <script src="scripts.js"></script>


</body>
            
          
!
            
              
#calc {
  border-style: solid;
  border-width: medium;
  padding: 2em;
  margin-top: 5em;
  margin-right: auto; /* Abstand rechts */
  margin-bottom: 5em;
  margin-left: auto; /* Abstand links */
}

#display {
  text-align: right;
  margin: 0.1em;
  margin-bottom: 0.5em;
  font-size: 2em;
  height: auto;

}

#reset {
  margin-bottom: 0.5em;

}

.btn {
  margin: 0.1em;
  font-size: 2em;
}

            
          
!
            
              // Global variables
var chars = [];
var save = [];
var calc = "add";
var result = 0;

//getters for the characters
document.getElementById("char0").addEventListener("click", function() {
    getNumber(char0);
});
document.getElementById("char1").addEventListener("click", function() {
    getNumber(char1);
});
document.getElementById("char2").addEventListener("click", function() {
    getNumber(char2);
});
document.getElementById("char3").addEventListener("click", function() {
    getNumber(char3);
});
document.getElementById("char4").addEventListener("click", function() {
    getNumber(char4);
});
document.getElementById("char5").addEventListener("click", function() {
    getNumber(char5);
});
document.getElementById("char6").addEventListener("click", function() {
    getNumber(char6);
});
document.getElementById("char7").addEventListener("click", function() {
    getNumber(char7);
});
document.getElementById("char8").addEventListener("click", function() {
    getNumber(char8);
});
document.getElementById("char9").addEventListener("click", function() {
    getNumber(char9);
});
document.getElementById("komma").addEventListener("click", function() {
    getNumber(komma);
});

function getNumber(char) {
    if ((chars.length === 0) && (char === komma)){
        chars.push("0");
    }
    chars.push(char.value);
    document.getElementById("display").value = chars.join('');
    save[0] = chars.join('');
}

//Reset the calculator
document.getElementById("reset").addEventListener("click", resetCalculator);

function resetCalculator() {
    chars.length = 0;
    save.length = 0;
    document.getElementById("display").value = "0";
}

//Calculator functions
document.getElementById("add").addEventListener("click", function() {
    doMath(add.value);
});
document.getElementById("sub").addEventListener("click", function() {
    doMath(sub.value);
});
document.getElementById("div").addEventListener("click", function() {
    doMath(div.value);
});
document.getElementById("mul").addEventListener("click", function() {
    doMath(mul.value);
});

function doMath(math) {
  //reset chars array to create a new number next turn
  chars.length = 0;
      if (math === calc){
          //call calcResult if there are already two numbers present to calculate
          if (save.length > 1){
              result = calcResult(save[1], save[0]);
              document.getElementById("display").value = result;
              save[1] = result;
          }
          else {
              save[1] = save[0];
          }
      }
      else {
        calc = math;
        //after switching the operator dont use calcResult in that turn, just give result over to save[i] and continue
        if (save.length > 1){
            save[1] = result;
        }
        else {
            save[1] = save[0];
        }
      }
}

// --> = <<-
document.getElementById("result").addEventListener("click", getResult);

function getResult() {
    chars.length = 0;
    result = calcResult(save[1], save[0]);
    document.getElementById("display").value = result;
}


function calcResult(numb1, numb2) {
    switch (calc) {
      case 'add':
        return parseFloat(numb1) + parseFloat(numb2);
      case 'sub':
        return parseFloat(numb1) - parseFloat(numb2);
      case 'div':
        return parseFloat(numb1) / parseFloat(numb2);
      case 'mul':
        return parseFloat(numb1) * parseFloat(numb2);
    }
}

            
          
!
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