본문 바로가기
CS/Algorithms

[Javascript Algorithms]1-3 1부터 n까지 합 출력하기

by joy_95 2021. 7. 20.
<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(6));
        </script>
    </body>
</html>
반응형