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

E0202_개념01_팩토리얼

개념

<script> /* [팩토리얼] - n! (엔 팩토리얼)은 1부터 n까지의 모든 자연수를 곱한 값입니다. - 예) 5! = 1 × 2 × 3 × 4 × 5 = 120 - 이러한 팩토리얼을 반복문으로 구현해보겠습니다. */ /* [출력예시] 5 120 */ let a = 5; document.write(a, "<br>"); // [for문] let total = 1; for(let i = 1; i <= a; i++) { total *= i; } document.write(total); document.write("<br>"); // [while문] let total2 = 1; let i = 1; while(i <= a) { total2 *= i; i += 1; } document.write(total2); </script>
HTML
복사

영상