Web/HTML_CSS_jQuery

HTML 연습

SZCODE 2020. 4. 1. 01:39
임의의 정수 한 개를 발생시켜 변수에 저장한 후 사용자가 값을 입력해 알아 맞히는 프로그램

 

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <title>Insert title here</title>
      <script type="text/javascript">
         let answer = Math.floor(Math.random()*10);
         let times = 0;
         function match(){
            let input = Number(document.getElementById("input").value);
            let output = document.getElementById("output");
            times++;
            if (input == answer) {
               output.value = "정답입니다. \n 시도횟수 : "+times;
               
            }
            else {
               output.value = "다시 시도해보세요."
            }
         }
      </script>
   </head>
   <body>
      <p>0~9중 값을 맞추세요.</p>
      <input type="text" id = "input"><br>
      <button onclick = "match()"> 제출 </button>
         <br>
      <textarea id = "output"></textarea>
   </body>
</html>