Math

기혁·2023년 3월 6일
0

Javascript 학습

목록 보기
11/15

day08

📌 Math (1)

<script>
        //Math
        // 두 수 중에서 최대값
        let max = Math.max(100,123);
        document.write("<h1>최대값 : " + max + "</h1>");

        // 두 수 중에서 최소값
        let min = Math.min(100,123);
        document.write("<h1>최소값 : " + min + "</h1>");

        // 원주율
        document.write("<h1원주율 : " + Math.PI + "</h1>")

        // 소수점 반올림
        let num1 = 3.7146;
        document.write("<h1>소수점 반올림 : "
            + Math.round(num1) + "</h1>");

        // 소수점 올림과 내림
        document.write("<h1>소수점 올림 : "
            + Math.ceil(num1) + "</h1>");
        document.write("<h1>소수점 내림 : "
            + Math.floor(num1) + "</h1>");

        // 절대값 반환
        let num2 = -123;
        document.write("<h1>절대값 : "
            + Math.abs(num2) + "</h1>");
        
        // 난수 발생
        document.write("<h1>난수 : "
            + Math.random() + "</h1>");
    </script>

💡 결과값

📌 Math (2)

	<script>
        // 두 수 사이의 난수를 리턴하는 함수
        function random(n1, n2){
            return parseInt(Math.random()*(n2 - n1 + 1)) + n1;
        }

        // 함수를 사용해서 0~9사이의 난수 출력
        let num = random(0, 9);
        document.write("<h1>0~9사이의 난수 : " 
            + num + "</h1>");            

        // 함수를 사용해서 5자리 인증번호를 출력
        let auth = "";
        for( let i = 0; i<5; i++ ){
            auth += random(0,9);
        }

        document.write("<h1>인증번호 : " 
            + auth + "</h1>");            
    </script>

💡 결과값
인증번호 5자리가 랜덤으로 출력된다.

profile
⭐️내가만든쿠키⭐️

0개의 댓글