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

              
                
<div class="calculator">
<h1>Calculator</h1>
  <div class="outputs">
  <output id="calc-output"></output>
  <output id="calc-full-output"></output>
  </div>
  
  <div class="col-sm-">
  <button type="button" class="btn btn-warning" id="clr">C</button> 
  </div>
 


  <div class="col-sm-">
  <button type="button" class="btn btn-primary" id="one">1</button>
  <button type="button" class="btn btn-primary" id="two">2</button>
  <button type="button" class="btn btn-primary" id="three">3</button>
  <button type="button" class="btn btn-primary operator" id="plus">+</button>
  </div>
  
  <div class="col-sm-">
  <button type="button" class="btn btn-primary" id="four">4</button>
  <button type="button" class="btn btn-primary" id="five">5</button>
  <button type="button" class="btn btn-primary" id="six">6</button>
  <button type="button" class="btn btn-primary operator" id="minus">-</button>  
  </div>
  
  <div class="col-sm-">
  <button type="button" class="btn btn-primary" id="seven">7</button>
  <button type="button" class="btn btn-primary" id="eight">8</button>
  <button type="button" class="btn btn-primary" id="nine">9</button>
    <button type="button" class="btn btn-primary operator" id="divide">/</button>
  </div>
  
  <div class="col-sm-">
  <button type="button" class="btn btn-primary" id="zero">0</button>
  <button type="button" class="btn btn-primary" id="decimal">.</button>
  <button type="button" class="btn btn-primary operator" id="equals">=</button>
     <button type="button" class="btn btn-primary operator" id="multiply">*</button>
  </div>
</div>
              
            
!

CSS

              
                .btn-primary{
  width: 50px;
  height: 50px;
  background-color: #1d6df7;
}
.btn-warning{
  width: 100px;
  height: 50px;
  background-color: orange;
}

.col-sm-{
  margin-bottom: 2px;
}

.operator{
  background-color: #124399;
  border-color: #124399;
}

.calculator{
  position: fixed; /* or absolute */
  top: 10%; /* change back to 50% when done, it's just easier to debug this way..*/
  left: 50%;
  margin-top: -50px;
  margin-left: -100px;
}

.outputs{
  border: 0.5px solid black;
  margin-bottom: 2px;
}

#calc-output{
  display: inline-block;
    line-height: 0.75em;
  font-size: 125%;
}

#calc-full-output{
    line-height: 0.85em;
}
              
            
!

JS

              
                // Quick note --- This library: http://www.openjs.com/scripts/events/keyboard_shortcuts/shortcut.js --- It doesn't work very well...
// Just wanted to let you know.

function changeText(id) {
  var replacementText = document.getElementById(id).innerHTML;
  
  //----------------------------------
  // all the text manipulation stuff..
  //----------------------------------  
  if (id == "plus" ||
     id == "minus" ||
     id == "multiply" ||
     id == "divide"){
    document.getElementById("calc-output").innerHTML = "";
  }else if(id == "equals"){
    document.getElementById("calc-output").innerHTML = "";
  }
  else{
    document.getElementById("calc-output").innerHTML += replacementText;
  }
}

function clearText(){
  document.getElementById("calc-output").innerHTML = "";
  document.getElementById("calc-full-output").innerHTML = "";
}

function allText(id){
  var replacementText = document.getElementById(id).innerHTML;
  document.getElementById("calc-full-output").innerHTML += replacementText;
}

function answer(result){
  document.getElementById("calc-full-output").innerHTML = result;
}

//-------------------------------------------------------------------------------------------------||||
// all the actual calculations
// the only reason my functions aren't named consistently is because the code wouldn't work if like
// I had the function called divide or multiply. I guess those are functions built into javascript.
//-------------------------------------------------------------------------------------------------||||

function calculateNumbers(){
  var calculate = document.getElementById("calc-full-output").innerHTML;
  var arr = [];
  var instances = [];
  if(calculate.indexOf("+") !== -1){
    for(var i=0; i<calculate.length; i++) {
      if (calculate.charAt(i) === "+"){
       instances.push(i); 
       arr = calculate.split(calculate.charAt(i));
      } 
  }
  add(arr);
  }
  else if(calculate.indexOf("-") !== -1){
    for(var i=0; i<calculate.length; i++) {
      if (calculate.charAt(i) === "-"){
       instances.push(i); 
       arr = calculate.split(calculate.charAt(i));
      } 
  }
  subtract(arr);
  }
   else if(calculate.indexOf("/") !== -1){
    for(var i=0; i<calculate.length; i++) {
      if (calculate.charAt(i) === "/"){
       instances.push(i); 
       arr = calculate.split(calculate.charAt(i));
      } 
  }
  
  division(arr);
  }
  else if(calculate.indexOf("*") !== -1){
    for(var i=0; i<calculate.length; i++) {
      if (calculate.charAt(i) === "*"){
       instances.push(i); 
       arr = calculate.split(calculate.charAt(i));
      } 
  }
  
  multiplication(arr);
  }
   
}

function add(arrayTocalculate){
  var array = [];
  
  for (var i=0; i<arrayTocalculate.length; i++){
    array[i] = parseFloat(arrayTocalculate[i]);
  }
  array = array.reduce(function(a, b){
      return a+b;
    });
  
  answer(array);
}

function subtract(arrayTocalculate){
  var array = [];
  
  for (var i=0; i<arrayTocalculate.length; i++){
    array[i] = parseFloat(arrayTocalculate[i]);
  }
  array = array.reduce(function(a, b){
      return a-b;
    });
  
  answer(array);
}

function division(arrayTocalculate){
  var array = [];
  
  for (var i=0; i<arrayTocalculate.length; i++){
    array[i] = parseFloat(arrayTocalculate[i]);
  }
  array = array.reduce(function(a, b){
      return a/b;
    });
  
  answer(array);
}

function multiplication(arrayTocalculate){
  var array = [];
  
  for (var i=0; i<arrayTocalculate.length; i++){
    array[i] = parseFloat(arrayTocalculate[i]);
  }
  array = array.reduce(function(a, b){
      return a*b;
    });
  
  answer(array);
}



//----------------------------------------------
// initializing all the button clicking stuff..
//----------------------------------------------

var one = document.getElementById('one');
var two = document.getElementById('two');
var three = document.getElementById('three');
var four = document.getElementById('four');
var five = document.getElementById('five');
var six = document.getElementById('six');
var seven = document.getElementById('seven');
var eight = document.getElementById('eight');
var nine = document.getElementById('nine');
var zero = document.getElementById('zero');

var divide = document.getElementById('divide');
var multiply = document.getElementById('multiply');
var minus = document.getElementById('minus');
var plus = document.getElementById('plus');

var decimal = document.getElementById('decimal');

var equals = document.getElementById('equals');
var clear = document.getElementById('clr');



one.addEventListener("click", function(){changeText(one.id); allText(one.id);}, true);
two.addEventListener("click", function(){changeText(two.id); allText(two.id);}, true);
three.addEventListener("click", function(){changeText(three.id); allText(three.id);}, true);
four.addEventListener("click", function(){changeText(four.id); allText(four.id);}, true);
five.addEventListener("click", function(){changeText(five.id); allText(five.id);}, true);
six.addEventListener("click", function(){changeText(six.id); allText(six.id);}, true);
seven.addEventListener("click", function(){changeText(seven.id); allText(seven.id);}, true);
eight.addEventListener("click", function(){changeText(eight.id); allText(eight.id);}, true);
nine.addEventListener("click", function(){changeText(nine.id); allText(nine.id);}, true);
zero.addEventListener("click", function(){changeText(zero.id); allText(zero.id);}, true);


divide.addEventListener("click", function(){changeText(divide.id); allText(divide.id);}, true);
multiply.addEventListener("click", function(){changeText(multiply.id); allText(multiply.id);}, true);
minus.addEventListener("click", function(){changeText(minus.id); allText(minus.id);}, true);
plus.addEventListener("click", function(){changeText(plus.id); allText(plus.id);}, true);

decimal.addEventListener("click", function(){changeText(decimal.id); allText(decimal.id);}, true);

equals.addEventListener("click", function(){changeText(equals.id); calculateNumbers();}, true);
clear.addEventListener("click", function(){clearText(clear.id);}, true);



//------------------------------------------------------------------
// the keyboard shortcuts, really happy with the way these came out.
//------------------------------------------------------------------
document.addEventListener("keydown", function(key) {
  
  switch(key.which){
    case 8: // TODO: make this delete the last character only.
      clearText();
      break;
    case 13:
      changeText(equals.id);
      calculateNumbers();
      break;
    case 48:
      changeText(zero.id); 
      allText(zero.id);
      break;
    case 49:
      changeText(one.id); 
      allText(one.id);
      break;
    case 50:
      changeText(two.id); 
      allText(two.id);
      break;
    case 51:
      changeText(three.id); 
      allText(three.id);
      break;
    case 52:
      changeText(four.id); 
      allText(four.id);
      break;
    case 53:
      changeText(five.id); 
      allText(five.id);
      break;
    case 54:
      changeText(six.id); 
      allText(six.id);
      break;
    case 55:
      changeText(seven.id); 
      allText(seven.id);
      break;
    case 56:
      changeText(eight.id); 
      allText(eight.id);
      break;
    case 57:
      changeText(nine.id); 
      allText(nine.id);
      break;
    case 67:
      clearText();
      break;
    case 187:
      changeText(plus.id); 
      allText(plus.id);
      break;
    case 56:
      changeText(multiply.id); 
      allText(multiply.id);
      break;
    case 191:
      changeText(divide.id); 
      allText(divide.id);
      break;
    case 189:
      changeText(minus.id); 
      allText(minus.id);
      break;
      
                  }
});


              
            
!
999px

Console