자바스크립트
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>세미콜론(;)과 주석</title>
</head>
<body>
<h1>세미콜론(;)과 주석</h1>
<script>
document.write('안녕'); document.write('하이')
</script>
</body>
</html>
HTML
복사
이렇게 해도 오류 안뜸 -> 원래 자바스크립트는 끝에 ;를 안써도 인식을 하지만 두 개를 쓰고 ;를 모두 안쓰면 화면에 뜨진않음. 그럴 경우 각각 문단을 바꾸거나 첫부분에 ;를 써야한다.
•
변수
var 변수명 = 값;
var name = ‘엄준식’;
let 변수명 = 값;
const 변수명 = 값;
•
문자열(String)
“엄준식”
‘엄준식‘
•
숫자(int, float)
정수형, 실수형
•
불(bool)
true, false
•
Math.random();
=> 0~1미만 실수(float)
•
parseInt();
=> 소수점은 버리고 정수로 변환
•
.push()
마지막 배열에 추가가된다.
•
DRY
Don’t Repeat Yourself
반복문 이용
•
for(시작; 끝; 증가) {
반복하려는 코드
}
•
만약 중복이 아니라면 .push()
.indexOf(값)
값이 있으면 위치, 없으면 –1
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>로또 번호 추첨기</title>
</head>
<body>
<h1>로또 번호 추첨기</h1>
<script>
var lotto = [];
for (var i = 0; i < 6; i++){
var num = parseInt(Math.random() * 45 + 1);
if (lotto.indexOf(num) == -1) {
lotto.push(num);
}
}
document.write(lotto);
</script>
</body>
</html>
HTML
복사
for문만 할 경우 6번 반복
•
while(조건) {
반복하려는 코드
}
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>로또 번호 추첨기</title>
</head>
<body>
<h1>로또 번호 추첨기</h1>
<script>
var lotto = [];
while (lotto.length < 6) {
var num = parseInt(Math.random() * 45 + 1);
if (lotto.indexOf(num) == -1) {
lotto.push(num);
}
}
document.write(lotto);
</script>
</body>
</html>
HTML
복사
•
.sort()
=> 배열의 값 정렬
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>로또 번호 추첨기</title>
</head>
<body>
<h1>로또 번호 추첨기</h1>
<script>
var lotto = [];
while (lotto.length < 6) {
var num = parseInt(Math.random() * 45 + 1);
if (lotto.indexOf(num) == -1) {
lotto.push(num);
}
}
lotto.sort((a,b)=>a-b);
document.write(lotto);
</script>
</body>
</html>
HTML
복사
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>로또 번호 추첨기</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>로또 번호 추첨기</h1>
<script>
var lotto = [];
while (lotto.length < 6) {
var num = parseInt(Math.random() * 45 + 1);
if (lotto.indexOf(num) == -1) {
lotto.push(num);
}
}
lotto.sort((a,b)=>a-b);
document.write("<div class='ball ball1'>" + lotto[0] + "</div>");
document.write("<div class='ball ball2'>" + lotto[1] + "</div>");
document.write("<div class='ball ball3'>" + lotto[2] + "</div>");
document.write("<div class='ball ball4'>" + lotto[3] + "</div>");
document.write("<div class='ball ball5'>" + lotto[4] + "</div>");
document.write("<div class='ball ball6'>" + lotto[5] + "</div>");
</script>
</body>
</html>
HTML
복사
.ball {
float: left;
width: 60px;
height: 60px;
line-height: 60px;
font-size: 28px;
border-radius: 100%;
text-align: center;
vertical-align: middle;
color: #fff;
font-weight: 500;
text-shadow: 0px 0px 3px rgba(73, 57, 0, .8);
margin-right: 6px;
}
.ball1 {
background: #fbc400;
}
.ball2 {
background: #69c8f2;
}
.ball3 {
background: #ff7272;
}
.ball4 {
background: #aaa;
}
.ball5 {
background: #b0d840;
}
.ball6 {
background: #c7c7c7;
}
CSS
복사