예제
<script>
/*
[문제]
1부터 12까지의 숫자를 출력하시오.
단, 한 줄에 숫자 네 개씩만 출력하고,
네 개를 출력할 때마다 줄을 바꾸어 이어서 출력하시오.
*/
/*
[출력예시]
1 2 3 4
5 6 7 8
9 10 11 12
*/
</script>
HTML
복사
정답
<script>
/*
[문제]
1부터 12까지의 숫자를 출력하시오.
단, 한 줄에 숫자 네 개씩만 출력하고,
네 개를 출력할 때마다 줄을 바꾸어 이어서 출력하시오.
*/
/*
[출력예시]
1 2 3 4
5 6 7 8
9 10 11 12
*/
// [for문]
let count = 0;
for(let i = 1; i <= 12; i++) {
document.write(i, " ");
count += 1;
if(count == 4) {
document.write("<br>");
count = 0;
}
}
document.write("<br>");
// [while문]
let count2 = 0;
let i = 1;
while(i <= 12) {
document.write(i, " ");
count2 += 1;
if(count2 == 4) {
document.write("<br>");
count2 = 0;
}
i += 1;
}
</script>
HTML
복사


