css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container js-container">
      <div class="view js-view">View</div>
      <div class="mini-view">mini-view</div>
      <div class="cell clear">C</div>
      <div id = "divide" class="cell math divide">/</div>
      <div id = "multiply" class="cell math multiply">X</div>
      <div id = "add" class="cell math add">+</div>
      <div class="cell">7</div>
      <div class="cell">8</div>
      <div class="cell">9</div>
      <div id= "subtract" class="cell math subtract">-</div>
      <div class="cell">4</div>
      <div class="cell">5</div>
      <div class="cell">6</div>
      <div class="cell">.</div>
      <div class="cell">1</div>
      <div class="cell">2</div>
      <div class="cell">3</div>

      <div class="cell">0</div>

      <div class="cell equal">=</div>
    </div>
            
          
!
            
              .container{
  display:flex;
  flex-flow: row wrap;
  height:650px;
  width:400px;
}
.view{
  height: 100px;
  width:400px;
}
.mini-view{
  height:50px;
  width:400px;
}

.cell{
  height:100px;
  width:100px;
  margin:auto;
  font-size:2em;
}

.equal{
  background-color:blue;
  width:400px;
  height:100px;
  text-align:center;
  font-size:5em;
  color:white;
}

            
          
!
            
              const view = {
  //Updates view when buttons are clicked
  updateView: function(){
    let viewScreen = document.getElementsByClassName('js-view')[0];
    let miniView = document.getElementsByClassName('mini-view')[0];
    viewScreen.innerHTML = '';
    miniView.innerHTML = '';
    const jsContainer = document.getElementsByClassName('js-container')[0];
    jsContainer.addEventListener('click', function(e){
      let show = e.target.innerHTML;
      viewScreen.innerHTML += show;
    });
  },
  //have a handler that sets what each button does with event delegation
  btnHandle: function(){
    let mathType = {"type": undefined};
    let mathHoldOne = {"num": 0, "set": false};
    let mathHoldTwo = {"num": 0, "set": false};
    let btnHandler = document.querySelector('.js-container');

    btnHandler.addEventListener('click', function(event){
      let btn = event.target;
      let screenValue = document.querySelector('.js-view');
      let miniView = document.querySelector('.mini-view');
      switch(btn.className){
        //clears whats in the view window
        case('cell clear'):
          screenValue.innerHTML = '';
          miniView.innerHTML = '';
          mathHoldOne.num = 0;
          mathHoldOne.set = false;
          mathHoldTwo.num  = 0;
          mathHoldTwo.set = false;
          mathType.type = undefined;
          break;
        case('cell math multiply'):
        //assigns mathHoldTwo.num if mathHoldOne.set is true and blanks the screenValue
          if(mathHoldOne.set){
            mathHoldTwo.num = parseInt(screenValue.innerHTML);
            mathHoldTwo.set = true;
            screenValue.innerHTML = '';
            //if mathHoldOne.set is false it assigns mathHoldOne.num and sets the set property to true
            //also sets mathType.type to multiply
          }else{
            mathHoldOne.num = parseInt(screenValue.innerHTML);
            mathHoldOne.set = true;
            screenValue.innerHTML = '';
            mathType.type = "multiply";
          }
          if(mathHoldOne.set && mathHoldTwo.set){
            //if both numbers are set cycle through calcFunc to find which mathType.type matches
            //and execute that function with the two values
            for(let name in calcFunc){
              if(mathType.type === name){
                miniView.innerHTML = calcFunc[name](mathHoldOne.num, mathHoldTwo.num);
                mathHoldOne.num = calcFunc[name](mathHoldOne.num, mathHoldTwo.num);
              }
            }
            mathHoldTwo.num = 0;
            mathHoldTwo.set = false;
            mathType.type = 'multiply';
          }
          break;
        case('cell math divide'):
          if(mathHoldOne.set){
            mathHoldTwo.num = parseInt(screenValue.innerHTML);
            mathHoldTwo.set = true;
            screenValue.innerHTML = '';
          }else{
            mathHoldOne.num = parseInt(screenValue.innerHTML);
            mathHoldOne.set = true;
            screenValue.innerHTML = '';
            mathType.type = "divide";
          }
          if(mathHoldOne.set && mathHoldTwo.set){
            for(let name in calcFunc){
              if(mathType.type === name){
                miniView.innerHTML = calcFunc[name](mathHoldOne.num, mathHoldTwo.num);
                mathHoldOne.num = calcFunc[name](mathHoldOne.num, mathHoldTwo.num);
              }
            }
            mathHoldTwo.num = 0;
            mathHoldTwo.set = false;
            mathType.type = 'divide';
          }
          break;
        case('cell math add'):
          if(mathHoldOne.set){
            mathHoldTwo.num = parseInt(screenValue.innerHTML);
            mathHoldTwo.set = true;
            screenValue.innerHTML = '';
          }else{
            mathHoldOne.num = parseInt(screenValue.innerHTML);
            mathHoldOne.set = true;
            screenValue.innerHTML = '';
            mathType.type = "add";
          }
          if(mathHoldOne.set && mathHoldTwo.set){
            for(let name in calcFunc){
              if(mathType.type === name){
                miniView.innerHTML = calcFunc[name](mathHoldOne.num, mathHoldTwo.num);
                mathHoldOne.num = calcFunc[name](mathHoldOne.num, mathHoldTwo.num);
              }
            };
            mathHoldTwo.num = 0;
            mathHoldTwo.set = false;
            mathType.type = 'add';
          }
          break;
        case('cell math subtract'):
          if(mathHoldOne.set){
            mathHoldTwo.num = parseInt(screenValue.innerHTML);
            mathHoldTwo.set = true;
            screenValue.innerHTML = '';
          }else{
            mathHoldOne.num = parseInt(screenValue.innerHTML);
            mathHoldOne.set = true;
            screenValue.innerHTML = '';
            mathType.type = "subtract";
          }
          if(mathHoldOne.set && mathHoldTwo.set){
            for(let name in calcFunc){
              if(mathType.type === name){
                miniView.innerHTML = calcFunc[name](mathHoldOne.num, mathHoldTwo.num);
                mathHoldOne.num = calcFunc[name](mathHoldOne.num, mathHoldTwo.num);
              }
            };
            mathHoldTwo.num = 0;
            mathHoldTwo.set = false;
            mathType.type = 'subtract';
          }
          break;
        case('cell equal'):
        mathHoldTwo.num = parseInt(screenValue.innerHTML);
          if(mathType.type === "add"){
            screenValue.innerHTML = calcFunc.add(mathHoldOne.num, mathHoldTwo.num);
            miniView.innerHTML = calcFunc.add(mathHoldOne.num, mathHoldTwo.num);
            mathHoldTwo.num = 0;
            mathHoldOne.num = 0;
            mathHoldOne.set = false;
          }else if(mathType.type === "subtract"){
            // screenValue.innerHTML = calcFunc.subtract(mathHoldOne.num, mathHoldTwo.num);
            // miniView.innerHTML = calcFunc.subtract(mathHoldOne.num, mathHoldTwo.num);
            // mathHoldOne.num = (mathHoldOne.num - mathHoldTwo.num);
            // mathHoldTwo.num = 0;
            // mathHoldOne.num = 0;
            // mathHoldOne.set = false;
            screenValue.innerHTML = calcFunc.subtract(mathHoldOne.num, mathHoldTwo.num);
            miniView.innerHTML = calcFunc.subtract(mathHoldOne.num, mathHoldTwo.num);
            mathHoldTwo.num = 0;
            mathHoldOne.num = 0;
            mathHoldOne.set = false;
          }
          else if(mathType.type === "multiply"){
            screenValue.innerHTML = calcFunc.multiply(mathHoldOne.num, mathHoldTwo.num);
            miniView.innerHTML = calcFunc.multiply(mathHoldOne.num, mathHoldTwo.num);
            mathHoldOne.num = (mathHoldOne.num * mathHoldTwo.num);
            mathHoldTwo.num = 0;
            mathHoldOne.num = 0;
            mathHoldOne.set = false;
          }else if(mathType.type === "divide"){
            screenValue.innerHTML = calcFunc.divide(mathHoldOne.num, mathHoldTwo.num);
            miniView.innerHTML = calcFunc.divide(mathHoldOne.num, mathHoldTwo.num);
            mathHoldOne.num = (mathHoldOne.num / mathHoldTwo.num);
            mathHoldTwo.num = 0;
            mathHoldOne.num = 0;
            mathHoldOne.set = false;
          }
          break;
      }
      console.log(mathHoldOne, mathHoldTwo, mathType.type);
    })
  }

}
view.updateView();
view.btnHandle();
const calcFunc = {
  add: function(x, y){
    return x + y;
  },
  subtract: function(x,y){
    return x-y;
  },
  multiply: function(x, y){
    return x * y;
  },
  divide: function(x,y){
    return x/y;
  },
  clear: function(){
    let view = document.querySelector('js-view');
    view.innerHTML = '';
  }
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console