개념
<script>
/*
[반복문과 혼합 연산의 누적2]
- 반복문을 활용한 누적과 or 논리 연산자를 함께 사용해보겠습니다.
- 먼저, 1부터 15까지 15회 반복합니다.
- 이때, 값이 5보다 작거나 10보다 큰 숫자들의 합을 구하려 합니다.
*/
/*
[출력예시]
1 2 3 4 11 12 13 14 15
75
*/
// [for문]
let total = 0;
for(let i = 1; i <= 15; i++) {
let tf = i < 5 || i > 10;
if(tf) {
document.write(i, " ");
total += i;
}
}
document.write("<br>");
document.write(total);
document.write("<br>");
// [while문]
let total2 = 0;
let i = 1;
while(i <= 15) {
let tf = i < 5 || i > 10;
if(tf) {
document.write(i, " ");
total2 += i;
}
i += 1;
}
document.write("<br>");
document.write(total2);
</script>
HTML
복사


