1부터 N까지의 합

아기코딩단2·2022년 7월 21일
0
<!--my solution-->
<html>
    <head>
        <meta charset="UTF-8">
        <title>출력결과 - 섹션1-4 - 1부터N까지의 합</title>
    </head>
    <body>
        <script>
          //자연수 N이 주어지면 1부터 N까지의 합을 구하여라
            function solution(N) {
                let answer;
                answer = N * ( N + 1 )/2;
                return answer;
            }
            console.log(solution(10));
        </script>
    </body>
</html>

<!--teacher's solution-->
<html>
    <head>
        <meta charset="UTF-8">
        <title>출력결과</title>
    </head>
    <body>
        <script>
            function solution(n){
                let answer=0;
                for(let i=1; i<=n; i++){
                    answer=answer+i;
                }
                
                return answer;
            }

            console.log(solution(10));
        </script>
    </body>
</html>

for문 개념을 묻는 문제였는데 나는 그냥 공식을 써버렸다;;

;; 당황쓰 ^^;;

profile
레거시 학살자

0개의 댓글