-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_work_로또번호만들기.html
133 lines (111 loc) · 3.78 KB
/
05_work_로또번호만들기.html
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>template</title>
<style>
.ball {
width: 100px;
height: 100px;
border: 1px solid black;
display: inline-block;
border-radius: 50%;
/* 글씨크기 조정*/
text-align: center;
font-size: 3em;
line-height: 100px;
box-shadow: 10px 10px 15px inset #3a3a3a;
}
.ball0 {
background-color: red;
}
.ball1 {
background-color: pink;
}
.ball2 {
background-color: mediumaquamarine;
}
.ball3 {
background-color: skyblue;
}
.ball4 {
background-color: lemonchiffon;
}
.ball5 {
background-color: orange;
}
</style>
<script>
/*
(실습) 로또번호 만들고, 화면 출력
1. 로또번호 저장할 배열을 만들고
2. 1-45 숫자 중 중복되지 않는 숫자 생성해서 배열에 저장.
(첫번째, 두번째 값 비교.)
2-1. 1-45 숫자 중 랜덤하게 숫자를 만들고 (Math.random())
2-2. 중복된 값이 있으면 다시 생성해서 6개의 번호 배열 저장.
3. 화면출력 HTML div 태그에 ball, ball+숫자 클래스명 사용.
*/
// 내 풀이
// document.write("<h3>JavaScript 사용 로또번호 만들기</h3>")
//
// let lotto = [];
//
// for(let i = 0; i < 999; i++){
// let randomNum = Math.floor(Math.random() * 45) + 1;
// if(lotto.indexOf(randomNum) == -1){
// lotto.push(randomNum);
// if(lotto.length == 6){
// break;
// }
// }
// }
//선생님 풀이
var lotto = new Array();
console.log("lotto.length : " + lotto.length);
for (let i = 0; i < 10; i++) {
document.write()
}
while (lotto.length < 6) {
let lottoNum = Math.floor(Math.random() * 45) + 1;
if(isExist(lotto,lottoNum)) continue;
lotto[lotto.length] = lottoNum;
// if(!lotto.includes(lottoNum)){ // include도 사용 가능.
// lotto[lotto.length] = lottoNum;
// }
}
//배열 데이터 정렬
lotto.sort(); // 기본적으로 문자열 기준 정렬 처리
lotto.sort(function(a,b){
return a-b; //오름차순 정렬
// return b-a //내림차순 정렬
});
//함수만들기.//배열에 값이 있는지 확인!
function isExist(arr,num){
let result = false;
for(let i = 0; i<arr.length; i++){
if(arr[i] == num){ //같은 값이 존재하는 경우
result = true;
break;
}
}
return result;
}
//(html) 연결 !
for (let i = 0; i < lotto.length; i++) {
document.write('<div class="ball ball' + i + '">' + lotto[i] + '</div>');
}
</script>
</head>
<body>
<h2>로또번호 만들기(문제)</h2>
<p>1-45 까지의 숫자 중 6개를 추출해서 로또 만들기</p>
<hr>
<div class="ball ball0">5</div>
<div class="ball ball1">3</div>
<div class="ball ball2">27</div>
<div class="ball ball3">7</div>
<div class="ball ball4">43</div>
<div class="ball ball5">28</div>
<br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br>
</body></html>