JAVASCRIPT

JAVASCRIPT while문 사용하기

JDK's blog 2015. 11. 22. 23:35

안녕하세요.

이번엔 for문에 이어서 while문사용하는 방법을 알려드릴꼐요.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<meta charset="utf-8"/>
<html>
<head>
<title>JDK의 블로그</title>
<script language="javascript">
//버튼클릭시 if_javascript 호출합니다.
function if_javascript(){
 
    //txt_Value값을 가져옵니다.
    var value = document.getElementById("txt_Value").value;
 
    var result;    //결과
    var i = 1;    //? * i
    while(i < 10){
        result = value * i;
        console.log(value+"*"+i+"="+result);
        i++;
    }
}
</script>
</head>
<body>
<input type='text' id='txt_Value'/>
<input type='button' onclick='if_javascript()' value='버튼'/>
</body>
</html>
cs

for문과 같이 구할 단수를 입력하고, 버튼을 클릭하면 결과를 뿌려줍니다.

while문 같은 경우엔 그 안에 있는 조건. 즉, i가 10보다 작을 경우 계속 반복해요.

 

 

 구하고자 하는 단수를 입력하고, 버튼을 클릭하면..

이렇게 결과가 나와요.