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

E0306_개념03_개수의초기화와개수증가3

개념

<script> /* [개수의 초기화와 개수 증가3] - 숫자 100부터 500까지 다음 조건에 따라 숫자를 출력하시오. [조건] (1) 각 자리수에 1이 두 개 포함된 수를 세 개 출력합니다. (2) 이어서 각 자리수에 2가 두 개 포함된 수를 세 개 출력합니다. (3) 이어서 각 자리수에 3이 두 개 포함된 수를 세 개 출력합니다. (4) 같은 방식으로 숫자를 하나씩 증가시키며 반복합니다. */ /* [출력예시] 101 110 112 122 202 212 233 303 313 344 404 414 455 */ // [for문] let a = 1; let count = 0; let countMax = 0; for(let i = 100; i <= 500; i++) { let _100 = parseInt(i / 100); let _10 = parseInt(i % 100 / 10); let _1 = i % 10; if(_100 == a) { count += 1; } if(_10 == a) { count += 1; } if(_1 == a) { count += 1; } if(count == 2) { document.write(i, " "); countMax += 1; if(countMax == 3) { countMax = 0; a += 1; } } count = 0; } document.write("<br>"); // [while문] let a2 = 1; let count2 = 0; let countMax2 = 0; let i = 100; while(i <= 500) { let _100 = parseInt(i / 100); let _10 = parseInt(i % 100 / 10); let _1 = i % 10; if(_100 == a2) { count2 += 1; } if(_10 == a2) { count2 += 1; } if(_1 == a2) { count2 += 1; } if(count2 == 2) { document.write(i, " "); countMax2 += 1; if(countMax2 == 3) { countMax2 = 0; a2 += 1; } } count2 = 0; i += 1; } </script>
HTML
복사

영상