[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