렛츠기릿 자바스크립트 강의에서 배운 2 가지의 random으로 숫자 뽑기 방식을 정리하였습니다.
const arr = Array(45).fill().map((val, i) => { return i + 1 });
const answer = [];
for(int i = 0; i < 7; i++) {
const index = Math.floor(Math.random() * arr.length);
answer.push(arr.splice(index, 1)[0]); // splice는 배열을 return함을 유의
}
1~45 중 random으로 숫자를 1개씩 7번 뽑기
※splice
는 배열을 반환함에 유의하자!
const arr = Array(45).fill().map((val, i) => { return i + 1 });
const shuffle = [];
while(arr.length > 0) {
const index = Math.floor(Math.random() * arr.length);
const value = arr.splice(index, 1)[0]; // splice는 배열을 return함을 유의
shuffle.push(value);
}
1~45 숫자를 random으로 섞은 후 앞에서 7개를 빼내기