본문 바로가기

Javascript

함수 function / isNaN, parseInt

더하기

See the Pen xxGYwQZ by MoonSeonhyeon (@Seonhyeon) on CodePen.

 

 

See the Pen XWbZmoW by MoonSeonhyeon (@Seonhyeon) on CodePen.

 

구구단

 

아래에 코드있어요.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        function gugudan(n) {
            m = parseInt(n);
            if(isNaN(m)||m<1||m>9){
                alert("잘못 입력하셨습니다.");
                return;//밑에 코드가 실행되지 않고 함수를 빠져나감 break랑 비슷.
            }
            for (i=1;i<10;i++){
                document.writeln(m + "*" + i + "=" + (m*i)+ "<br>");
            }
        }
    </script>
</head>
<body>
    <h3>구구단</h3><hr>
    <script>
        n = prompt("구구단 몇단을 원하세요?","5");
        gugudan(n);
    </script>
</body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs