728x90
반응형
<!-- src/main/webapp/2/mathex1.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Math 클래스 예제</title>
<script type="text/javascript">
document.write(3/2) //화면에 1.5 출력
document.write(eval("3+5")) //화면에 8 출력
/*
Math.random() : 0 <= x < 1.0 임의의 수
Math.floor() : 근사정수. 입력된 수보다 작은 근사 정수
Math.ceil() : 근사정수. 입력된 수보다 큰 근사 정수
*/
function getNumber() { // 0 ~ 9 사이의 임의의 수를
return Math.floor(Math.random() * 1000) % 10
}
//배열 : 0부터 length-1 까지의 인덱스 값.
let op = new Array("+", "-", "*", "/", "%") //배열
function getOp() { //op배열의 한개값을 리턴
return op[Math.floor(Math.random() * op.length)]
}
str = getNumber() + getOp() + getNumber(); //수식 => ex) 9 - 3
//prompt : 입력가능한 대화창
//str + "=" : 메세지 부분
// 0 : 초기화 값
ans = prompt(str + "=",0)
//parseInt : 정수 <= 문자열
//eval(str) : 문자열을 수식으로 변환하여 계산한 결과
res = parseInt(eval(str)) //정답
if(res != ans) {
document.write("입력한값:" + ans + "<br>")
document.write("<font color='red' size='5'> 틀렸습니다.</font><br>")
document.write("정답:" + res)
} else
document.write("<font color='blue' size='7'> 정답입니다.</font>")
</script>
</head>
<body>
</body>
</html>
728x90
반응형