자바스크립트
home
2025 자바스크립트 초급 1500제
home

E0201_개념03_반복문과혼합연산의누적

개념

<script> /* [반복문과 혼합 연산의 누적] - 반복문을 활용한 누적과 and 논리 연산자를 함께 사용해 보겠습니다. - 먼저 1부터 15까지 15회 반복합니다. - 이때 값이 5보다 크고 10보다 작은 숫자들의 합을 구하려 합니다. */ /* [출력예시] 6 7 8 9 30 */ // [for문] let total = 0; for(let i = 1; i <= 15; i++) { let tf = 5 < i && 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 = 5 < i && i < 10; if(tf) { document.write(i, " "); total2 += i; } i += 1; } document.write("<br>"); document.write(total2); </script>
HTML
복사

영상