JS(Vanilla)

배울 내용


숫자

전체코드1


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        a1 = parseFloat(prompt("1")) ;
        a2 = parseFloat(prompt("2"));
        
        document.write(+(a1+a2).toFixed(12)+"<br>");
        document.write(+(a1/a2).toFixed(12)+"<br>");
        if (isNaN(a1)==false){
            if(Number.isInteger(a1)==true){
                document.write("정수"+"<br>");
            }
            else{
                document.write("실수"+"<br>");
            }
        }
        else if (isNaN(a1)==true){
            document.write("NAN")
        }
        if (isNaN(a2)==false){
            if(Number.isInteger(a2)==true){
                document.write("정수"+"<br>")
            }
            else{
                document.write("실수"+"<br>")
            }
        }
        else if (isNaN(a2)==true){
            document.write("NAN"+"<br>")
        }
    </script>
</body>
</html>

JS 부분1


a1 = parseFloat(prompt("1")) ;
        a2 = parseFloat(prompt("2"));
        
        document.write(+(a1+a2).toFixed(12)+"<br>");
        document.write(+(a1/a2).toFixed(12)+"<br>");
        if (isNaN(a1)==false){
            if(Number.isInteger(a1)==true){
                document.write("정수"+"<br>");
            }
            else{
                document.write("실수"+"<br>");
            }
        }
        else if (isNaN(a1)==true){
            document.write("NAN")
        }
        if (isNaN(a2)==false){
            if(Number.isInteger(a2)==true){
                document.write("정수"+"<br>")
            }
            else{
                document.write("실수"+"<br>")
            }
        }
        else if (isNaN(a2)==true){
            document.write("NAN"+"<br>")
        }

Parse(형변환)


형변환

이외 기타 기초 문법들은 JavaScript 기초 를 참고한다.

+ unary plus


document.write(+(a1+a2).toFixed(12)+"<br>");

+를 값 앞에 붙이면 해당 값을 숫자로 변환하도록 한다.

toFixed()