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

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

              
                <body>
  <div class="calculator">
    
    <div id='entrybox' class='text-right'>
      <div id='entry'>
        <p id='answer'>0</p>
      </div>
      <div id='history'>
        <p>0</p>
      </div>
    </div> 
  
  
    <div class="row">
      <div class="container-fluid">
        <div class="col-xs-3">
          <div value="C" class="button">C</div>
          <div value="7" class="button">7</div>
          <div value="4" class="button">4</div>
          <div value="1" class="button">1</div>
          <div value="0" class="button">0</div>
        </div>
        <div class="col-xs-3">
          <div value="CE" class="button">CE</div>
          <div value="8" class="button">8</div>
          <div value="5" class="button">5</div>
          <div value="2" class="button">2</div>
          <div value="." class="button">.</div>
        </div>
        <div class="col-xs-3">
          <div value="%" class="button">%</div>
          <div value="9" class="button">9</div>
          <div value="6" class="button">6</div>
          <div value="3" class="button">3</div>
          <div value="=" class="button" id="equals">=</div>
        </div>
        <div class="col-xs-3">
          <div value="/" class="button">/</div>
          <div value="*" class="button">*</div>
          <div value="-" class="button">-</div>
          <div value="+" class="button">+</div>
        </div>
      </div>
    </div>
  </div>
</body>
              
            
!

CSS

              
                body {
  background: url(http://www.islamnews.ru/wp-content/uploads/2016/09/laptop-glasses-coffe-preview-850x565.jpg) no-repeat center center fixed;
  background-size: cover;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  padding-top: 50px;
  font-weight: bold;
}

.calculator {
  width: 250px;
  height: 380px;
  background: red;
  margin: 10px auto 50px;
  padding: 5px 10px;
  border-radius: 10px;
  background: #ececec;
  box-shadow: 1px 7px 10px 3px #000, inset 0 0 2px 0 #000;
  background-image: -webkit-repeating-linear-gradient(left, hsla(0, 0%, 100%, 0) 0%, hsla(0, 0%, 100%, 0) 6%, hsla(0, 0%, 100%, .1) 7.5%), -webkit-repeating-linear-gradient(left, hsla(0, 0%, 0%, 0) 0%, hsla(0, 0%, 0%, 0) 4%, hsla(0, 0%, 0%, .03) 4.5%), -webkit-repeating-linear-gradient(left, hsla(0, 0%, 100%, 0) 0%, hsla(0, 0%, 100%, 0) 1.2%, hsla(0, 0%, 100%, .15) 2.2%), linear-gradient(180deg, hsl(0, 0%, 78%) 0%, hsl(0, 0%, 85%) 47%, hsl(0, 0%, 78%) 53%, hsl(0, 0%, 70%)100%);
}

#entrybox {
  width: 200px;
  height: 80px;
  background: #999;
  margin: 15px;
  border: 1px solid #888;
  border-radius: 5px;
  text-align: right;
  padding-top: 3px;
  padding-right: 5px;
  overflow: hidden;
  background: -webkit-linear-gradient(top, #c9c7c7 0%, #999 100%);
  background: -linear-gradient(top, #c9c7c7 0%, #999 100%);
  background: -moz-linear-gradient(top, #c9c7c7 0%, #999 100%);
}

#entry {
  font-size: 30px;
}

.button {
  cursor: pointer;
  width: 40px;
  height: 30px;
  background: #a5a5a5;
  margin: 10px auto 20px auto;
  border-radius: 4px;
  padding-top: 5px;
  box-shadow: 0 1px 2px #000;
  border-bottom: 1px solid #555;
  -moz-transition: 0.1s;
  -webkit-transition: 0.1s;
  transition: 0.1s;
  position: relative;
  background: -webkit-linear-gradient(top, #c0c0c0 0%, #8f8f8f 100%);
  background: -linear-gradient(top, #c0c0c0 0%, #8f8f8f 100%);
  background: -moz-linear-gradient(top, #c0c0c0 0%, #8f8f8f 100%);
}

#equals{
  width:110px;
}

.button:hover {
  background: -webkit-linear-gradient(top, #c0c0c0 0%, #737373 100%);
  background: -linear-gradient(top, #c0c0c0 0%, #737373 100%);
  background: -moz-linear-gradient(top, #c0c0c0 0%, #737373 100%);
}

.button:active {
  border-bottom-width: 0;
  top: 1px;
}

.container-fluid {
  text-align: center;
}

.col-xs-3 {
  padding: 0;
}
              
            
!

JS

              
                $(document).ready(function(){
      
    var log= '';
    var ans= '';
    var entry= '';
    var current = '';
    var decimal = true;
    var reset = '';
  
  // 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);
    
    //reset for log after answer
    if (reset) {
      if (entry==='%' || entry === '/' || entry === '*' || entry === '-' || entry === '+') {
        log = ans;
      } else {
        ans = '';
      }
    }
    reset = false;
    
    //CLear all and clear entry
    if (entry === 'C' || entry === 'CE' && current === 'noChange') {
      ans = '';
      current = '';
      entry = '';
      log = '';
      $('#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 = '';
      }
    }
    
    
    // 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);
        } else {
          current += entry;
        }
        ans += entry;
        $('#answer').html(current);
        log += entry;
        $('#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 = '';
      decimal = true;

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

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

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

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

    } else if (entry === '=') {
      if (current[current.length - 1] === '.') {
        entry = '';
      } else {
        current = eval(ans).toString();
        $('#answer').html(round(eval(ans)));
        ans = round(eval(ans));
        log += entry + ans;
        $('#history').html(log);
        log = ans;
        entry = '';
        reset = true;
        decimal = true;
      }
      current = 'noChange';
    }
    entry = '';

    if (reset) {
      log = '';
    }
    
    // max digits on screen
    if ($('#entry').children().text().length > 11 || $('#history').text().length > 23) {
      $('#answer').html('0');
      $('#history').html('Digit Limit Met');
      current = '';
      ans = '';
      log = '';
      decimal = true;
    }

    console.log('decimal: ' + decimal);
    console.log('current: ' + current);
    console.log('answer: ' + ans);
    console.log($('#history').text().length);

    
  });
});
              
            
!
999px

Console