브라우저에서 로또 번호 출력하기
console.log("-----------lotto-------------")
console.log("-----------lotto-------------")
console.log("-----------lotto-------------")
const btn = document.getElementById("btn")
console.log(btn)
btn.addEventListener("click", (event) => {
console.log(event)
const target = document.getElementById("resultDiv")
const balls = makeLottoNum()
target.innerHTML = ''
for (const num of balls) {
const tag = document.createElement("h3")
const textNode = document.createTextNode(num)
tag.appendChild(textNode)
target.appendChild(tag)
}
}, false)
function makeLottoNum() {
const numArr = []
while (true) {
num = parseInt((Math.random() * 45)) + 1
const index = numArr.indexOf(num)
if (index >= 0) {
continue
}
const newLength = numArr.push(num)
if (newLength === 6) {
break
}
}
return numArr
}
console.log("-----------lotto-------------")
console.log("-----------lotto-------------")
console.log("-----------lotto-------------")
const btn = document.getElementById("btn")
console.log(btn)
btn.addEventListener("click", (event) => {
console.log(event)
const target = document.getElementById("resultDiv")
const balls = getLotto()
console.log('balls' + balls)
target.innerHTML = ''
for (const num of balls) {
const tag = document.createElement("h3")
const textNode = document.createTextNode(num)
tag.appendChild(textNode)
target.appendChild(tag)
}
}, false)
function nums() {
const lottoArr = []
for (let i = 1; i <= 45; i++) {
lottoArr.push(i)
}
return lottoArr
}
function randomNum(lottoArr) {
const lottoResult = []
for (let i = 0; i < 6; i++) {
const index = parseInt(Math.random() * lottoArr.length)
lottoResult.push(lottoArr[index])
lottoArr.splice(index, 1)
}
return lottoResult
}
function getLotto() {
const result = randomNum(nums())
return result
}