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

E0204_예제04_문제

예제

<script> /* [문제] 반복문을 사용해서 보기와 같이 출력하시오. [보기] 9 0 8 2 7 6 6 12 5 20 4 30 3 42 2 56 1 72 0 90 */ </script>
HTML
복사

정답_for문

<script> /* [문제] 반복문을 사용해서 보기와 같이 출력하시오. [보기] 9 0 8 2 7 6 6 12 5 20 4 30 3 42 2 56 1 72 0 90 */ let total = 0; let a = 0; for(let i = 9; i >= 0; i--){ total += a; a += 2; document.write(i, " ", total, "<br>"); } document.write("<br>"); let total2 = 0; for(let i = 1; i <= 10; i++) { document.write(10 - i, " ", total2, "<br>"); total2 += (i * 2); } </script>
HTML
복사

정답_while문

<script> /* [문제] 반복문을 사용해서 보기와 같이 출력하시오. [보기] 9 0 8 2 7 6 6 12 5 20 4 30 3 42 2 56 1 72 0 90 */ let total = 0; let a = 0; let i = 9; while(i >= 0) { total += a; a += 2; document.write(i, " ", total, "<br>"); i -= 1; } document.write("<br>"); let total2 = 0; let i2 = 1; while(i2 <= 10) { document.write(10 - i2, " ", total2, "<br>"); total2 += (i2 * 2); i2 += 1; } </script>
HTML
복사