123

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.

            
              <!DOCTYPE html>
<html lang="pt-br">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Calculadora</title>
	<link rel="stylesheet" type="text/css" href="./css/styles.css">
</head>

<body>
  
	<div class="cal">

		<div class="res">
			<input type="text" readonly id="calc" name="calc" value="0" style="display: none"/>
			<input type="text" readonly id="resultado" name="resultado" value="0"/>
		</div>

		<div class="buttons">
			<div class="num-btn">
				<div>
					<input type="button" value="⥪" class="btn-bs" onclick="bs()">
					<input type="button" value="(" onclick="addOper(value)">
					<input type="button" value=")" onclick="addOper(value)">
				</div>
				<div>
					<input type="button" value="1" id="" name="" onclick="addNum(value)" />
					<input type="button" value="2" id="" name="" onclick="addNum(value)" />
					<input type="button" value="3" id="" name="" onclick="addNum(value)" />
				</div>
				<div>
					<input type="button" value="4" id="" name="" onclick="addNum(value)" />
					<input type="button" value="5" id="" name="" onclick="addNum(value)" />
					<input type="button" value="6" id="" name="" onclick="addNum(value)" />
				</div>
				<div>
					<input type="button" value="7" id="" name="" onclick="addNum(value)" />
					<input type="button" value="8" id="" name="" onclick="addNum(value)" />
					<input type="button" value="9" id="" name="" onclick="addNum(value)" />
				</div>
				<div>
					<input type="button" value="." id="" name="" onclick="addOper(value)" />
					<input type="button" value="0" id="" name="" onclick="addNum(value)" />
					<input type="button" value="c" id="" name="" class="btn-c" onclick="clean()" />
				</div>
			</div>

			<div class="side">
				<input type="button" value="=" onclick="Total()" />
				<input type="button" value="+" placeholder="0" onclick="addOper(value)" />
				<input type="button" value="–" onclick="addOper(value)" />
				<input type="button" value="×" onclick="addOper(value)" />
				<input type="button" value="÷" onclick="addOper(value)" />
			</div>
		</div>
	</div>

	<script type="text/javascript" src="./js/main.js"></script>
</body>

</html>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Roboto:400,500,700,900');

* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

body {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-flow: column nowrap;
          flex-flow: column nowrap;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  background-image: url("https://backgroundcheckall.com/wp-content/uploads/2017/12/javascript-background-1.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.cal {
  background: silver;
  width: 257px;
  padding: 10px;
  border-radius: 10px;
  -webkit-box-shadow: 5px 5px 40px black;
          box-shadow: 5px 5px 40px black;
}
.cal .top-buttons input[type="button"],
.cal .buttons input[type="button"] {
  -webkit-box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.438);
          box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.438);
}
.cal .top-buttons {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: end;
      -ms-flex-pack: end;
          justify-content: flex-end;
}
.cal .top-buttons input[type="button"] {
  width: 57px;
  padding: 15px 0;
  border-radius: 10px;
}
.cal .buttons {
  margin-top: 5px;
}
.cal .side {
  float: left;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-flow: column nowrap;
          flex-flow: column nowrap;
  width: 57px;
}
.cal .side input[type="button"] {
  width: 100%;
  padding: 20.5px 0;
  background: #fff;
  color: #212121;
  -webkit-box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.5);
          box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.5);
}
.cal .side input[type="button"]:hover {
  background: #b9b9b9;
}
.cal .side input[type="button"]:focus {
  background: #b9b9b9;
}
.cal .side input[value="="] {
  border-top-right-radius: 10px;
  background: #ff8800;
}
.cal .side input[value="="]:hover {
  background: #008f2b;
}
.cal .side input[value="="]:focus {
  background: #008f2b;
}
.cal .side input[value="/"] {
  border-bottom-right-radius: 10px;
}
.cal .res {
  margin-bottom: 15px;
}
.cal .res input[type="text"] {
  background: none;
  border: 2px solid #212121;
  border-radius: 10px;
  padding: 5px;
  color: #212121;
  width: 243px;
  text-align: right;
  direction: ltr;
  overflow: hidden;
  /* &:focus {
  	box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.41);
  } */
}
.cal .res input[type="text"]:not(:focus) {
  text-align: right;
  direction: rtl;
  unicode-bidi: plaintext;
}
.cal .top-btn input[type="button"] {
  margin: 10px 2.5px 5px;
}
.cal .num-btn {
  float: left;
  width: 200px;
}
.cal .num-btn div {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
      -ms-flex-flow: row nowrap;
          flex-flow: row nowrap;
  width: 100%;
}
.cal .num-btn div input[type="button"] {
  -webkit-box-flex: 1;
      -ms-flex: 1;
          flex: 1;
}
.cal .num-btn div .btn-bs {
  border-top-left-radius: 10px;
}
.cal .num-btn div input[value="."] {
  border-bottom-left-radius: 10px;
}
.cal .num-btn div .btn-c {
  background: #ff2929;
}
.cal .num-btn div .btn-c:hover {
  background: #b40000;
}
.cal .num-btn div .btn-c:focus {
  background: #b40000;
}

.top-btn {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
}

input {
  font-size: 20px;
  outline: none;
}

input[type="button"] {
  background: #212121;
  color: #fff;
  border: none;
  height: 64px;
  text-transform: uppercase;
}
input[type="button"]:hover {
  -webkit-transition: .2s;
  transition: .2s;
  background: #2e2e2e;
}
input[type="button"]:focus {
  -webkit-transition: .2s;
  transition: .2s;
  background: #2e2e2e;
}

            
          
!
            
              res = document.getElementById("resultado");
calc = document.getElementById('calc');

//converte o valor do input e transforma em calculo.
function Total() {
	var resv = res.value;
	var calcv = calc.value;

	res.value = eval(calcv);
	calc.value = eval(calcv);
}

//resgata o sinal de operação e adiciona ao input.
function addOper(oper) {
	slc = res.value;
	sOpers = " " + oper + " ";

	if (res.value == "0") {
		res.value = '';
		calc.value = '';
	}

	if (slc.slice(-1) == ")") {
		switch (oper) {
			case "+":
				res.value = res.value + sOpers;
				calc.value = calc.value + oper;
				break;
			case "–":
				res.value = res.value + "-";
				calc.value = calc.value + "-";
				break;
			case "×":
				res.value = res.value + sOpers;
				calc.value = calc.value + "*";
				break;
			case "÷":
				res.value = res.value + sOpers;
				calc.value = calc.value + "/";
				break;
			default:
				break;
		}
	} else if (isNaN(slc.slice(-1)) || slc.slice(-1) == " ") {
		res.value = res.value;
		calc.value = calc.value;
	} else {
		switch (oper) {
			case "+":
				res.value = res.value + sOpers;
				calc.value = calc.value + oper;
				break;
			case "–":
				res.value = res.value + "-";
				calc.value = calc.value + "-";
				break;
			case "×":
				res.value = res.value + sOpers;
				calc.value = calc.value + "*";
				break;
			case "÷":
				res.value = res.value + sOpers;
				calc.value = calc.value + "/";
				break;
			default:
				res.value = res.value + oper;
				calc.value = calc.value + oper;
				break;
		}

	}
}

//reculpera o valor do botão que fez a ação e adiciona no input.
function addNum(num) {

	//se o valor do input for '0' ele limpa e adiciona o valor resgatado.
	if (res.value == "0") {
		res.value = "";
		calc.value = "";
		res.value = parseInt(num);
		calc.value = parseInt(num);
	} else {
		//caso não seja zero, ele busca o valor atual e concatena com o digitado.
		res.value = res.value + parseInt(num);
		calc.value = calc.value + parseInt(num);
	}
}

//exclui os 3 ultimos ou O ultimo caractere do input, dando a função do backspace
function bs() {
	var resv = document.getElementById('resultado').value;
	var clv = document.getElementById('calc').value;

	//caso o ultimo caractere seja um espaço em branco, ele deleta os 3 ultimos caracteres.
	if (resv.slice(-1) == " ") {
		document.getElementById('resultado').value = resv.substring(0, resv.length - 3);
		document.getElementById('calc').value = clv.substring(0, clv.length - 1);
	} else { //caso não seja, ele deleta apenas o ultimo caractere.
		document.getElementById('resultado').value = resv.substring(0, resv.length - 1);
		document.getElementById('calc').value = clv.substring(0, clv.length - 1);
	}


	//caso não sobre nenhum caractere, ele reseta para o '0'.
	if (res.value == "") {
		calc.value = "0";
		res.value = "0";
	}
}

//substitui todo o valor do input para '0'.
function clean() {
	res.value = "0";
	calc.value = "0";
}

/***************HOTKEY (atalhos no teclado)***************/

//função que lê clicks no teclado.
document.onkeyup = function (e) {
	slc = res.value;
	switch (e.which) {
		case 97://caso a tecla pressionada for key97 (numero 1)
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + '1';
				calc.value = calc.value + '1';
			} else {
				res.value = res.value + '1';
				calc.value = calc.value + '1';
			}
			break;
		case 98://caso a tecla pressionada for key98 (numero 2)
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + '2';
				calc.value = calc.value + '2';
			} else {
				res.value = res.value + '2';
				calc.value = calc.value + '2';
			}
			break;
		case 99:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + '3';
				calc.value = calc.value + '3';
			} else {
				res.value = res.value + '3';
				calc.value = calc.value + '3';
			}
			break;
		case 100:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + '4';
				calc.value = calc.value + '4';
			} else {
				res.value = res.value + '4';
				calc.value = calc.value + '4';
			}
			break;
		case 101:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + '5';
				calc.value = calc.value + '5';
			} else {
				res.value = res.value + '5';
				calc.value = calc.value + '5';
			}
			break;
		case 102:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + '6';
				calc.value = calc.value + '6';
			} else {
				res.value = res.value + '6';
				calc.value = calc.value + '6';
			}
			break;
		case 103:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + '7';
				calc.value = calc.value + '7';
			} else {
				res.value = res.value + '7';
				calc.value = calc.value + '7';
			}
			break;
		case 104:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + '8';
				calc.value = calc.value + '8';
			} else {
				res.value = res.value + '8';
				calc.value = calc.value + '8';
			}
			break;
		case 105:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + '9';
				calc.value = calc.value + '9';
			} else {
				res.value = res.value + '9';
				calc.value = calc.value + '9';
			}
			break;
		case 96:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + '0';
				calc.value = calc.value + '0';
			} else {
				res.value = res.value + '0';
				calc.value = calc.value + '0';
			}
			break;
		case 190:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + '.';
				calc.value = calc.value + '.';
			} else {
				if (isNaN(slc.slice(-1)) || slc.slice(-1) == " ") {
					res.value = res.value;
				} else {
					res.value = res.value + '.';
					calc.value = calc.value + '.';
				}
			}
			break;
		case 107:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + ' + ';
				calc.value = calc.value + '+';
			} else {
				if (slc.slice(-1) == ")") {
					res.value = res.value + ' + ';
					calc.value = calc.value + '+';
				} else if (isNaN(slc.slice(-1)) || slc.slice(-1) == " ") {
					res.value = res.value;
				} else {
					res.value = res.value + ' + ';
					calc.value = calc.value + '+';
				}
			}
			break;
		case 109:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + ' - ';
				calc.value = calc.value + '-';
			} else {
				if (slc.slice(-1) == ")") {
					res.value = res.value + ' - ';
					calc.value = calc.value + '-';
				} else if (isNaN(slc.slice(-1)) || slc.slice(-1) == " ") {
					res.value = res.value;
				} else {
					res.value = res.value + ' - ';
					calc.value = calc.value + '-';
				}
			}
			break;
		case 106:
		case 88:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + ' × ';
				calc.value = calc.value + '*';
			} else {
				if (slc.slice(-1) == ")") {
					res.value = res.value + ' × ';
					calc.value = calc.value + '*';
				} else if (isNaN(slc.slice(-1)) || slc.slice(-1) == " ") {
					res.value = res.value;
				} else {
					res.value = res.value + ' × ';
					calc.value = calc.value + '*';
				}
			}
			break;
		case 111:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + ' ÷ ';
				calc.value = calc.value + '/';
			} else {
				if (slc.slice(-1) == ")") {
					res.value = res.value + ' ÷ ';
					calc.value = calc.value + '/';
				} else if (isNaN(slc.slice(-1)) || slc.slice(-1) == " ") {
					res.value = res.value;
				} else {
					res.value = res.value + ' ÷ ';
					calc.value = calc.value + '/';
				}
			}
			break;
		case e.shiftKey && 57:
			if (res.value == "0") {
				res.value = "";
				calc.value = "";
				res.value = res.value + '(';
				calc.value = calc.value + '(';
			} else {
				if (isNaN(slc.slice(-1)) || slc.slice(-1) == " ") {
					res.value = res.value;
				} else {
					res.value = res.value + '(';
					calc.value = calc.value + '(';
				}
			}
			break;
		case e.shiftKey && 48:
			if (res.value == "0") {
				res.value = "0";
			} else {
				if (isNaN(slc.slice(-1)) || slc.slice(-1) == " ") {
					res.value = res.value;
				} else {
					res.value = res.value + ')';
					calc.value = calc.value + ')';
				}
			}
			break;
		case 13:
			Total();
			break;
		case 8:
			bs();
			break;
		case 46:
		case 67:
			clean();
			break;
		default:
			break;
	}
}
            
          
!
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.

Console