cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>



    <!-- google font -->
    <link href="https://fonts.googleapis.com/css?family=Nanum+Gothic+Coding" rel="stylesheet"> 
    
    <!-- google font -->
    <link href="https://fonts.googleapis.com/css?family=Inconsolata" rel="stylesheet"> 
   
    <title>Javascript calulcator</title>
  </head>
  <body>

    <div class="calc-container">
      <div class="row" id="title">
         <div class="col-sm-12">
          Javascript calculator
             
         </div>
      </div>
      
      <div id="result-area">
          <div>
            <h1 class="text-right" id="result">0</h1>
          </div>
      </div>
      
        <div class="row" id="button-area">

            <div class="col-sm-12" id="number-buttons">

                <div class="row button-rows">
                    <div class="col-sm-2">
                        <button class="btn btn-dark btn-block" type="submit">7</button>
                    </div>

                    <div class="col-sm-2">
                        <button class="btn btn-dark btn-block" type="submit">8</button>
                    </div>

                    <div class="col-sm-2">
                        <button class="btn btn-dark btn-block" type="submit">9</button>
                    </div>
                    
                    <div class="col-sm-6">
                        <button class="btn btn-danger btn-block" type="submit">Clear</button>
                    </div>
        
    
                </div>
                
                <div class="row button-rows">
                    <div class="col-sm-2">
                        <button class="btn btn-dark btn-block" type="submit">4</button>
                    </div>

                    <div class="col-sm-2">
                        <button class="btn btn-dark btn-block" type="submit">5</button>
                    </div>

                    <div class="col-sm-2">
                        <button class="btn btn-dark btn-block" type="submit">6</button>
                    </div>
                    
                    <div class="col-sm-3">
                        <button class="btn btn-dark btn-block" type="submit">x</button>
                    </div>
        
                    <div class="col-sm-3">
                        <button class="btn btn-dark btn-block" type="submit">&#247;</button>
                    </div>
                    
                            
                </div>
                
                <div class="row button-rows">
                    <div class="col-sm-2">
                        <button class="btn btn-dark btn-block" type="submit">1</button>
                    </div>

                    <div class="col-sm-2">
                        <button class="btn btn-dark btn-block" type="submit">2</button>
                    </div>

                    <div class="col-sm-2">
                        <button class="btn btn-dark btn-block" type="submit">3</button>
                    </div>
                    
                    <div class="col-sm-3">
                        <button class="btn btn-dark btn-block" type="submit">-</button>
                    </div>
        
                    <div class="col-sm-3">
                        <button class="btn btn-dark btn-block" type="submit">+</button>
                    </div>
                    
                            
                </div>
                
                <div class="row button-rows">
                    <div class="col-sm-3">
                        <button class="btn btn-dark btn-block" type="submit">0</button>
                    </div>
                    
                    <div class="col-sm-3">
                        <button class="btn btn-dark btn-block" type="submit">.</button>
                    </div>
                    
                    <div class="col-sm-6">
                        <button class="btn btn-success btn-block" type="submit">=</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <p id="me">Designed and coded by A.Watkin</p>
  </body>
</html>
            
          
!
            
              .calc-container {
  max-width: 460px;

  margin-top: 80px;
  margin-left: auto;
  margin-right: auto;

  border: 2px black solid;
  border-radius: 15px;

}

#button-area {
/*    max-width: 300px;*/
    min-width: 280px;
    margin: 20px;
}

#title {
    text-align: center;
}

#result-area {
    border: solid;
    margin-left: 20px;
    margin-right: 20px;
    padding-right: 4px;
    border-radius: 15px;
}

#result {
    font-family: 'Inconsolata', monospace;
    
/*    margin-top: 2%;*/
    margin-bottom: 0%;
    
/*    padding-top: 2%;*/
    text-align: right;
}

#sub-text {
    font-family: 'Inconsolata', monospace;
    text-align: right;
    margin-bottom: 0%;
}

/* buttons container */
#number-buttons {
/*    border-style: solid;*/
/*    border-color: red;*/
    margin-bottom: 10px;
}

/* number rows and columns spacing */
.col-sm-2 {
    padding-left: 1%;
    padding-right: 1%;
    
}

.col-sm-3 {
    padding-left: 1%;
    padding-right: 1%;
}

.col-sm-6 {
    padding-left: 1%;
    padding-right: 1%;
}

.button-rows {
    margin-bottom: 1%;
    margin-top: 2%;
}

#me {
    margin-top: 15px;
    text-align: center;
}
            
          
!
            
              $( document ).ready(function() {
 
    var combinedValues = [];
    var operators = ['x', '÷', '-', '+'];
    var other = ['Cear', '='];
    var values = [];
    
    var calcValues = [];
    
    var allowPoint = true;
    var allowOperator = true;

    $('.btn').click(function() {
        
        var currentValue = $(this).text();
        var len = values.length;
        
        // check for length greater than 0 and operators
        // only allow operators if length greater than 1
        // also only allow operators if last value was NOT an operator
        
        function isOperator(val) {
            if( operators.indexOf( val ) > -1 ) {
                return true;
            } else {
                return false;
            }
        }
        
        function isNumber(val) {
            if( val != '.' && !isOperator(val) ) {
                if( other.indexOf( val ) == -1 ) {
                    return true;
                } 

            } else {
                return false;
            }
        }
        
        if( isOperator( currentValue ) ) {
            if( len > 0 && isNumber(values[len -1]) ) {
                if( currentValue == '÷') {
                    calcValues.push('/');
                }

                else if ( currentValue == 'x') {
                    calcValues.push('*');
                } else {
                    calcValues.push( currentValue );
                }
                
                // must be an operator and be preceeded by a number
                values.push(currentValue)
                allowPoint = true;
            }
        }

        
        if ( currentValue === '.' && allowPoint ) {
            if( len == 0 ) {
                values.push(0);
                calcValues.push(0);
                values.push( currentValue );
                calcValues.push( currentValue );
                allowPoint = false;
            // check preceeding value is a number
            } else if ( isNumber(values[ len - 1 ]) ){
                values.push( currentValue );
                calcValues.push( currentValue );
                allowPoint = false;
            }
        }
        
        // adds number, checking first for preceeding zeros
        if( isNumber( currentValue ) ) {
            if( currentValue == 0 && len != 0 ) {
                values.push( currentValue );
                calcValues.push( currentValue );
            } else if ( currentValue != 0 ) {
                values.push( currentValue );
                calcValues.push( currentValue );
            }
        }
        
        function findPlace(aStr) {
            for( var i = 0; i < aStr.length; i++ ) {
                var num = parseInt(aStr[i]);
                if( num > 0) {
                    return i;
                }
            }
                
        }
        
        // displays the input
        if ( values.length > 0 ) {
            $('#result').text(values.join(''));
        }
        
        if( currentValue == '=' ) {
            // check the len is 3 or greater
            // check that the last value is a number
            if( len >= 3 && isNumber(values[ len - 1 ]) ) {
                
                
                var copy = calcValues.join('');
                
                var temp = eval(copy);
                
                // this is the whole number
                if ( temp % 1 == 0 ) {
                    $('#result').text( temp ); 
                    
                // this is the decimal
                } else {
                    let strTemp = String(temp);
                    if( strTemp.length > 14 || strTemp[0] == '0' ) {
                        let place = findPlace(strTemp);
                        $('#result').text( temp.toFixed( place + 1 ) );
                        
                    } else {
                        $('#result').text( temp.toFixed(1) );
                    }
                }
            }
        }
        
        // removes all values and clear the screen
        if ( currentValue === 'Clear') {
            $('#result').text(0);
            values = [];
            calcValues = [];
            allowPoint = true;
        }
        
        // ensures the values don't go outside the result area
        if( $('#result').text().length >= 20 ) {
            values = [];
            calcValues = [];
            $('#result').text("Digit limit");   
        }
    });
});
            
          
!
999px
Loading ..................

Console