<style>
li{
margin: 10px;
}
</style>
<button>로또 번호 추출</button>
<ol class="js-lotto">
</ol>
<script>
const lotto = document.querySelector('.js-lotto')
const btn = document.querySelector('button')
const lotto_number = []
function handleLotto(){
let arr = [];
for(let i = 0; i < 6; i++){
arr.push(Math.floor(Math.random()*45)+1);
for(let c = 0; c<i; c++){
if (arr[i] === arr[c]) {
arr.pop();
i--;
}
}
}
console.log(arr);
for (let i = 0; i < arr.length; i++) {
let swap;
for(let j = 0; j<arr.length -1 -i; j++){
if (arr[j] > arr[j + 1]) {
swap = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = swap;
}
}
}
console.log(arr);
lotto_number.push(arr)
liPush(lotto_number);
}
function liPush(lotto_number){
const li = document.createElement("li");
const lottoLengh = Object.keys(lotto_number).length;
li.innerText = lotto_number[lottoLengh-1];
lotto.appendChild(li);
}
btn.addEventListener("click", handleLotto)
활용)
1. 버블 정렬(bubble sort)
2. addEventListener
3. document.createElement
4. Object.keys
5. appendChild
6. Math.random, Math.floor
7. querySelector
8. Array.prototype.push, Array.prototype.pop
'알고리즘 > JavaScript' 카테고리의 다른 글
로또 번호 생성하기 (Javascript) (0) | 2020.07.27 |
---|