예제
<script>
/*
[문제]
10부터 20 사이의 랜덤 숫자를 10번 출력하고,
일의 자리가 가장 큰 수를 출력하시오.
단, 가장 큰 수가 여러 번 나오는 경우
중복 출력된 횟수도 함께 출력하시오.
*/
/*
[출력예시]
17 13 11 17 18 13 15 17 18 12
18 : 2개
*/
</script>
HTML
복사
정답
<script>
/*
[문제]
10부터 20 사이의 랜덤 숫자를 10번 출력하고,
일의 자리가 가장 큰 수를 출력하시오.
단, 가장 큰 수가 여러 번 나오는 경우
중복 출력된 횟수도 함께 출력하시오.
*/
/*
[출력예시]
17 13 11 17 18 13 15 17 18 12
18 : 2개
*/
// [for문]
let count = 0;
let maxNum = 0;
let max = 0;
for(let i=1; i<10; i++) {
let num = Math.floor(Math.random() * 11) + 10;
document.write(num, " ");
let _1 = num % 10;
if(max < _1) {
maxNum = num;
max = _1;
count = 1;
} else if(max == _1) {
count += 1;
}
i += 1;
}
document.write("<br>");
document.write(maxNum, " : ", count, "개<br>");
document.write("<br>");
// [while문]
let count2 = 0;
let maxNum2 = 0;
let max2 = 0;
let i = 1;
while(i <= 10) {
let r = Math.floor(Math.random() * 11) + 10;
document.write(r, " ");
let _1 = r % 10;
if(max2 < _1) {
maxNum2 = r;
max2 = _1;
count2 = 1;
} else if(max2 == _1) {
count2 += 1;
}
i += 1;
}
document.write("<br>");
document.write(maxNum2, " : ", count2, "개");
</script>
HTML
복사


