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

E0202_점검02_문제

점검

<script> /* [문제] 2! 와 3! 을 더한 값을 구하시오. */ /* [출력예시] 2 2 3 6 8 */ </script>
HTML
복사

정답

<script> /* [문제] 2! 와 3! 을 더한 값을 구하시오. */ /* [출력예시] 2 2 3 6 8 */ // [for문] let a = 2; let total = 1; for(let i = 1; i <= a; i++) { total *= i; } document.write(a, " ", total, "<br>"); let b = 3; let total2 = 1; for(let i = 1; i <= b; i++) { total2 *= i; } document.write(b, " ", total2, "<br>"); let c = total2 + total; document.write(c); document.write("<br>"); // [while문] let total3 = 1; let i = 1; while(i <= a) { total3 *= i; i += 1; } document.write(a, " ", total3, "<br>"); let total4 = 1; let i2 = 1; while(i2 <= b) { total4 *= i2; i2 += 1; } document.write(b, " ", total4, "<br>"); let d = total4 + total3; document.write(d); </script>
HTML
복사