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

D0303_개념01_경우의수와개수전

개념

<script> /* [경우의 수와 개수 전] - 조건문을 사용하여, 특정 조건이 충족될 때마다 - 누적 개수를 구할 수 있습니다. - 숫자 100부터 999 사이의 랜덤 숫자를 하나 저장합니다. - 해당 숫자의 각 자리수에 포함된 - 숫자 5의 개수를 세어 출력하시오. */ /* [출력예시] 756 1 */ let r = Math.floor(Math.random() * 900) + 100; document.write(r, "<br>"); let _100 = parseInt(r / 100); let _10 = parseInt(r % 100 / 10); let _1 = r % 10; if(_100 == 5 && _10 == 5 && _1 == 5) { document.write("3"); } if(_100 != 5 && _10 == 5 && _1 == 5) { document.write("2"); } if(_100 == 5 && _10 != 5 && _1 == 5) { document.write("2"); } if(_100 == 5 && _10 == 5 && _1 != 5) { document.write("2"); } if(_100 == 5 && _10 != 5 && _1 != 5) { document.write("1"); } if(_100 != 5 && _10 == 5 && _1 != 5) { document.write("1"); } if(_100 != 5 && _10 != 5 && _1 == 5) { document.write("1"); } if(_100 != 5 && _10 != 5 && _1 != 5) { document.write("0"); } </script>
HTML
복사

영상