점검
<script>
/*
[문제]
110부터 190 사이의 랜덤 숫자를 10번 출력하고,
십의 자리가 가장 큰 수를 출력하시오.
단, 가장 큰 수가 여러 번 나오는 경우
중복 출력된 횟수도 함께 출력하시오.
*/
/*
[출력예시]
122 113 171 159 150 182 165 186 166 189
182 : 3개
*/
</script>
HTML
복사
정답
<script>
/*
[문제]
110부터 190 사이의 랜덤 숫자를 10번 출력하고,
십의 자리가 가장 큰 수를 출력하시오.
단, 가장 큰 수가 여러 번 나오는 경우
중복 출력된 횟수도 함께 출력하시오.
*/
/*
[출력예시]
122 113 171 159 150 182 165 186 166 189
182 : 3개
*/
// [for문]
let count = 0;
let maxNum = 0;
let max = 0;
for(let i=1; i<=10; i++) {
let r = Math.floor(Math.random() * 81) + 110;
document.write(r, " ");
let _10 = parseInt(r % 100 / 10);
if(max < _10) {
maxNum = r;
max = _10;
count = 1;
} else if(max == _10) {
count += 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() * 81) + 110;
document.write(r, " ");
let _10 = parseInt(r % 100 / 10);
if(max2 < _10) {
maxNum2 = r;
max2 = _10;
coun2t = 1;
} else if(max2 == _10) {
count2 += 1;
}
i += 1;
}
document.write("<br>");
document.write(maxNum2, " : ", count2, "개<br>");
</script>
HTML
복사


