재밌는 이벤트의 세계, JavaScript
<script>
// 절대값
let num = -123;
document.write("<h1>절대값 : " + Math.abs(num) + "</h1>");
</script>
<script>
// 원주율
document.write("<h1>원주율 : " + Math.PI + "</h1>");
</script>
<script>
// 난수 발생
document.write("<h1>난수 : " + Math.random()+ "</h1>");
</script>
<script>
// 소수점 반올림
let num2 = 3.373565;
document.write("<h1>반올림 : " + Math.round(num2) + "</h1>");
</script>
<script>
// 소수점 기준 올림
document.write("<h1>올림 : " + Math.ceil(num2) + "</h1>");
</script>
<script>
// 소수점 기준 내림
document.write("<h1>내림 : " + Math.floor(num2) + "</h1>");
</script>
Math.random()을 사용해서 5자리 인증번호 생성해라. 출력(00159, 45911, ..)
단, funtion 을 생성하여 출력한다.
<script>
// 두 수 사이에 난수를 리턴하는 함수
function random(n1, n2){
return Math.floor(Math.random() * (n2 - n1 + 1) + n1);
}
// 인증번호 5자리
let auth = '';
for(let i = 0; i < 5; i++){
auth += random(0, 9);
}
document.write("<h1>인증번호 : " + auth + "</h1>")
// 2. '가위', '바위', '보' -> 랜덤으로 출력. 단, function 생성
let act = ['가위', '바위', '보'];
function game(){
let random = Math.floor(Math.random() * 3);
document.write("<h1>" + act[random] + "</h1>");
}
game();
</script>
'가위', '바위', '보'를 랜덤으로 출력. 단, function 생성
<script>
let act = ['가위', '바위', '보'];
function game(){
let random = Math.floor(Math.random() * 3);
document.write("<h1>" + act[random] + "</h1>");
}
game();
</script>
출처
https://media.giphy.com/media/qqtvGYCjDNwac/giphy.gif
https://media.giphy.com/media/26tPplGWjN0xLybiU/giphy.gif