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

E0303_점검01_문제

점검

<script> /* [문제] 200부터 250까지의 숫자중에서 6의 배수를 각각의 자리로 숫자를 나눈 후, 2 또는 4의 개수가 두 개인 숫자만 출력하시오. */ /* [출력예시] 204 2 228 2 234 2 240 2 246 2 */ </script>
HTML
복사

정답

<script> /* [문제] 200부터 250까지의 숫자중에서 6의 배수를 각각의 자리로 숫자를 나눈 후, 2 또는 4의 개수가 두 개인 숫자만 출력하시오. */ /* [출력예시] 204 2 228 2 234 2 240 2 246 2 */ // [for문] let count = 0; for(let i = 200; i <= 250; i++) { if(i % 6 == 0) { let _100 = parseInt(i / 100); let _10 = parseInt(i % 100 / 10); let _1 = i % 10; if(_100 == 2 || _100 == 4) { count += 1; } if(_10 == 2 || _10 == 4) { count += 1; } if(_1 == 2 || _1 == 4) { count += 1; } if(count == 2) { document.write(i, " ", count, "<br>"); } count = 0; } } document.write("<br>"); // [while문] let count2 = 0; let i = 200; while(i <= 250) { if(i % 6 == 0){ let _100 = parseInt(i / 100); let _10 = parseInt(i % 100 / 10); let _1 = i % 10; if(_100 == 2 || _100 == 4) { count2 += 1; } if(_10 == 2 || _10 == 4) { count2 += 1; } if(_1 == 2 || _1 == 4) { count2 += 1; } if(count2 == 2) { document.write(i, " ", count2, "<br>"); } count2 = 0; } i += 1; } </script>
HTML
복사