FrontEND/JavaScript

1~45숫자중에서 로또번호 만들기

smartlittlepuppy 2024. 1. 24. 17:58
반응형

 

// 0~ 5 까지 렌덤번호
 let randomNum = Math.random() * 6;
// Int형태로 보여준다.
 let randomNum = Math.floor(Math.random() * 6);
 console.log(randomNum);

 

int형태로 보여주기위해서 Math.floor()사용했다. 

Math.random() * 6 이렇게 하게되면 0~ 5까지 숫자를 반복해준다. 그래서 + 1을하면 1~6까지의 숫자가 반복된다. 

 

1~45까지의 숫자중에서 6자리를 램덤으로 출력해보자. 

<h2>lotto number</h2>
   <button id="button">Random Number</button>
   <table border="1">
    <tr>
        <td id="label1"></td>
        <td id="label2"></td>
        <td id="label3"></td>
        <td id="label4"></td>
        <td id="label5"></td>
        <td id="label6"></td>
    </tr>
   </table>

    <script src="index.js"></script>

 

let button = document.getElementById("button");
let label1 = document.getElementById("label1");
let label2 = document.getElementById("label2");
let label3 = document.getElementById("label3");
let label4 = document.getElementById("label4");
let label5 = document.getElementById("label5");
let label6 = document.getElementById("label6");

let max = 45
let min = 1



button.onclick = function(){
 
    let randomNum1 = Math.floor(Math.random() * max)+min;
    let randomNum2 = Math.floor(Math.random() * max)+min;
    let randomNum3 = Math.floor(Math.random() * max)+min;
    let randomNum4 = Math.floor(Math.random() * max)+min;
    let randomNum5 = Math.floor(Math.random() * max)+min;
    let randomNum6 = Math.floor(Math.random() * max)+min;
    label1.textContent = randomNum1;
    label2.textContent = randomNum2;
    label3.textContent = randomNum3;
    label4.textContent = randomNum4;
    label5.textContent = randomNum5;
    label6.textContent = randomNum6;

}

 

반응형