본문 바로가기
WEB/JAVASCRIPT

가위바위보 게임 및 간이 계산기 만들기

by 둥바 2022. 6. 27.

가위바위보 맞추기 게임

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>가위,바위,보 맞추기</title>
</head>
<body>
<script type="text/javascript">
document.write("<h1>가위,바위,보 맞추기</h1>");
var t = prompt("가위,바위,보 중 선택하세요.","가위");
var tNum;
switch(t){
case "가위" : 
	tNum = 1;
	break;
case "바위" :
	tNum = 2;
	break;
case "보" :
	tNum = 3;
	break;
default:
	alert("잘못 작성하셨습니다.");
	location.reload(); //새로고침 (현재 페이지)
	break;
}
//Math.random 0 ~ 1 * 3 -> 0~2인데 Math.ceil로 1~3됨
var com = Math.ceil(Math.random() * 3);//컴퓨터
document.write("<img src=../image/math_img_"+com+".png>");
if(tNum == com){
	alert("정답입니다. 축하합니다.");
}else{
	alert("틀렸습니다. 다음 기회에!");
}


</script>
</body>
</html>

 

간이 계산(테스트 받는)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function calc(type){
	var x = Number(document.getElementById("input1").value);
	var y = Number(document.getElementById("input2").value);
	var c;
	switch (type) {
	case 1:
		c = Math.sin((x * Math.PI) / 180.0);
		break;
	case 2:
		c = Math.sin((x * Math.PI) / 180.0);
		break;
	case 3:
		c = Math.tan((x * Math.PI) / 180.0);
		break;
	case 4:
		c = Math.log(x);
		break;
	case 5:
		c = x * y;
		break;
	}
	document.getElementById("output").value = c;
}

function reset(){
	document.getElementById("output").value = "";//빈따옴표처리
	
}
</script>
</head>
<body>
첫번째 값 : <input type="text" id="input1">
두번째 값 : <input type="text" id="input2">
결과 값 : <input type="text" id="output">
<p>
<input type="button" id="sin" value="sin" onclick="calc(1)">
<input type="button" id="cos" value="cos" onclick="calc(2)">
<input type="button" id="tan" value="tan" onclick="calc(3)">
<input type="button" id="Log" value="log" onclick="calc(4)">
<input type="button" id="*" value="*" onclick="calc(5)">
<input type="reset" id="C" value="C" onclick="reset()">


</body>
</html>