Web/JavaScript
[days05] js Math 내장 객체와 js boolean
다연
2020. 12. 15. 13:39
반응형
js Math
<script>
//[js Math 내장 객체]
document.write(Math.PI+"<br>");
document.write(Math.round(4.7)+"<br>"); //5
document.write(Math.round(4.45)+"<br>"); //4기본적으로 소수점 첫째자리에서 반올림함
document.write(Math.ceil(4.45)+"<br>"); //절상 5
document.write(Math.floor(4.9)+"<br>"); //절삭 4
document.write(Math.min(2,3,1,23,45)+"<br>"); //1
document.write(Math.max(2,3,1,23,45)+"<br>"); //45
document.write(Math.random()+"<br>"); //45
</script>
로또번호 발생기
버튼을 누를때마다 랜덤한 로또 번호가 발생한다.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<button>로또 번호 발생기</button>
<div id="demo"></div>
<script>
var lotto=[];
//document.getElementsByTagName("button")[0];
document.querySelector("button").onclick = function () {
var n;
lotto=[]; //초기화
while (lotto.length<6) {
n=getRandRange(1,45);
if(isDuplicationLotto(lotto,n)){//중복체크
lotto.push(n);
}
};
lotto.sort(function(a, b) { //숫자는 비교가 안되니
return a-b; //이렇게 정렬
});
document.getElementById("demo").innerHTML = lotto.toString();
};
function getRandRange(min,max) { //1 ~ 45
return Math.floor(Math.random()*(max-min+1))+min;
}
//중복되면f 안되면t
function isDuplicationLotto(lotto, n) {
return lotto.every(function(elt, i, array) { //모든 요소가 만족하는지 물어봄
return elt!=n; //모두가 이 조건을 만족하면 true 돌림
});
}
</script>
</body>
</html>
리스트에서 숫자를 선택하면 그 줄만큼의 로또 번호를 출력하게 하자
(리스트의 숫자 1~20)은 동적으로 생성하자
+JQuery로 수정하자
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<select id="cmbCnt">
</select>
<button>로또 번호 발생기</button>
<div id="demo"></div>
<script>
for (var i = 1; i <=20; i++) {
$("#cmbCnt").append("<option>"+i+"</option>");
}
</script>
<script>
var lottos=[];
$("button").click(function(event) {
//alert($("#cmbCnt").val());
//$("#demo").empty();
lottos=[];
var n;
for (var i = 0; i <$("#cmbCnt").val(); i++) {
lotto=[]; //초기화
while (lotto.length<6) {
n=getRandRange(1,45);
if(isDuplicationLotto(lotto,n)){//중복체크
lotto.push(n);
}
};
lotto.sort(function(a, b) { //숫자는 비교가 안되니
return a-b; //이렇게 정렬
});
lottos.push(lotto);
}//for
$("#demo").html(lottos.join("<br>").toString());
});//click
function getRandRange(min,max) { //1 ~ 45
return Math.floor(Math.random()*(max-min+1))+min;
}
//중복되면f 안되면t
function isDuplicationLotto(lotto, n) {
return lotto.every(function(elt, i, array) { //모든 요소가 만족하는지 물어봄
return elt!=n; //모두가 이 조건을 만족하면 true 돌림
});
}
</script>
</body>
</html>
join, evey 등등 기억해두기 - 로또 관련해 시험에 자주 나온다
js boolean
<script>
//js boolean 확인
//true, false
// > < == != 결과값 boolean
document.write((8>7)+"<br>");
document.write(Boolean("")+"<br>"); //빈문자열: false
document.write(Boolean(0)+"<br>"); //0 false
document.write(Boolean(-0)+"<br>"); //-0: false
document.write(Boolean(null)+"<br>"); //null: false
document.write(Boolean(NaN)+"<br>"); //NaN: false
var x
document.write(Boolean(x)+"<br>"); //undefined: false
</script>
반응형