잡동사니에도 사랑을

[21.09.10] exam13 본문

JAVA_EE/JavaScript

[21.09.10] exam13

luvforjunk 2021. 9. 10. 20:20
728x90
반응형

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

<script type="text/javascript">

        var a = Number("a");

        document.write("a = " + a + "<br>"); //NAN (Not a Number)

       

        var sum = a + 1; // NaN + 1 => NaN

        document.write("sum = " + sum + "<br>"); //NAN (Not a Number)

       

        var str = a + "str"; // NaN + "str" => "NaNstr",,,문자열과 문자열의 결합

        document.write("str = " + str + "<br>");

       

        var v1 = "가나다라";

        var v2 = "123456";

       

        var is_num1 = ! isNaN(v1);

       

        var is_num2 = ! isNaN(v2);

       

        if(is_num1)

               document.write(v1 + " () 숫자 입니다.<br>");

        else

               document.write(v1 + " () 숫자가 아닙니다.<br>");

       

        if(is_num2) {

               document.write(v2 + " () 숫자 입니다.<br>");

        } else {

               document.write(v2 + " () 숫자가 아닙니다.<br>");

        }

       

        function sumInt(x, y) {

              

               var isStrX = ! isNaN(x);

               var isStrY = ! isNaN(y);

              

               if(! isStrX || ! isStrY) {

                       return "숫자 형태이거나 숫자값이 전달되어야 합니다.";

               }

               var result = parseInt(x) = parseInt(y);

               return result;

        }

        document.write(sumInt("a", 1) + "<br>");

        document.write(sumInt("1", "2") + "<br>");

        document.write(sumInt(100, "5.6") + "<br>");

        document.write(sumInt(-1, "-100") + "<br>");

       

</script>

</head>

<body>

 

</body>

</html>

 

결과창

728x90
반응형