[모던 JavaScript] 2.17 ~ 3.2 과제 정리

    SMALL
    • 2.17 화살표 함수 기본
    • 2.18 기본 문법 요약
    • 3.1 Chrome 디버깅하기
    • 3.2 코딩스타일

    2.17 화살표 함수로 변경하기

    <!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>화살표 함수로 변경하기/title>
    </head>
    <body>
        <script>
            function ask(question, yes, no) {
                if (confirm(question)) yes()
                else no();
            }
    
            ask("동의하십니까?",
                () => alert("동의하셨습니다."),
                () => alert("취소 버튼을 누르셨습니다.")
            );
        </script>
    </body>
    </html>

    3.2 좋지 않은 코드 스타일

    <!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>좋지 않은 코드 스타일</title>
    </head>
    <body>
        <script>
            // 더 나은 코드로 바꾼 것!
            function pow(x, n) {
                let result = 1;
    
                for (let i=0; i<n; i++) {
                    result *= x;
                }
                
                return result;
            }
    
            let x = prompt("x?", '');
            let n = prompt("n?", '');
            
            if (n <= 0) {
                lert(`Power ${n} is not supported, 
                please enter an integer number greater than zero`);
            } else {
                alert( pow(x, n) )
            }
        </script>
    </body>
    </html>

     

    728x90

    댓글