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">

		<!-- show numbers and calculation -->
		<div class="show-calc" id="display">0</div>

		<!-- numbers div -->

		<div id="zero" class="box box-num">0</div>
		<div id="one" class="box box-num">1</div>
		<div id="two" class="box box-num">2</div>
		<div id="three" class="box box-num">3</div>
		<div id="four" class="box box-num">4</div>
		<div id="five" class="box box-num">5</div>
		<div id="six" class="box box-num">6</div>
		<div id="seven" class="box box-num">7</div>
		<div id="eight" class="box box-num">8</div>
		<div id="nine" class="box box-num">9</div>
		<div id="decimal" class="box box-num">.</div>

		<!-- Operators div -->
		<div id="add" class="box box-opr">+</div>
		<div id="subtract" class="box box-opr">-</div>
		<div id="multiply" class="box box-opr">x</div>
		<div id="divide" class="box box-opr">/</div>
		<div id="equals" class="box box-opr">=</div>

		<!--  -->

		<div class="box box-reset" id="reset">reset</div>
		<div class="box box-clear" id="clear">clear</div>

	</div>
            
          
!
            
              .container {
			width: 30%;
			margin: 0 auto;
			margin-top: 20px;
			background: #ccc;
			display: flex;
			flex-wrap: wrap;
			justify-content: center;
			align-content: center;
			border-radius: 20px 20px 0px 0px;
			box-shadow: 0px -2px 0px 2px darkred;
			text-align: center;
			color: darkred;
      font: 15px/1.5 Arial, Helvetica, sans-serif;
		}


		.container .show-calc {
			min-width: 100%;
			margin: 20px;
			font-size: 35px;
		}

#reset, #clear {
  width: 100%;
}

		.container .box {
			width: 5vw;
			height: 5vw;
			margin: 10px;
			font-size: 25px;
			cursor: pointer;
		}


		.container .box:hover {
			transform: scale(1.07,1.07);
			transition: 0.7s;
		}


@media screen and (max-width: 700px) {
  
  .container {
    width: 40%;
  }
  
  #reset, #clear {
    width: 100%;
  }
}
            
          
!
            
              // create out variables 
		var num1 = 0, num2 = 0, opr = " ",
		divNumbers = document.querySelectorAll(".container .box-num"),
		divOpers = document.querySelectorAll(".container .box-opr"),
		showCalcBox = document.getElementById("display"),
		resetBox = document.getElementById("reset"),
		clearBox = document.getElementById("clear"),
		// isOprClick = check if an operator is clicked
		// isEqClick = check if an equals is clicked
		// fc0 = first click click on operator
		isOprClick = false, isEqClick = false, fco = 0;


		// add action to all div
		//clear show-calc letter by letter

		resetBox.onclick = function () {
			showCalcBox.innerHTML = showCalcBox.innerHTML.substring(0, showCalcBox.innerHTML.length -1);
		};

		// reset div to clear and start again

		clearBox.onclick = function () {
			isOprClick = false; 
			isEqClick = false; 
			fco = 0;
			num1 = 0; 
			num2 = 0; 
			opr = " ";

			display.innerHTML = "0";
		};

		// add action to show numbers 

		for (var i = 0; i < divNumbers.length; i++) {

			divNumbers[i].onclick = function () {

				if (isOprClick) {
					num1 = eval(display.innerHTML);
					display.innerHTML = " ";
				}


				// check if the text don't contain "."

				if(display.innerHTML.toString().indexOf(".") === -1) {

					// id text is equal tp 0 and the div pressed is not "."

					if (display.innerHTML === "0" && this.innerHTML !== ".") {
						display.innerHTML =  this.innerHTML;
						isOprClick = false;
					} else {
						display.innerHTML = display.innerHTML + this.innerHTML;
						isOprClick = false;
					}
					
				} else if (this.innerHTML !== ".") {
					display.innerHTML = display.innerHTML + this.innerHTML;
					isOprClick = false;
				}
				
			}
		}

		// add action to do operations

		for (var i = 0; i < divOpers.length; i++) {
			divOpers[i].onclick = function () {
				if (fco == 0) {
					fco++;
					num1 = eval(display.innerHTML);
					// get the operator
					opr = this.innerHTML;
					isOprClick = true;
				} else {
					if (this.innerHTML!== "=") { // if the clicked div is not "="
						if (!isEqClick) { // if "=" is not already clicked
							num2 = eval(display.innerHTML);
							display.innerHTML = calc(opr, num1, num2);
							opr = this.innerHTML;
							num2 = eval(display.innerHTML);
							isOprClick = true;
							isEqClick = false;
						} else {
							isEqClick = false;
							opr = this.innerHTML;
						}
					} else {
							num2 = eval(display.innerHTML);
							display.innerHTML = calc(opr, num1, num2);
							opr = this.innerHTML;
							num1 = eval(display.innerHTML);
							isOprClick = true;
							isEqClick = true;
					}
				}
			}
		}

		// creat a clac function to the operations

		function calc(op,n1,n2) {
			var result = 0;

			switch(op) {
				case "+": 
					result = n1 + n2;
				break;

				case "-": 
					result = n1 - n2;
				break;

				case "x": 
					result = n1 * n2;
				break;

				case "/": 
					if(n2 > 0)
					result = n1 / n2;

				break;
			}

			return result;
		}
            
          
!
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