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

E0204_예제05_문제

예제

<script> /* [문제] 반복문을 사용해서 보기와 같이 출력하시오. [보기] 9 9 7 16 5 21 3 24 1 25 */ </script>
HTML
복사

정답_for문

<script> /* [문제] 반복문을 사용해서 보기와 같이 출력하시오. [보기] 9 9 7 16 5 21 3 24 1 25 */ let total = 0; for(let i = 9; i >= 0; i -= 2){ total += i; document.write(i, " ", total, "<br>"); } document.write("<br>"); let total2 = 0; for(let i = 0; i< 5; i++) { let a = 9 - 2 * i; total2 += a; document.write(a, " ", total2, "<br>"); } </script>
HTML
복사

정답_while문

<script> /* [문제] 반복문을 사용해서 보기와 같이 출력하시오. [보기] 9 9 7 16 5 21 3 24 1 25 */ let total = 0; let i = 9; while(i >= 0) { total += i; document.write(i, " ", total, "<br>"); i -= 2; } document.write("<br>"); let total2 = 0; let i2 = 0; while(i2 < 5) { let a = 9 - 2 * i2; total2 += a; document.write(a, " ", total2, "<br>"); i2 += 1; } </script>
HTML
복사