Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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="container-fluid">

  <div id="calculator">
    <div id="solar-panel">
    </div>
    <!-- End Solar panel -->
    <div id="lcd">
      <div class="row-fluid">
        <p id="operator-indicator"></p>
      </div> <!-- End row fluid -->  
      <div class="row-fluid">
      <p id="digits">0</p>
      </div> <!-- End row fluid --> 
    </div>
    <!-- End lcd -->

    <div id="button-container">
      <div class="row-fluid">
        <button id="AC"type="button" class="calc-button red-button">AC</button>
        <button id="CE"type="button" class="calc-button red-button">CE</button>
        <button id="DEL" type="button" class="calc-button">DEL</button>
        <button id="MOD" type="button" class="calc-button">MOD</button>
      </div>
      <!-- End row fluid -->

      <div class="row-fluid">
        <button id="7" type="button" class="calc-button number-button">7</button>
                <button id="8" type="button" class="calc-button number-button">8</button>
                <button id="9" type="button" class="calc-button number-button">9</button>
                <button id="divide"type="button" class="calc-button blue-button">/</button>
      </div> <!-- End row fluid -->
      
           <div class="row-fluid">
        <button id="4" type="button" class="calc-button number-button">4</button>
                <button id="5" type="button" class="calc-button number-button">5</button>
                <button id="6" type="button" class="calc-button number-button">6</button>
                <button id="multiply"type="button" class="calc-button blue-button">X</button>
      </div> <!-- End row fluid -->
      
                 <div class="row-fluid">
        <button id="1"type="button" class="calc-button number-button">1</button>
                <button id="2"type="button" class="calc-button number-button">2</button>
                <button id="3"type="button" class="calc-button number-button">3</button>
                <button id="subtraction"type="button" class="calc-button blue-button">-</button>
      </div> <!-- End row fluid -->
                 <div class="row-fluid">
        <button id="0"type="button" class="calc-button number-button">0</button>
                <button id="decimal" type="button" class="calc-button">.</button>
                <button id="equals" type="button" class="calc-button">=</button>
                <button id="addition"type="button" class="calc-button blue-button">+</button>
      </div> <!-- End row fluid -->
      
    </div>
    <!-- End button-container -->


  </div>
  <!-- End Calculator -->

</div>
<!-- End Container Fluid -->
              
            
!

CSS

              
                body {
  margin: 30px;
}
#calculator {
  height: 500px;
  width: 350px;
  background-color: #646567;
  margin: 0 auto;
  border-radius: 25px;
  padding: 10px;
}

#solar-panel {
  height: 40px;
  width: 150px;
  background-color: #393929;
  margin-left: 155px;
  margin-bottom: 15px;
  border-radius: 5px;
  border: 2px solid black;
}

#lcd {
  height: 70px;
  width: 300px;
  background-color: #D0D8D0;

  margin-left: 20px;
  border-radius: 10px;
  font-famly: Orbitron;
  font-size: 3em;
  
}

#operator-indicator {
 float: right;
 vertical-align: super; 
 font-size: 15px; 
 padding: 10px;
  
}
#digits {
 float: right;
 padding-top: 10px;
}
#button-container {
  margin: 30px;
}
.calc-button {
  height: 50px;
  width: 50px;
  margin-left: 10px;
  margin-bottom: 10px;
  border-radius: 5px;
}

.red-button {
  background-color: #e62e00;

}
.blue-button {
  background-color: #1a52ff;
}

.number-button {
  background-color: #9494b8;
}
              
            
!

JS

              
                var operatorKeys = ['multiply', 'divide', 'addition', 'subtraction', 'AC', 'CE', 'DEL', 'MOD', 'decimal', 'equals'];

var firstNumber = 0;
var secondNumber = 0;
var operationRunning = false;
var newOperation = true;

for (var i = 0; i < 10; i++) {
  assignKeys(i);
}

for (var operators in operatorKeys) {

  assignOperatorKeys(operatorKeys[operators]);

}

function assignKeys(keyPress) {

  $("#" + keyPress).click(function() {

    checkForZero($("#digits").text());

    var lcdLength = $("#digits").text().length;
    var screen = $("#operator-indicator").html();
    //alert(screen);

    if (lcdLength <= 9 && operationRunning == false && newOperation == true) {

      //   var currentScreen = ("#digits").html();

      $("#digits").append(keyPress);

    } else if (operationRunning == true) {
      operationRunning = false;

      $("#digits").html("");
      $("#digits").append(keyPress);
    } else if (newOperation == false) {
      $("#digits").html("");
      $("#digits").append(keyPress);
      newOperation = true;
    }

  });
}

function checkForZero(value) {

  if (value == 0 && value.indexOf(".") == -1) {
    $("#digits").html("");
  }

}

function assignOperatorKeys(keyPress) {
  keyPress = "#" + keyPress;

  // Add functionality to each keypress
  $(keyPress).click(function() {

    switch (keyPress) {
      case "#multiply":
        if ($("#digits").html() != "" && $("#operator-indicator").html() != "") {
          // $("#operator-indicator").html("X");
          equalsFunction("X");
        } else {
          allOperations("X");
        }
        break;
      case "#divide":
        if ($("#digits").html() != "" && $("#operator-indicator").html() != "") {
          equalsFunction();
        } else {
          allOperations("/");
        }
        break;
      case "#addition":
        if ($("#digits").html() != "" && $("#operator-indicator").html() != "") {
          // $("#operator-indicator").html("X");
          equalsFunction();
        } else {
          allOperations("+");
        }
        break;
      case "#subtraction":
        if ($("#digits").html() != "" && $("#operator-indicator").html() != "") {
          // $("#operator-indicator").html("X");
          equalsFunction();
        } else {
          allOperations("-");
        }
        break;
      case "#AC":
        clearButton();
        break;
      case "#CE":
        clearButton();
        break;
      case "#DEL":
        deleteNumbers();
        break;
      case "#MOD":
        if ($("#digits").html() != "" && $("#operator-indicator").html() != "") {
          // $("#operator-indicator").html("X");
          equalsFunction();
        } else {
          allOperations("%");
        }
        break;
      case "#decimal":
        addDecimal();
        break;
      case "#equals":
        equalsFunction();
        break;
    }

  });

}

// moved these functions outside

function clearButton() {
  $("#digits").html("0");
  $("#operator-indicator").html("");
  operationRunning = false;
}

function deleteNumbers() {
  var screen = $("#digits").html();
  screen = screen.slice(0, -1);
  $("#digits").html(screen);
  if (screen == "") {
    clearButton();
  }
}

/*
function multiply() {
  var screen = $("#digits").html();
  operationRunning = true;
  $("#operator-indicator").html("X");
} */

function allOperations(operationSymbol) {
  firstNumber = Number($("#digits").html());
  operationRunning = true;

  $("#operator-indicator").html(operationSymbol);
}

function equalsFunction(indicator) {
  var argumentsLength = arguments.length;
  var total = 0;
  var currentOperation = $("#operator-indicator").html();
  //firstNumber = Number($("#digits").html());
  // console.log(currentOperation);
  

  $("#operator-indicator").html("");
 

  secondNumber = Number($("#digits").html());
  //alert(currentOperation);
  switch (currentOperation) {
    case "+":
      total = firstNumber + secondNumber;
      break;
    case "X":
      total = firstNumber * secondNumber;
      break;
    case "-":
      total = firstNumber - secondNumber;
      break;
    case "/":
      total = firstNumber / secondNumber;
      break;
    case "%":
      total = firstNumber % secondNumber;
      break;
  }

  if (total % 1 != 0) {

    total = total.toString();
    total = total.substring(0, 10);
    total = Number(total);

  }
  /*
    var fixedDecimal = 0;
     fixedDecimal = calculateDecimalPlaces(total);

      total = total.toFixed(fixedDecimal);
      
     
  } 
  */

  if (total > 9999999999) {
    total = "E";
  }

  $("#digits").html(total);
  operationRunning = false;
  decimalValue = false;
  newOperation = false;

}

function addDecimal() {
  var screen = $("#digits").html();

  if (screen.indexOf(".") == -1) {
    $("#digits").append(".");
  }

}

/*
function calculateDecimalPlaces(totalAmt) {
  totalAmt = totalAmt.toFixed(9);
  var totalStr = totalAmt.toString();
  var totalArray = totalStr.split("");
  var decimalPlace;
  var lcdLength = 9;
  var fixedDecimal =0;
  
  
  for (var decimal in totalArray) {
    if (totalArray[decimal] == ".") {
      decimalPlace = decimal; // save decimal place index location
    }
    
  }
  
  

  fixedDecimal = lcdLength - decimalPlace+1;
  

  
  return fixedDecimal;

  
  
  
}

*/
              
            
!
999px

Console