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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <html>
  <head>
    <title>A Calculator FreeCodeCamp Practice</title>
  </head>
  <body>
    <div class="container bodyContainer">
      <div class="row">
        <p class="title">Pocket Calculator</p>
        <input type="text" disabled="disabled" id="screen"/>
      </div>
      <div class="container calculator">
        <div class="row">
          <button class="red">AC</button>
          <button class="red">CE</button>
          <button class="blue">%</button>
          <button class="blue">÷</button>
        </div>
        <div class="row">
          <button>7</button>
          <button>8</button>
          <button>9</button>
          <button class="blue">x</button>
        </div>
        <div class="row">
          <button>4</button>
          <button>5</button>
          <button>6</button>
          <button class="blue">-</button>
        </div>
        <div class="row">
          <button>1</button>
          <button>2</button>
          <button>3</button>
          <button class="blue">+</button>
        </div>
        <div class="row">
          <button>0</button>
          <button>.</button>
          <button class="long blue">=</button>
        </div>
      </div>
    </div>
  </body>
</html>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Share+Tech+Mono);
@import url(https://fonts.googleapis.com/css?family=VT323);

body {
  background-color: #6E6E6E;
}

.bodyContainer {
  background-color: pink;
  text-align: center;
  margin-top: 10%;
  position: relative;
  border: 2px solid #666;
  border-radius: 35px;
  padding: 10px;
  padding-top: 10px;
  padding-bottom: 25px;
  width: 320px;
  margin: 60px auto; 
  box-shadow: 5px 5px 2px #333;
}

input {
  position: relative;
  border: 2px solid #666;
  border-radius: 15px;
  width: 225px;
  height: 30px;
  margin: 7px;
  margin-top: 1px;
  padding: 10px;
}

#screen {
  text-align: right;
  font-family: 'VT323';
  font-size: 53px;
}

button {
  position: relative;
  border: 1px;
  border-radius: 10px;
  box-shadow: 2px 2px 2px black;
  width: 50px;
  height: 50px;
  margin: 10px 5px;
  font-size: 30px;  
  font-family: 'Share Tech Mono';
  text-align: center;
}

button:focus {
  box-shadow: 0 0 5pt 2pt #FE9A2E;
  outline: none;
}

button:active {
  top: 2px;
}

.long {
  width: 115px;
}

.title {
  font-family: 'Share Tech Mono';
  font-size: 28px;
  font-weight: bold;
}

.red {
  background-color: #F7819F;
}

.blue {
  background-color: #A9E2F3;
}

@media screen and (max-width: 450px) {
  .title {
    font-family: Arial;
  }
  #screen {
    color: #000000;
    text-align: right;
    font-size: 38px;
  }
  button {
    background-color: #E6E6E6;
    font-size: 18px;
    font-weight: bold;
  }
}
              
            
!

JS

              
                $(document).ready(function() {
    var entry_num = 0;
    var entry_cal = '';
    var temp = '';
    var total = 0;
    $('button').on('click', function(){
        //Collect every single input from buttons
        val = $(this).text();

        //Button input which is number or '.'
        if (!isNaN(val) || val === '.') {
            //In case last '=' result stack up with new input
            if (typeof temp === 'number') {
                temp = '';
            }
            //Put all number inputs into a string until button input gives non-number
            temp += val;
            //Print whatever number input on calculator screen
            $("#screen").val(temp.substring(0,10));
        }

        //Button inputs which is 'AC'
        else if (val === 'AC') {
            //Clear everything up
            temp = '';
            entry_num = '';
            entry_cal = '';
            total = 0;
            $("#screen").val(temp);
        }

        //Button inputs which is 'CE'
        else if (val === 'CE') {
            //Delete last digit of input number string
            temp = temp.substring(0, temp.length - 1);
            $("#screen").val(temp.substring(0,10));
        }

        //Button inputs which is '+', '-', 'x', '÷', '%', or '='
        else {
            //Check if equation-right number exists <-- newer number input
            if (temp) {
                //Check if equation-left number exists <--older number input, and equation symbol exist
                if (entry_num && entry_cal) {
                    entry_num = filterDigitsOverTen(entry_num + "");
                    temp = filterDigitsOverTen(temp);
                    console.log(entry_num);
                    console.log(temp);
                    //Calculate a equation with given elements
                    switch (entry_cal) {
                        case '+':
                            total = entry_num + temp;
                            break;
                        case '-':
                            total = entry_num - temp;
                            break;
                        case 'x':
                            total = entry_num * temp;
                            break;
                        case '÷':
                            total = entry_num / temp;
                            break;
                        case '%':
                            total = entry_num % temp;
                            break;
                    }

                    //Limit the total digit of result to 10
                    if ((total + "").length > 10) {
                        total = limitTotalDigitTen(total + ".");
                    }

                    //Print result on calculator screen
                    console.log(total);
                    if (total) {
                        $("#screen").val(total);
                    }
                    else {
                        $("#screen").val("err");
                    }

                    //If current equation symbol is '=', store the current result to equation-right number, then clear up equation-left number and equation symbol, also could keep going with chain calculation
                    if (val === '=') {
                        temp = total;
                        entry_cal = '';
                        entry_num = 0;
                    }
                    //Chain calculation, store the current result of equation-left number, store the latest equation symbol to equation symbol, equation-right number wait for new number input
                    else {
                        entry_num = total;
                        entry_cal = val;
                        temp = '';
                    }
                }

                //If only one equation number exists, store if to equation-left number, store the current equation symbol, and wait for new number input, will store in equation-right number
                else {
                    entry_num = parseFloat(temp);
                    entry_cal = val;
                    temp = '';
                }

            }

            //If equation doesn't build up in the correct way, print 'err' on calculator screen
            else {
                $("#screen").val("err");
                temp = '';
                entry_num = '';
                entry_cal = '';
                total = 0;
            }
        }
    });

    function filterDigitsOverTen(str) {
        if (str.length <= 10) {
            return parseFloat(str);
        }
        else {
            if (str[9] === '.') {
                return parseFloat(str.slice(0, 9));
            }
            return parseFloat(str.slice(0, 10));
        }
    }

    function limitTotalDigitTen(str) {
        if (str.length <= 10) {
            return parseFloat(str);
        }
        else {
            if (str.slice(0, 10).indexOf('.') !== -1) {
                console.log("enter");
                console.log(str.slice(0, 10).indexOf('.'))
                if (str[9] === '.') {
                    return str.slice(0, 8);
                }
                return parseFloat(str.slice(0, 10));
            }
            console.log("should be err");
            return null;
        }
    }
});
              
            
!
999px

Console