[바닐라 JS] random(), floor(), createElement()

이지수·2021년 7월 7일
0
post-thumbnail

Math.random()

0부터 1사이의 랜덤한 숫자를 제공한다

Math.floor()

소수점을 내림해준다.

random함수를 사용하여 10을 곱한 후 floor를 사용하면 0부터 9까지의 수중에서 랜덤으로 구할 수 있다.

Math.floor(Math.random() * 10)

10 대신 필요한 값을 곱하여 특정 숫자사이의 수들중에서 랜덤으로 구할 수 있다.

Math.round()
반올림 해준다.

Math.ceil()
올림해준다.

createElement()

html에서 직접 요소를 추가하는 것이 아니라 자바스크립트에서 요소를 생성할 수 있다.

append
가장뒤에 추가

prepend
가장앞에 추가

const bgImage = document.createElement("img");

bgImage.src = `img/${chosenImage}`;

document.body.appendChild(bgImage);

reference

노마드코더

profile
The only thing worse than starting something and failing...is not starting something

0개의 댓글