<h2>랜덤숫자 생성</h2>
<h5>Math.random() : 0이상 1미만 소수를 랜덤으로 자동 반환</h5>
<h5 class="point">💙아래 예제의 경우 0이상 100미만 정수를 반환💙<br>
: Math.floor(Math.random() * 100) 형태</h5>
<input class="box" type="text" placeholder="random값 입력되는 박스">
<button class="but">랜덤버튼</button>
xxxxxxxxxx
.box{
width:200px;
height:50px;
border:2px solid black;
box-sizing:border-box;
text-align:center;
}
.point{
color: blue;
}
const box=document.querySelector('.box');
const but=document.querySelector('.but');
function randomClick(e){
e.preventDefault();//기본button 이벤트 중지
const random_num=Math.floor(Math.random() * 100)
box.value=random_num
}
but.addEventListener('click',randomClick)
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.