JS_daily_algorithm_03

박하영·2022년 4월 20일
0

algorithm

목록 보기
6/9

문제:

내가 쓴 코드:

<html>
  <head>
    <meta charset="UTF-8" />
    <title>출력결과</title>
  </head>
  <body>
    <script>
      function solution(n) {
        let sum = 0;

        for (i = 1; i < n + 1; i++) {
          sum += i;
        }

        return sum;
      }

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

 

모범 답안:

<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>

 

Impressive Point & Learning Point

  1. 내가 쓴 코드와 모범 답안이 가장 차이 안 났던 문제중에 하나였다. for 문에 대해서 상기시킬 수 있었고, for문 속 i의 range 설정할 때에 나는 i < n+1을 통해서 범위를 지정했는데, 이와 같은 의미로 사용할 수 있는 코드가 i <= n도 있다는 것을 알게 되었다.

 

profile
RM_young

0개의 댓글