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

E0204_점검04_문제

점검

<script> /* [문제] 반복문을 사용해서 보기와 같이 출력하시오. [보기] 9 1 8 5 7 12 6 22 5 35 4 51 3 70 2 92 1 117 0 145 */ </script>
HTML
복사

정답_for문

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

정답_while문

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