JS_daily_algorithm_05

박하영·2022년 4월 23일
0

algorithm

목록 보기
9/9

문제:

내가 쓴 코드:

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

        for (let x of arr) {
          if (x % 10 === day) answer += 1;
        }

        return answer;
      }

      arr = [25, 23, 11, 47, 52, 17, 33];

      console.log(solution(3, arr));
    </script>
  </body>
</html>

 

모범 답안:

<html>
    <head>
        <meta charset="UTF-8">
        <title>출력결과</title>
    </head>
    <body>
        <script>
            function solution(day, arr){
                let answer=0;
                for(let x of arr){
                    if(x%10==day) answer++;
                }
                
                return answer;
            }
            
            arr=[25, 23, 11, 47, 53, 17, 33];
            console.log(solution(3, arr));
        </script>
    </body>
</html>

 

Impressive Point & Learning Point

  1. for문 선언시 "let x of arr" 구문에서 of 부분을 깜빡해서 in 으로 작성했다가 코드 부분에서 분명 오류날 것이 없었는데, 결과값이 다르게 나오는 것을 보고 다시 상기시킬 수 있었다. 외워두장!
  1. 답안 코드와 작성한 코드가 거의 똑같았다. 달랐던 부분은 카운트를 +1씩 해주는 부분에 있어서 나는 += 1을 사용했고, 답안에서는 ++을 사용했다. ++ 연산자의 활용성에 대해서 한번 생각해볼 수 있었다.

 

profile
RM_young

0개의 댓글