Math
의 메소드 기능을 사용하여 배경화면 랜덤으로 나타내기
중요 코드
Math.floor()
Math.random()
const images = [
"1.jpeg","2.jpeg","3.jpeg"
];
//random으로 이미지 나타내기
const chosenImage = images[Math.floor(Math.random() * images.length)];
// 배경 이미지 넣는 형식 url('img주소')
const bgImage = `url(img/${chosenImage})`
// body에 추가
document.body.style.backgroundImage = bgImage
Math.floor
= 주어진 숫자와 같거나 작은 정수 중에서 가장 큰 수를 반환한다.
ex )
console.log(Math.floor(5.95));
output: 5
console.log(Math.floor(5.05));
output: 5
console.log(Math.floor(-5.05));
output: -6
출처 : MDN
Math.random은 0이상 1이하의 난수를 생성한다.
코드에서는 배경 이미지 수량에 맞게 array를 변수로 만들어 곱해줌