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>