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

E0301_예제01_문제

예제

<script> /* [문제] 반복문을 사용하여 5부터 15 사이의 숫자 중 4의 배수를 찾아 출력하고, 그 개수도 함께 출력하시오. */ /* [출력예시] 8 12 2 */ </script>
HTML
복사

정답

<script> /* [문제] 반복문을 사용하여 5부터 15 사이의 숫자 중 4의 배수를 찾아 출력하고, 그 개수도 함께 출력하시오. */ /* [출력예시] 8 12 2 */ // [for문] let count = 0; for(let i = 5; i <= 15; i++) { if(i % 4 == 0) { document.write(i, " "); count += 1; } } document.write("<br>"); document.write(count); document.write("<br>"); // [while문] let count2 = 0; let i = 5; while(i <= 15) { if(i % 4 == 0) { document.write(i, " "); count2 += 1; } i += 1; } document.write("<br>"); document.write(count2); </script>
HTML
복사