WEB2 - JavaScript - 반복문, 배열과 반복문
반복문 - https://opentutorials.org/course/3085/18827
특정 코드를 반복해야 할 때 반복문을 사용하여 간편하게 반복시킬 수 있다.
<h1>Loop</h1>
<ul>
<script>
document.write('<li>1</li>');
var i = 0;
while(i < 3){ //괄호안의 내용이 false가 될때까지 중괄호 내용을 반복 시킨다.
document.write('<li>2</li>');
document.write('<li>3</li>');
i = i + 1; //반복문을 끝내기 위해 i값을 계속 올려준다. i값이 2가 되면 반복문은 종료된다.
}
document.write('<li>4</li>');
</script>
[실행결과]
Loop
- 1
- 2
- 3
- 2
- 3
- 2
- 3
- 4
배열과 반복문 - https://opentutorials.org/course/3085/18828
배열안의 내용을 반복문으로 하나씩 꺼내서 반복해주는 예제이다.
<h1>Loop & Array</h1>
<script>
var coworkers = ['egoing','leezche','duru','taeho']; //서로 연관된 데이터를 배열로 담는다.
</script>
<h2>Co workers</h2>
<ul>
<script>
var i = 0;
while(i < coworkers.length){ //coworkers의 배열크기임.
document.write('<li><a href="http://a.com/'+coworkers[i]+'">'+coworkers[i]+'</a></li>');
i = i + 1;
}
</script>
</ul>
[결과화면]