개념
<script>
/*
[연속 누적 for문]
- 연속 누적은 반복문 안에서 계산된 값을
매번 더해가며 합계를 구하는 방식입니다.
- 반복문을 사용하여 보기와 같이 출력하시오.
[보기]
0 0
1 1
2 3
3 6
4 10
5 15
6 21
7 28
8 36
9 45
*/
// [1번 방법] 변수 활용
let total = 0;
let a = 0;
for(let i = 0; i < 10; i++) {
total += a;
a += 1;
document.write(i, " ", total, "<br>");
/*
i = 0 a = 0 total = 0 + 0
i = 1 a = 1 total = 0 + 1
i = 2 a = 2 total = 1 + 2
*/
}
document.write("<br>");
// [2번 방법] 반복 변수 i를 활용
let total2 = 0;
for(let i = 0; i < 10; i++) {
total2 += i;
document.write(i, " ", total2, "<br>");
}
</script>
HTML
복사


