영상
개념
<script>
/*
[문제]
arr배열은 이미 1~3의 값이 저장되어 있다.
이제 추가로 랜덤(1~10)을 10회 반복하여 추가하려 한다.
단, 중복숫자가 있으면 저장하지 않는다.
[예시1]
1, 2, 3, 10, 8, 9, 4
[예시2]
1, 2, 3, 9, 5, 4, 8, 10, 6
[예시3]
1, 2, 3, 6, 4, 8
*/
let arr = [1, 2, 3];
let count = arr.length;
for(let i=0; i<10; i++) {
let num = Math.floor(Math.random() * 10) + 1;
let check = false;
for(let j=0; j<count; j++) {
if(num == arr[j]) {
check = true;
}
}
if(check == false) {
arr.push(num);
count += 1;
}
}
document.write(arr);
</script>
Java
복사