간이계산기 만들기 4
전체코드
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>실습문제1</title>
</head>
<body>
<h1>간이계산기 만들기</h1>
첫번째 값: <input type="text" id="num1"> <br>
두번째 값: <input type="text" id="num2"> <br>
<button onclick="calculate(this)">+</button>
<button onclick="calculate(this)">-</button>
<button onclick="calculate(this)">*</button>
<button onclick="calculate(this)">/</button>
<button onclick="calculate(this)">%</button>
<br><br>
계산결과: <span id="result"></span>
<script>
function calculate(btn){
const op = btn.innerText;
const num1 = Number(document.getElementById("num1").value);
const num2 = Number(document.getElementById("num2").value);
document.getElementById("result").innerHTML= new Function("return " + num1 + op + num2)();
}
</script>
</body>
</html>
