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

              
                <header>
  <h1 id='title'>JS Calculator</h1>
</header>
<main id="calc">
  <div id='display'></div>
  <div id='buttons'>
    <button type='button' id='AC'>AC</button>
    <button type='button' id='CE'>&#9003;</button>
    <button type='button' id='/' class='operator'>
      &divide;</button>
    <button type='button' id='*' class='operator'>
      &times;</button>
    <br>
    <button type='button' id='7' class='number'>
      7</button>
    <button type='button' id='8' class='number'>
      8</button>
    <button type='button' id='9' class='number'>
      9</button>
    <button type='button' id='-' class='operator'>-</button>
    <br>
    <button type='button' id='4' class='number'>
      4</button>
    <button type='button' id='5' class='number'>
      5</button>
    <button type='button' id='6' class='number'>
      6</button>
    <button type='button' id='+' class='operator'>+</button>
    <br>
    <button type='button' id='1' class='number'>
      1</button>
    <button type='button' id='2' class='number'>
      2</button>
    <button type='button' id='3' class='number'>
      3</button>
    <button type='button' id='=' class='equals'>=</button>
    <br>
    <button type='button' id='0' class='number zero'>
      0</button>
    <button type='button' id='.' class='operator'>.</button>
      </div>
  </main>
<footer id='copyright'>Created by <a href='https://www.linkedin.com/in/william-kwon-b3ab3b132/' target='_blank'>Will Kwon</a></footer>

              
            
!

CSS

              
                body {
background-color: #cc6666;
font-family: Palatino, Georgia, "Times New Roman";

}

#copyright {
  margin-top: 15px;
  text-align: center;
  color:#fff;
}

#copyright a {
  color: #FFF;
}

#title {
  margin-top: 60px;
  color:#FFF;
  text-align: center;
}

#display {
  text-align: right;
  border: 2px solid #525252;
  background-color: #FFF;
  height: 72px;
  border-radius: 10px;
  font-size: 32px;
  margin: 25px 13px;
  color: #000;
  width: 270px;
  overflow: scroll;
  font-weight: bold;
}

#calc {
  width: 300px;
  height: 350px;
  border: 2px solid #525252;
  border-radius: 15px;
  margin: auto;
  background-color: #66cccc;
}

button{
  width: 60px;
  height: 40px;
  margin: 2px 6px;
  font-size: 15px;
  font-weight: bold;
  border: 0;
  outline: 0;
  border-radius: 5px;
  color: #FFF;
  padding: 1px 2px;
}

button:hover {
  cursor: pointer;
}

#AC {
  background-color: #cb324c;
  }

#CE {
  background-color: #cb6532;
}

.operator, .number {
  background-color: #525252;
}

.equals{
  position: absolute;
  height: 84px;
  border-radius: 5px;
  background-color: #525252;
  }

.zero {
  width: 136px;
}

              
            
!

JS

              
                (function calculator () {
    
    var number=document.getElementsByClassName("number");
    var operator=document.getElementsByClassName("operator");
    var equal=document.getElementById("=");
    var AC=document.getElementById('AC');
    var CE=document.getElementById('CE');
    var numArray=[];
    var display="";
    var total=0;
  
  /*declared variables number, operator, equal, AC, CE to be used for button click event targets, numArray to store input, display to join numArray together into a string and a variable to hold total when equal button is clicked*/
    
    for (var i=0; i<operator.length; i++) {operator[i].addEventListener('click', addOperator)};
    for (var i=0; i<number.length; i++) {number[i].addEventListener('click', addNumber)};
    equal.addEventListener('click', evalThis);
    AC.addEventListener('click', allClear);
    CE.addEventListener('click', backOne);
    window.addEventListener('load', (function(){document.getElementById('display').innerHTML='0';}))
  /*Added eventlistener method to button click event targets tying functions to targets*/
  
    function addNumber() {
    //function to push numbers into numArray//
        number=this.getAttribute('id').toString();
        numArray.slice(-1)[0]===total ? (numArray=[], numArray.push(number)):numArray.push(number);
        updateDisplay(numArray);
    } 
    
    function addOperator () {
      //function to push operators into numArray//
        
        operator=this.getAttribute('id').toString();
        
        switch(true) {
            
            case numArray.slice(-1)[0]===number:
                numArray.push(operator);
                break;
            
            case operator==='.' && numArray.slice(-1)[0]===total:
                numArray=['.'];
                break;
                
            case operator==='.' && numArray.slice(-1)[0]!=='.':
                numArray.push(operator);
                break;
            
            case numArray.slice(-1)[0]===total:
                numArray.push(operator);
                break;
            
            default: console.log('Error');
                    }
      
        updateDisplay(numArray);
    }            

    function updateDisplay(numArray) {
   /*function to update display taking in numArray as an argument*/
        numArray.length===0 ? document.getElementById('display').innerHTML='0':(display=numArray.join(''), document.getElementById('display').innerHTML=display);   
    }

    function evalThis() {
  //function for equal button click evaluates contents of numArray//
        numArray.join('')==="" ? console.log('Error'):(total=Math.round(eval(numArray.join(''))*100000000000000)/100000000000000, numArray=[], numArray.push(total)); 
        updateDisplay(numArray);
    }
    
    function allClear () {
  //function to clear all contents of numArray//
        numArray=[];
        updateDisplay(numArray);
    }

    function backOne () {
  //function to delete last item of numArray//
        numArray.pop();
        updateDisplay(numArray);  
    }
})();
              
            
!
999px

Console