예제
<script>
/*
[문제]
반복문을 사용해서 보기와 같이 출력하시오.
[보기]
0 3
1 2
2 1
3 3
4 2
5 1
6 3
7 2
8 1
9 3
*/
</script>
HTML
복사
정답
<script>
/*
[문제]
반복문을 사용해서 보기와 같이 출력하시오.
[보기]
0 3
1 2
2 1
3 3
4 2
5 1
6 3
7 2
8 1
9 3
*/
// [for문]
let count = 3;
for(let i = 0; i < 10; i++) {
document.write(i, " ", count, "<br>");
count -= 1;
if(count == 0) {
count = 3;
}
}
document.write("<br>");
// [while문]
let count2 = 3;
let i = 0;
while(i < 10) {
document.write(i, " ", count2, "<br>");
count2 -= 1;
if(count2 == 0) {
count2 = 3;
}
i += 1;
}
</script>
HTML
복사


