개발자공부 (2021.11~현재)/JavaScript

자바스크립트로 만드는 로또번호 뽑기 프로그램

purplecloud 2022. 1. 11. 20:42

배열을 이용하여 로또번호 뽑기 프로그램을 만들어보자.

<!DOCTYPE html>
<html lang="en">
<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>2. 로또번호 뽑기</title>
</head>
<script>
    'use strict';
 
    let lotto=[1,2,3,4,5,6];
    for(let i in lotto){
    lotto[i]=Math.floor(Math.random()*45+1);}

    for(let i=0; i<lotto.length-1; i++){
        for(let j=i+1;j<lotto.length;j++){
            if(lotto[i]==lotto[j]){
                lotto[j]=Math.floor(Math.random()*45+1);
            }
        }
    }

    let temp;
    for(let i=0; i<5; i++){
        for(let j=i+1; j<6; j++){
            if(lotto[i]>lotto[j]){    
                temp=lotto[i];
                lotto[i]=lotto[j];
                lotto[j]=temp;
            }
        }
    }

    for (let i=0; i<6; i++){
        document.write(lotto[i]+" ")
    }
</script>
<body>
    <h2>로또번호 만들기</h2>
</body>
</html>