[JS] ํจ์ ์ฐ์ต๋ฌธ์ 1, 2
2021. 10. 27. 15:21ใHTML + CSS + JS/JS ์๋ฐ์คํฌ๋ฆฝํธ
728x90
1. ์ฐ์ต๋ฌธ์
'๋ฐฐ๊ฒฝ ์์ ๋ฐ๊พธ๊ธฐ' ๋ฒํผ์ ๋๋ฅด๋ฉด ๋ฐฐ๊ฒฝ ์์์ด ๋ฐ๋๋ ํจ์ ๋ง๋ค๊ธฐ
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ํจ์ </title>
<script>
function chColor() {
var arrColor = ["#ff0", "#6c0", "#fcf", "#cf0", "#39f"];
var arrNum = Math.floor((Math.random() * arrColor.length));
var bodyTag = document.getElementById("theBody");
bodyTag.style.backgroundColor = arrColor[arrNum];
}
</script>
</head>
<body id="theBody">
<button onclick="chColor();">๋ฐฐ๊ฒฝ ์์ ๋ฐ๊พธ๊ธฐ</button>
</body>
</html>
2. ์ฐ์ต๋ฌธ์
๊ฐ์ฒด ์์ฑ์๋ฅผ ๋ง๋ค๊ณ ์ด๋ฆ, ๊ตญ์ด์ ์, ์์ด์ ์๋ฅผ ์ ๋ ฅํ๋ฉด ํ๊ท ์ ๋ง๋๋ ํจ์ ๋ง๋ค๊ธฐ
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ํจ์ </title>
<script>
function TestScore(name, kor, eng){
this.userName = name;
this.korNum = kor;
this.engNum = eng;
}
TestScore.prototype.getTestInfo = function() {
document.write("์ด๋ฆ: " + this.userName, "<br>");
document.write("๊ตญ์ด: " + this.korNum, "<br>");
document.write("์์ด: " + this.engNum, "<br>");
}
TestScore.prototype.getAvg = function() {
return (this.korNum + this.engNum ) / 2;
}
var kimgun = new TestScore("๊น๊ตฐ",80,90);
var ohgun = new TestScore("์ค๊ตฐ",100,80);
kimgun.getTestInfo();
document.write("ํ๊ท ์ ์:" + kimgun.getAvg(), "<br><br>");
ohgun.getTestInfo();
document.write("ํ๊ท ์ ์:" + ohgun.getAvg(), "<br>");
</script>
</head>
<body>
</body>
</html>
728x90