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

E0706_점검04_문제

점검

<script> /* [문제] 100의 약수를 모두 출력하고, 첫 번째 약수와 두 번째 약수의 합을 출력하시오. */ /* [출력예시] 1 2 4 5 10 20 25 50 100 3 */ </script>
HTML
복사

정답

<script> /* [문제] 100의 약수를 모두 출력하고, 첫 번째 약수와 두 번째 약수의 합을 출력하시오. */ /* [출력예시] 1 2 4 5 10 20 25 50 100 3 */ let a = 100; let total= 0; let count = 0; for(let i = 1; i <= 100; i++) { if(a % i == 0){ document.write(i, " "); count += 1; if(count == 1) { total += i; } if(count == 2) { total += i; } } } document.write("<br>"); document.write(total); </script>
HTML
복사