자바스크립트
home
2025 자바스크립트 초급 1500제
home

E0305_개념01_개수의초기화와나머지for문

개념

<script> /* [개수의 초기화와 나머지 for문] - 숫자 1부터 9까지의 숫자를 출력하시오. - 단, 한 줄에 숫자 세 개씩만 출력하고, 숫자 세 개를 출력할 때마다 줄을 바꾸어 계속 출력하시오. */ /* [출력예시] 1 2 3 4 5 6 7 8 9 */ // (1) % 로 처리 let count = 0; for(let i = 1; i < 10; i++) { document.write(i, " "); if(i % 3 == 0) { document.write("<br>"); } } document.write("<br>"); // (2) count로 처리 let count2 = 0; for(let i = 1; i < 10; i++) { document.write(i, " "); count2 += 1; if(count2 == 3) { document.write("<br>"); count2 = 0; } } </script>
HTML
복사

영상