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.

+ 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.

            
              <div class="container">
  <div class="calculator">
    <div class="display">
    <div class="disp1" id="disp1"></div>
    <div class="disp2" id="disp2"></div>
    </div>
      
    <button value="ac">ac</button>
    <button value="ce">ce</button>
    <button value="/">/</button>
    <button value="*">*</button>

    <button value="7">7</button>
    <button value="8">8</button>
    <button value="9">9</button>
    <button value="-">-</button>

    <button value="4">4</button>
    <button value="5">5</button>
    <button value="6">6</button>
    <button value="+">+</button>

    <button value="1">1</button>
    <button value="2">2</button>
    <button value="3">3</button>
    <button value="=" class="doubleheight">=</button>

    <button value="0" class="doublewidth">0</button>
    <button value=".">.</button>


  </div>
</div>
            
          
!
            
              body {
  background-color: indianred; 
  
}

.disp1 {
  height: 60px;
  text-align: right;
  font-size:30px;
  
}
.disp2 {
  height: 20px;
  text-align: right;
  font-size:15px;
}

.calculator {
  min-width: 216px;
  max-width: 216px;
  width: 216px;
  margin: 80px auto;
  padding: 15px;
  background-color: darkgrey;
  font-family: monospace;
  display:block;
}

button {
  display: inline-block;
  height: 40px;
  width: 40px;
  text-align: center;
  font-size: 18px;
  margin: 4px;
  border: none;
  font-family: monospace;
  background-color:white;
  color: #444444;
    
}

button:hover {
  background-color:whitesmoke;
}
button:focus {
  outline:none;
  background-color:lightgrey;
}

.display {
  color: whitesmoke;
background-color: #444444;
  margin: 4px 4px 16px 4px;
  padding: 4px;
}

.doublewidth {
  width: 96px;
}

.doubleheight {
  height: 88px;
  position: absolute;
}
            
          
!
            
              $(document).ready(function() {
  var disp1 = $("#disp1");
  var disp2 = $("#disp2");
  var mem1 = ""; //short term memory for disp1 top display
  var mem2 = []; //long term memory for disp2 lower display
  var i = 0;
  var decimal = false; // got to make sure only one decimal per number!
  var operator = false; // keeping track of how many operators in a row.
  var equals = false; // keeping track of whether equals has just been pressed

  $("button").click(function() {
    var input = $(this).attr("value");

    switch (input) {
      case ".":
        if (!decimal) {
          decimal = true;
          mem1 += input;
        }
        break;

      case "ac": // make clear function
        mem1 = "";
        mem2 = [];
        i = 0;
        decimal = false;
        break;

      case "ce":
        if (i > 0) {
          mem1 = "";
          decimal = false;
        }
        mem1 = "";
        break;

      case "+":
      case "-":
      case "*":
      case "/":
        if (!operator) {
          mem2[i] = mem1;
          mem2[i + 1] = input;
          mem1 = "";
          i += 2;
          decimal = false;
          operator = true;
        }
        break;

      case "=":
        if (!operator) {
          mem2[i] = mem1;
          mem1 = Math.round(eval(mem2.join("")).toString() * 10000 ) / 10000;
          mem2 = [];
          i = 0;
          decimal = false;
          equals = true;
        }
        break;

      default:
        // if input is number or decimal
        if (equals) {
          mem1 = input;
          operator = false;
          equals = false;
        } else {
          mem1 += input;
          operator = false;
        }
    }

    console.log("mem1 length is: " + mem1.length);
    console.log(mem1);
    console.log("mem2 length is: " + mem2.join("").length);
    console.log(mem2);
    
    if (mem1.toString().length > 10 || mem2.join("").length > 18) {
      disp1.html("Too long!");
      disp2.html("That's what she said!");
    } else {
      disp1.html(mem1);
      disp2.html(mem2.join(""));
    }
    
    
  });
});

            
          
!
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