본문 바로가기
알고리즘/JavaScript

로또 번호 생성하기2 (Javascript)

by 싸공 2020. 8. 10.
    <style>
        li{
            margin10px;
        }
    </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 = 0i < 6i++){
            arr.push(Math.floor(Math.random()*45)+1); 
            for(let c = 0c<ic++){
                if (arr[i] === arr[c]) {
                    arr.pop();
                    i--;  
                }
            }
        }
        console.log(arr);
        for (let i = 0i < arr.lengthi++) {
            let swap;
            for(let j = 0j<arr.length -1 -ij++){
                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