javascript
javascript를 html으로 출력하기
멈머이
2023. 10. 3. 20:58
728x90
어제 생성했던 로또 번호를 브라우저 화면에 출력하는 방법을 알아보겠다.
<h3 id="ran"></h3>
<script>
let selected = [];
while (selected.length < 6) {
const num = parseInt(Math.random() * 45) + 1
if (selected.indexOf(num) == -1) {
selected.push(num);
selected.sort((a, b) => a - b)
}
}
console.log(selected)
</script>
어제 스크립트를 만들고 그 결과를 콘솔창에서 확인 했다.
https://coding-heading.tistory.com/6
랜덤으로 로또 번호 생성하기
우선 코드부터 자바스크립트의 Math.random 함수를 사용하여 중복되지 않은 랜덤한 숫자 6개를 뽑는 경우를 만들었다. console.log로 찍어 브라우저에서 개발자 도구로 확인 가능하다. 다음시간에는
coding-heading.tistory.com
오늘은 코드 한줄을 추가해서 브라우저화면에 출력해 보도록 하겠다.
<script>
let selected = [];
while (selected.length < 6) {
const num = parseInt(Math.random() * 45) + 1
if (selected.indexOf(num) == -1) {
selected.push(num);
selected.sort((a, b) => a - b)
}
}
console.log(selected)
document.getElementById("ran").innerHTML = selected
</script>
document.getElementById("ran").innerHTML = selected
"ran"이라는 아이디를 가진 대상에 selected 라는 함수의 값을 넣어 브라우저 화면에 출력한다 생각하면 쉽다.
브라우저화면에 출력된 결과와 콘솔창의 결과.
728x90