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

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

개념

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

영상