JavaScript

JS강의 로또만들기 22.08.10

연93 2022. 8. 10. 17:32
const getLottoNumber = function()
{
    const ranNumber = [];

    // ranNumber.length가 6이 될때까지 반복
    let num = 0;
    while(ranNumber.length < 6)
    {               
        // 1 ~ 45 사이의 랜덤값을 만든다.
        num = Math.floor(Math.random() * 45) + 1;

        if (-1 === ranNumber.indexOf(num))
        {
            ranNumber.push(num);
        }
        
    }

    return ranNumber;
}
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <!-- 웹 폰트 데이터를 불러온다. -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@400;700&display=swap" rel="stylesheet">

    <!-- CSS를 불러온다. 이 안에서 폰트를 설정한다.-->
    <link rel="stylesheet" href="style.css">

    <!-- Lodash 라이브러리와 lotto.js를 불러온다. -->
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
    <script src="./lotto.js"></script>
    
</head>
<body>
    <h1>로또번호 5라인</h1>
    <script>
    (function(){              

        let ranLine;
        let str;
        for (let i = 0; i < 5; i++)
        {
            ranLine = getLottoNumber();
            ranLine = _.sortBy(ranLine);

            str = ranLine.join();
            document.body.innerHTML += `<h1>${str}</h1>`
        }
        
    })();
    </script>
    
</body>
</html>

'JavaScript' 카테고리의 다른 글

콜백 함수 정리  (0) 2022.08.14
스코프 강의 내용 22.08.11  (0) 2022.08.11
객체연습  (0) 2022.08.09
forEach.map 실습 22.08.09  (0) 2022.08.09
콜백 사용 교육 22.08.08  (0) 2022.08.08