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

D0403_점검01_문제

점검

<script> /* [문제] 랜덤 숫자 4개를 저장한 후, 가장 첫 번째 홀수를 출력하시오. */ /* [출력예시] 9 5 7 6 9 */ </script>
HTML
복사

정답

<script> /* [문제] 랜덤 숫자 4개를 저장한 후, 가장 첫 번째 홀수를 출력하시오. */ /* [출력예시] 9 5 7 6 9 */ let r = Math.floor(Math.random() * 10) + 1; let r2 = Math.floor(Math.random() * 10) + 1; let r3 = Math.floor(Math.random() * 10) + 1; let r4 = Math.floor(Math.random() * 10) + 1; document.write(r, " ", r2, " ", r3, " ", r4, "<br>"); let count = 0; let a = 0; if(count == 0 && r % 2 == 1) { a = r; count += 1; } if(count == 0 && r2 % 2 == 1) { a = r2; count += 1; } if(count == 0 && r3 % 2 == 1) { a = r3; count += 1; } if(count == 0 && r4 % 2 == 1) { a = r4; count += 1; } document.write(a); </script>
HTML
복사