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="wrapper">
  <div class="colormenu">
    <select onchange="bkgdColors(this);">
      <option value="rgba(255, 140, 0, 0.4)">Orange</option>
      <option value="rgba(254, 42, 42, 0.4)">Red</option>
      <option value="rgba(162, 3, 254, 0.4)">Purple</option>
      <option value="rgba(3, 109, 254, 0.4)">Blue</option>
      <option value="rgba(3, 254, 109, 0.4)">Green</option>
    </select>
  </div>
  <div class="calheader">
      <h2>Simple Calculator</h2>
    </div>
  <div class="calculatorbox">
    <div class="calwindow">
      <!-- ENTRY BOX -->
      <div class="entry">
        <p id="answer"></p>
      </div>
      <div class="entryhistory">
        <p id="history"></p>
      </div>
    </div>
    <!-- BUTTONS -->
    <div class="calbuttons">
      <div class="row">
        <button class="key topcolor" value="clear">C</button>
        <button class="key topcolor" value="plusminus"><sup>+</sup>/<sub>−</sub></button>
        <button class="key topcolor" value="%">%</button>
        <button class="key orange" value="/">÷</button>
      </div>
      <div class="row">
        <button class="key" value="7">7</button>
        <button class="key" value="8">8</button>
        <button class="key" value="9">9</button>
        <button class="key orange" value="*">×</button>
      </div>
      <div class="row">
        <button class="key" value="4">4</button>
        <button class="key" value="5">5</button>
        <button class="key" value="6">6</button>
        <button class="key orange" value="-">−</button>
      </div>
      <div class="row">
        <button id="one" class="key" value="1">1</button>
        <button id="two" class="key" value="2">2</button>
        <button id="three" class="key" value="3">3</button>
        <button id="add" class="key orange" value="+">+</button>
      </div>
      <div class="row">
        <button id="zero" class="key btnspan" value="0">0</button>
        <button class="key" value=".">.</button>
        <button id="equals" class="key orange" value="=">=</button>
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Roboto:300,400');
h1, h2, h3, p {
  font-family: 'Roboto', sans-serif;
}
html, body{
  height:100%;
  margin: 0;
  background-color: #ffffff;
  transition-duration: .3s;
}
.colormenu {
  position: absolute;
  top: 20px;
  left: 20px;
}
.wrapper {
  width: 100%;
  height: 100%;
  position: relative;
  display:flex;
  flex-direction:column;
  justify-content:center;
  align-items:center;
  background-image: url(https://s6.postimg.org/y6nvcsblt/65302.jpg);
  
  background-repeat: no-repeat;
    background-size: cover;
  background-position: center center;
  padding: 160px 0;
}
.calculatorbox {
  width: 300px;
  margin: 0 auto;
  border: 1px solid #000000;
}
.calheader {
  text-align: center;
}
.calwindow {
  background: #000000;
  position: relative;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
   -webkit-flex-direction: column; /* Safari */
  flex-direction:         column;
  -webkit-justify-content: flex-end;
  justify-content: flex-end;
  -webkit-align-items: flex-end;
  align-items: flex-end;
  padding: 10px 0;
  box-sizing: border-box;
}
.entry {
  font-size: 4em;
  display: block;
  line-height: 1em;
}
.entryhistory {
  font-size: 1em;
  letter-spacing: 2px;
  padding-right: 5px;
}
.entry p, .entryhistory p {
  margin: 0;
  color: #ffffff;
}
sup {
  top: -0.5em;
}
 
sub {
  bottom: -0em;
}
.row {
  clear: both;
  width: 100%;
  display: flex;
}
button {
  display: inline-block;
  border: none;
  padding: 0;
  outline: none;
  cursor: pointer;
}
.key {
  width: 75px;
  height: 70px;
  font-size: 22px;
  border-top: 1px solid rgba(0, 0, 0, 0.3);
  border-right: 1px solid rgba(0, 0, 0, 0.3);
  box-sizing: border-box;
}
.key.btnspan {
  width: 150px;
}
.key.topcolor {
  background: #d9d9d9;
}
.key.orange {
  background: #ff8c00;
  color: #ffffff;
}
              
            
!

JS

              
                $(document).ready(function() {
  
  var mainMath = "0";
  var history = "0";
  var finalset = "";
  var prevKey = "";
  var historyarray = [];
  var equalPressed = false;
  update();
  
  $("button").click(function(){
    calculate($(this).attr("value"));
  });
  
  function calculate(keyitem) {
    console.log("buttonpress: " + keyitem);
    historyarray.push(keyitem);
    console.log("buttonpressHistoryArray: " + historyarray);
    switch(keyitem) {
      case "clear":
        clearScreen();
        break;
      case "%":
        percentageScreen();
        break;
      case "/":
      case "*":
      case "+":
      case "-":
        addOperator(keyitem);
        break;
      case "plusminus":
        plusminusScreen();
        break;
      case "0":
      case "1":
      case "2":
      case "3":
      case "4":
      case "5":
      case "6":
      case "7":
      case "8":
      case "9":
        addNumber(keyitem);
        break;
      case ".":
        addDecimal(keyitem);
        break;
      case "=":
        solveEqual(keyitem);
        break;
    }
    update();
    };
 
  function clearScreen() {
    mainMath = "0";
    history = "0";
    historyarray = [];
    equalPressed = false;
    if(mainMath.length > 0){
      $(".entry").css("font-size", "4em");
    }
    console.log("clearMain: " + mainMath);
    console.log("clearSub: " + history);
  };
  
  function plusminusScreen() {
     mainMath = -1 * mainMath;
    finalset = mainMath;
    console.log("plusminusMain: " + mainMath);
    console.log("plusminusFinal: " + finalset);
  };
  
  function addNumber(keyitem) {
      if (mainMath === "0" /*&& ["/", "*", "+", "-"].indexOf(keyitem) == -1*/){
        mainMath = keyitem;
        finalset = mainMath;
        return;
        console.log("addedMainZero: " + mainMath);
        console.log("addedFinalZero: " + finalset);
      } else //sif (keyitem == "0") 
      
      {
       // mainMath+=keyitem;
      }
    mainMath+=keyitem;
    finalset = mainMath;
    
    console.log("addedMain: " + mainMath);
    console.log("addedFinalset: " + finalset);
    
    if(mainMath.length > 8){
      $(".entry").css("font-size", "1.5em");
    }
  };
  
  function isOperator(char) {
    return ["/", "*", "+", "-"].indexOf(char) >= 0;
  }
  
  function addOperator(keyitem){
    console.log("addOpHistoryArray: " + historyarray);
    // if we already have an operation as the last keyitem typed
    if (isOperator(historyarray[historyarray.length-2]) ) {
      historyarray[historyarray.length-2] = keyitem;
       //history = history.substring(0, history.length-2)+keyitem;
       console.log("last key was an operator");
    // return;
    }
    
    //if the first character typed in an operation
    if    (["/", "*", "+", "-"].indexOf(historyarray[0]) > -1)   {
      historyarray = [];
      return; 
    }
         
    if (equalPressed == true){
      history = "";
      historyarray = [];
      equalPressed = false;
    }
    console.log("mainMainHistory0check: " + mainMath);
    //addNumber(keyitem);
    mainMath+=keyitem;
    console.log("mainMainHistory0: " + mainMath);
    
    if (isOperator(mainMath)) {
      console.log("mainMainHistoryIfOperatorBefore: " + history);
      history = history.substring(0, history.length-1) + keyitem;
      mainMath = "0";
      console.log("mainMainHistoryIfOperator: " + history);
      console.log("returned");
      return;
    } 
    
    if(history == "0"){
      // history = "";
      history = mainMath;
    } else {
      history += mainMath; 
    }
    
    
    mainMath = "0";
    equalPressed = false;
    console.log("addOpSub: " + history);
    console.log("addOpMain: " + mainMath);
    console.log("equaltrueTest: " + equalPressed);
  };
  
  function addDecimal(keyitem){
    if (mainMath.indexOf(keyitem) === -1){
      if(mainMath == "0") {
        mainMath = "0" + keyitem;
        return;
      }
    } else {
        return;
      }
    mainMath+=keyitem;
    //addNumber(keyitem);
  };
  
  function percentageScreen() {
    mainMath = mainMath / 100;
    return;
  };
  
  function solveEqual(keyitem) {
    history += finalset;
    mainMath = eval(history);
    console.log("solveEqualresult: " + mainMath);
    console.log("solveEqualhistory: " + history);
    var finalresult = mainMath.toString();
    if(finalresult.length > 8){
      $(".entry").css("font-size", "1.5em");
    }
    equalPressed = true;
  };
  
  function update(){
  $("#answer").html(mainMath);
  $("#history").html(history);
};
  
  $("option").click(function bkgdColors(color) {
    $(".wrapper").css("background-color", value);
  });
  
});

/*Problems
1. Need to add several color backgrounds. Default is Orange?
*/

/* Events 
1. add a number
2. add a dot
3. add minus sign to number
4. clear screen
5. perform operators (-,+, /, *)
6. calculate (=)
7. if an equal sign is pressed more than 2 times, add result to last number in operators. Ex. 1.2+2 = 4 then if equal sign is pressed again, add +2(last number in operator) to 4 = 6
8. If someone press an operator after first equation, make the result of the first equation the 1st part in next equation. Ex. if one press 1 +2 = 3 and then press a operator (+), make 
*/

/* Problems
1. Fix the problem when an operator is pressed more then one time. Ex. enter "5" and "+" and then add another "-", it shows "5+0-" when it should be "5+" at first then "5-" afterward.
*/
              
            
!
999px

Console