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

E0504_개념07_반복문맥스개수

개념

<script> /* [반복문 맥스 개수] - 숫자 1부터 10 사이의 랜덤 숫자를 10번 출력하고, 그중에서 가장 큰 수를 출력하시오. - 단, 가장 큰 수가 여러 번 나오는 경우 중복 출력된 횟수도 함께 출력하시오. */ /* [출력예시] 3 5 2 7 10 7 8 7 6 10 가장 큰 수 = 10 반복 횟수 = 2 */ // [for문] let count = 0; let max = 1; for(let i = 1; i <= 10; i++) { let r = Math.floor(Math.random() * 10) + 1; document.write(r, " "); if(max < r) { max = r; count = 1; } else if(max == r) { count += 1; } } document.write("<br>"); document.write(max, " : ", count, "개<br>"); document.write("<br>"); // [while문] let count2 = 0; let max2 = 0; let i = 1; while(i <= 10) { let r = Math.floor(Math.random() * 10) + 1; document.write(r, " "); if(max2 < r) { max2 = r; count2 = 1; } else if(max2 == r) { count += 1; } i += 1; } document.write("<br>"); document.write(max2, " : ", count2, "개"); </script>
HTML
복사

영상