개발

랜덤으로 조 할당 예제 본문

Frontend/JavaScript

랜덤으로 조 할당 예제

Study 2021. 3. 20. 15:37

인풋박스에서 받아온 이름들을 버튼을 누를 시 

어레이.value.split(",") 기능을 사용하려 배열로 나눠주고

어레이.sort(function(a, b){return 0.5 - Math.random()}) 기능으로 랜덤으로 정렬 해준 뒤

하단 인풋박스에 순서대로 넣어주는 방식입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!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>Document</title>
    <style>
        .wrapper{
            width: 300px;
            margin: auto;
            text-align: center;
        }
    </style>
    <script>
        var ppple;
        var div;
        var arr;
        var inputs=[];
        //기본값
        function init(){
            ppple=document.getElementById("ppple")
            ppple.value="양석호,유재석,김희철,강호동,이순재,신 구,조매력,정형돈,김국진,윤종신,김종국,블루디,비비,블랙홀";
            div=document.getElementById("ansRen")
        }
        //앤덤뽑기
        function hyohyohyo(){
            arr=ppple.value.split(",");
            arr.sort(function(a, b){return 0.5 - Math.random()});
            for(var a=0;a<inputs.length;a++){
                div.removeChild(inputs[a]);
            }
            inputs=[];
            for(var i=0;i<(arr.length/2);i++){
                var inPut= document.createElement("input");
                var arr1=arr[i*2];
                var arr2=arr[(i*2)+1];
                inPut.value=(i+1)+"조: "+arr1+", "+arr2;
                div.appendChild(inPut);
                inputs.push(inPut);
            }
        }
 
        //로드
        window.addEventListener("load"function(){
            init()
        });
    </script>
</head>
<body>
    <div class="wrapper">
        <div><input type="text" id="ppple"><br><br><br></div>
        <div id="ansRen"></div>
        <div>
            <button onclick="hyohyohyo()">서밋</button>
        </div>
    </div>
</body>
</html>
cs

'Frontend > JavaScript' 카테고리의 다른 글

단어게임 예제  (0) 2021.03.20
랜덤 조 할당 예제 2  (0) 2021.03.20
슬라이드쇼 예제  (0) 2021.03.20
카운트다운 예제  (0) 2021.03.20
스톱워치 예제  (0) 2021.03.20