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='container'>
  <div id='calculator'>

    <!-- TITLE -->

    <div id='title' class='text-center'>
      <h5><b>ELECTRONIC CALCULATOR</b></h5>
    </div>

    <!-- ENTRY BOX -->

    <div id='entrybox' class='text-right'>
      <div id='entry'>
        <p id='answer'>0</p>
      </div>
      <div id='history'>
        <p>0</p>
      </div>
    </div>

    <!-- BUTTONS -->

    <div id='buttons'>

      <button class='red' value='ac'>AC</button>
      <button class='red' value='ce'>CE</button>
      <button value='/'>&divide;</button>
      <button value='*'>x</button>

      <button value='7'>7</button>
      <button value='8'>8</button>
      <button value='9'>9</button>
      <button value='-'>-</button>

      <button value='4'>4</button>
      <button value='5'>5</button>
      <button value='6'>6</button>
      <button value='+'>+</button>


      <button value='1'>1</button>
      <button value='2'>2</button>
      <button value='3'>3</button>
      <button class='invisible'>N</button>

      <button id='zeroButton' value='0'>0</button>
      <button value='.'>.</button>
      <button id='equalButton' value='='>=</button>

    </div>
    <!-- end buttons -->
    <div id='tester'></div>
  </div>
  <!-- end calculator -->
</div>
<!-- end container -->

<footer id="footer" class="text-center">
  <div class="container">
    Designed & Coded by
    <a href="https://www.linkedin.com/in/justin-woodward-891772118" target="_blank">
        Justin Woodward</a>
  </div>
</footer>
              
            
!

CSS

              
                body {
  font-family: 'Orbitron', sans-serif;
  background-color: #818181;
}

#calculator {
  height: 410px;
  width: 300px;
  margin-top: 10%;
  margin-left: auto;
  margin-right: auto;
  background-color: #dfd8d0;
  border: 2px solid #908b85;
  border-radius: 20px;
  box-shadow: 7px 10px 34px 1px rgba(0, 0, 0, 0.68), inset -1px -6px 12px 0.1px #89847e;
}

#title {
  padding-top: 10px;
  padding-bottom: 10px;
}

#entrybox {
  width: 85%;
  height: 65px;
  margin-left: auto;
  margin-right: auto;
  border: 2px solid #b4b39d;
  border-radius: 6px;
  background-color: #c3c2ab;
}

#entry {
  margin-right: 10px;
  font-size: 35px;
}

#buttons {
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  position: absolute;
  display: inline-block;
  width: 280px;
  height: auto;
  margin-top: 15px;
  margin-left: 15px;
}

button {
  border-radius: 8px;
  border: none;
  background-color: #3a3a3a;
  margin: 0 4px 10px 8px;
  height: 40px;
  width: 50px;
  box-shadow: 0px 3px 0px 0px #222121, inset -1px -3px 10px 1px #515151;
}

button:active {
  transform: translate(0px, 3px);
  box-shadow: none;
}

.row {
  margin-top: 20px
}

#topAdjust {
  margin-top: -52px;
}

#equalButton {
  position: absolute;
  margin-left: 12px;
  margin-top: -50px;
  height: 90px;
}

#zeroButton {
  width: 117px;
}

.red {
  font-size: 14px;
  background-color: #a72d45;
  box-shadow: 0px 3px 0px 0px #671c2a;
}

#footer {
  font-family: 'Josefin Sans', sans-serif;
  position: relative;
  font-size: 16px;
  font-weight: normal;
  margin-top: 40px;
  width: 100%;
  height: 40px;
}

button,
button:hover,
button:active,
button:visited {
  text-decoration: none !important;
  outline: none !important;
}

a,
a:hover,
a:active,
a:visited {
  color: #922031;
  text-decoration: none !important;
  outline: none !important;
}

#history {
  /*transform: translate(-13px, -17px);*/
  position: relative;
  height: 10px;
  bottom: 17px;
  margin-right: 14px;
  color: #8b8b7b;
  font-size: 12px;
}
              
            
!

JS

              
                // final draft
$(document).ready(function() {

  //function is used to solve arithmetic equation following DMAS rule.
  //Credits: Swapnil Sharma
  function calcEval(string) {
    var arr = string.split(/([-+x/])/);
    var i = 0;
    // '!' is used as flag
    arr[arr.length++] = "!";
    //Division
    while(arr[i] != "!") {
      if(arr[i] == "/") {
        arr[i-1] = parseFloat(arr[i-1]) / parseFloat(arr[i+1]);
        for(var j = i; arr[j+2] != "!"; j++)
          arr[j] = arr[j+2];
        arr[j] = "!";
        i = 0;
      }
      else
        i++;
    }
    i = 0;
    //Multiplication
    while(arr[i] != "!") {
      if(arr[i] == "x") {
        arr[i-1] = parseFloat(arr[i-1]) * parseFloat(arr[i+1]);
        for(var j = i; arr[j+2] != "!"; j++)
          arr[j] = arr[j+2];
        arr[j] = "!";
        i = 0;
      }
      else
        i++;
    }
    //Imploding, exploding(considering sign of the terms) and then, adding them all
    string = "";
    for(i=0;arr[i] != "!";i++)
      string += arr[i];
    arr = string.split(/(?=[-+])/);
    var total = 0;
    for(i=0;i < arr.length;i++)
      total += parseFloat(arr[i]);
    return total;
  }
  
  var entry = '';
  var ans = '';
  var current = '';
  var log = '';
  var decimal = true;
  var reset = '';
  var check = '';

  // round function if answer includes a decimal
  function round(val) {
    val = val.toString().split('');
    if (val.indexOf('.') !== -1) {
      var valTest = val.slice(val.indexOf('.') + 1, val.length);
      val = val.slice(0, val.indexOf('.') + 1);
      var i = 0;
      while (valTest[i] < 1) {
        i++
      }
      valTest = valTest.join('').slice(0, i + 2);
      if (valTest[valTest.length-1] === '0') {
        valTest = valTest.slice(0, -1);
      }
      return val.join('') + valTest;
    } else {
      return val.join('');
    }
  }

  $('button').click(function() {

    entry = $(this).attr("value");
    console.log('entry: ' + entry);

    //allowing to change arithmetic operator
    if(current[current.length-1] == "+" || current[current.length-1] == "-"|| current[current.length-1] == "*"|| current[current.length-1] == "/") {
      if(entry != current[current.length-1]) {
        if (entry === '/' || entry === '*' || entry === '-' || entry === '+') {
          current[current.length-1] = entry;
          log = log.slice(0, -1);
          if(entry  === "*")
            log += "x";
          else
            log += entry;
          $('#history').html(log);
          $('#answer').html(entry);
        }
      }
    }
    
    //reset for log after answer to equation.
    if (reset) {
      if (entry === '/' || entry === '*' || entry === '-' || entry === '+') {
        log = ans;
      } else {
        ans = '';
      }
    }
    reset = false;

    // All clear or Clear Entry
    if (entry === 'ac' || entry === 'ce' && current === 'noChange') {
      ans = '';
      current = '';
      entry = '';
      log = '';
      check = '';
      $('#history').html('0');
      $('#answer').html('0');
      decimal = true;
    } else if (entry === 'ce') {
      $('#history').html(log.slice(0, -current.length));
      log = log.slice(0, -current.length);
      ans = ans.slice(0, -current.length);
      current = ans;
      if (log.length === 0 || log === ' ') {
        $('#history').html('0');
      }
      $('#answer').html('0');
      entry = '';
      decimal = true;
    }

    // prevents more than one deciminal in a number
    if (entry === '.' || entry === '0.') {
      if (!decimal) {
        entry = '';
      }
    }

    // prevents improper use of first digit
    if (ans.length === 0 && isNaN(entry) && entry !== '.' || ans.length === 0 && entry === '0') {
      entry = '';
      ans = '';
    }

    // prevents extra operators
    if (current !== 'noChange') {
      if (current === '' && isNaN(entry) && entry !== '.' || isNaN(current) && isNaN(entry) && entry !== '.') {
        entry = '';
      }
    }

    // digit combining
    while (Number(entry) || entry === '0' || current === '.') {

      if (isNaN(current) && entry === '0' && current !== '.') {
        entry = '';
      } else if (isNaN(current) && Number(entry) && current !== '.') {
        current = '';
      }
      if (entry === '.') {
        decimal = false;
      }
      if (current === '0.' && isNaN(entry)) {
        entry = '';
      } else {
        if (current[current.length - 1] === '.') {
          current = current.concat(entry);
          ans += entry;
          log += entry;
        } else {
          if(check) {
          current = entry;
          ans = entry;
          log = entry;
          check = '';
        }
        else {
          current += entry;
          ans += entry;
          log += entry;
        }
        }
        $('#answer').html(current);
        $('#history').html(log);
        entry = '';
      }
    }

    // Operation list

    if (entry === '.') {
      if (current === '' || isNaN(current[current.length - 1])) {
        current = '0.';
        ans += entry;
        $('#answer').html('0.');
        log += current;
        $('#history').html(log);

      } else {
        current = current.concat('.');
        ans = ans.concat('.');
        log = ans;
        $('#history').html(ans);
        $('#answer').html(current);
      }
      entry = '';
      decimal = false;

    } else if (entry === '/') {
      current = '/';
      ans = round(eval(ans)) + current;
      log += current;
      $('#history').html(log);
      $('#answer').html('/');
      entry = '';
      check = '';
      decimal = true;

    } else if (entry === '*') {
      current = '*';
      ans = round(eval(ans)) + current;
      log += 'x';
      $('#history').html(log);
      $('#answer').html('x');
      entry = '';
      check = '';
      decimal = true;

    } else if (entry === '-') {
      current = '-';
      ans = round(eval(ans)) + current;
      log += current;
      $('#history').html(log);
      $('#answer').html('-');
      entry = '';
      check = '';
      decimal = true;

    } else if (entry === '+') {
      current = '+';
      ans = round(eval(ans)) + current;
      log += current;
      $('#history').html(log);
      $('#answer').html('+');
      entry = '';
      check = '';
      decimal = true;

    } else if (entry === '=') {
      if (current[current.length - 1] === '.') {
        entry = '';
      } else {
        current = eval(ans);
        //calcEval is implemented to calculate by passing 'log' returning answer
        ans = round(calcEval(log));
        current = ans;
        $('#answer').html(ans);
        //log += entry + ans;
        $('#history').html(log + entry + ans);
        log = ans;
        entry = '';
        //reset = true;
        check = true;
        decimal = true;
      }
      current = 'noChange';
    }
    entry = '';

    if (reset) {
      log = '';
    }

    // max digits on screen
    if ($('#entry').children().text().length > 8 || $('#history').text().length > 22) {
      $('#answer').html('0');
      $('#history').html('Digit Limit Met');
      current = '';
      ans = '';
      log = '';
    }
    
    console.log('decimal: ' + decimal);
    console.log('current: ' + current);
    console.log('answer: ' + ans);
    console.log($('#history').text().length);
  });
}); // end doc ready function
              
            
!
999px

Console