변수를 함수 안에서 선언을 하면 함수가 끝나면 사라짐

그래서 함수 밖에서

 

ex)

let count = 1; 이 함수 안에 있으면

축적이 안되기 때문에  '홀수입니다'만 뜸

1
2
3
4
5
6
7
8
9
10
11
12
    <script>
        
        function hey() {
            let count = 1
            if (count % 2 == 0) {
                alert('짝짝짝👏');
            } else {
                alert('홀홀홀🎅');
            }
                        count += 1;
        }
    </script>
cs

그래서 함수 밖에 있어야 함 그러면 '홀수입니다','짝수입니다'가 잘 나옴

1
2
3
4
5
6
7
8
9
10
11
    <script>
        let count = 1;
        function hey() {
            if (count % 2 == 0) {
                alert('짝짝짝👏');
            } else {
                alert('홀홀홀🎅');
            }
                        count += 1;
        }
    </script>
cs

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

+ Recent posts