Java Script/work

변수의 연산

웨이칭 2020. 7. 9. 15:02

 

 

 

 

 

 

 

 

숫자만 입력받아 버튼을 누르면 자동 연산되는 프로그램

 

 

 

<body>

<h3>변수의 연산</h3>
a, b에 대해서 변수 a와 b의 계산 결과를 표시합니다.
<br><br>
<button type="button" onclick="plus()">덧셈</button>
<button type="button" onclick="minus()">뺄셈</button>
<button type="button" onclick="multi()">곱셈</button>
<button type="button" onclick="div()">나눗셈</button>
<button type="button" onclick="rest()">나머지</button>
<br><br>

<input type="text" id="a" size="10">
<input type="text" id="oper" size="5">
<input type="text" id="b" size="10">
=<input type="text" id="result">

<script type="text/javascript">
let a, b;

function getValue() {
	a = parseInt(document.getElementById("a").value);
	b = parseInt(document.getElementById("b").value);
}

function plus() {
	getValue();
	document.getElementById('oper').value = '+';
	document.getElementById('result').value = a + b;	
}
function minus() {	
}
function multi() {	
}
function div() {
	getValue();
	if(b == 0){
		alert('계산할 수 없습니다');
		return;
	}
	
	document.getElementById('oper').value = '/';
	document.getElementById('result').value = a / b;
}
function rest() {
	
}
</script>
</body>