개념
<script>
/*
[반복문 사용 전 누적]
- 숫자 1 + 2 + 3 + 4 + 5를 계산할 때는
왼쪽부터 순차적으로 더해갑니다.
- 먼저 1 + 2를 계산하면 3이 되고, 여기에 3을 더하면 6이 됩니다.
- 이 과정을 마지막 숫자까지 반복하면 최종 결과는 15가 됩니다.
- 이번에는 1부터 5까지의 숫자를 차례로 더하는 과정을
코드로 표현해 보겠습니다.
*/
/*
[출력예시]
total = 0 + 1
total = 1 + 2
total = 3 + 3
total = 6 + 4
total = 10 + 5
total = 15
*/
let total = 0;
document.write("total = ", total, " + ", 1, "<br>");
total = total + 1;
document.write("total = ", total, " + ", 2, "<br>");
total = total + 2;
document.write("total = ", total, " + ", 3, "<br>");
total = total + 3;
document.write("total = ", total, " + ", 4, "<br>");
total = total + 4;
document.write("total = ", total, " + ", 5, "<br>");
total = total + 5;
document.write("total = ", total);
</script>
HTML
복사


