๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript/JavaScript

[JavaScript] ์ œ์–ด๋ฌธ

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 2.
๋ฐ˜์‘ํ˜•

โ€‹โ–ถ ๋„์ „ Mission!

 

Q1) ์ง„์˜์ด์˜ ํ•˜๋ฃจ ์ง€์ถœ ๋‚ด์—ญ์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๊ณ  ํ•  ๋•Œ, ํ•˜๋ฃจ ์ง€์ถœ ๋น„์šฉ์˜ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•œ ํ›„ ์ ์ • ์ง€์ถœ ๋น„์šฉ์˜ ์ดˆ๊ณผ ์—ฌ๋ถ€๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

์ง„์˜์ด์˜ ํ•˜๋ฃจ ์ง€์ถœ ๋‚ด์—ญ์€ ๊ตํ†ต๋น„ 3000์›, ์‹๋น„ 6000์›, ์Œ๋ฃŒ๋น„ 3000์›์ž…๋‹ˆ๋‹ค. ์‚ผํ•ญ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜๋ฃจ ์ ์ • ์ง€์ถœ ๋น„์šฉ์ธ 1๋งŒ ์›์„ ์ดˆ๊ณผํ–ˆ์„ ๊ฒฝ์šฐ์—๋Š” "000์› ์ดˆ๊ณผ"๋ผ๊ณ  ์ถœ๋ ฅํ•˜๊ณ , ์•„๋‹ ๊ฒฝ์šฐ์—๋Š” "๋ˆ ๊ด€๋ฆฌ ์ž˜ ํ–ˆ์–ด์š”!"๋ผ๊ณ  ์ถœ๋ ฅํ•˜์‹œ์˜ค.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์ข…ํ•ฉ์‹ค์Šต1-1 </title>
<script>
	var price1 = 3000;
	var price2 = 3000;
	var price3 = 3000;

	var total = price1 + price2 + price3;
	var result = total > 10000 ? total-10000 + "์› ์ดˆ๊ณผ": "๋ˆ ๊ด€๋ฆฌ ์ž˜ ํ–ˆ์–ด์š”!";
	document.write(result);
</script>
</head>
<body>
</body>
</html>

 

 

Q2) ๋‹ค์Œ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‰๊ท  ํŒ๋งค๋Ÿ‰์„ ๊ตฌํ•˜๊ณ , 4๋ถ„๊ธฐ ํŒ๋งค๋Ÿ‰์„ ์ž…๋ ฅํ•˜์—ฌ ํ‰๊ท  ํŒ๋งค๋Ÿ‰์˜ ์ด์ƒ/๋ฏธ๋‹ฌ ๊ฐ’์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

์งˆ์˜์‘๋‹ต ์ฐฝ์„ ์ด์šฉํ•˜์—ฌ 4๋ถ„๊ธฐ ํŒ๋งค๋Ÿ‰์„ ์ž…๋ ฅํ•˜๊ณ , ์ „ ๋ถ„๊ธฐ ํ‰๊ท  ํŒ๋งค๋Ÿ‰๋ณด๋‹ค ํŒ๋งค๋Ÿ‰์ด ๋†’๊ฑฐ๋‚˜ ๊ฐ™์„ ๊ฒฝ์šฐ์—๋Š” 'ํŒ๋งค๋Ÿ‰์ด ํ‰๊ท  ์ด์ƒ์ž…๋‹ˆ๋‹ค.'๋ผ๊ณ  ์ถœ๋ ฅํ•˜๊ณ , ๋‚ฎ์„ ๊ฒฝ์šฐ์—๋Š” 'ํŒ๋งค๋Ÿ‰์ด ํ‰๊ท  ๋ฏธ๋‹ฌ์ž…๋‹ˆ๋‹ค.'๋ผ๊ณ  ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜์‹œ์˜ค.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์ข…ํ•ฉ์‹ค์Šต1-2 </title>
<script>
	var avgSales = ( 1200 + 1300 + 1000 ) / 3; //ํ‰๊ท  ํŒ๋งค๋Ÿ‰ 
	var fourth = prompt("4๋ถ„๊ธฐ ํŒ๋งค๋Ÿ‰์€?", "0");
	var msg_1 = "ํŒ๋งค๋Ÿ‰์ด ํ‰๊ท  ์ด์ƒ์ž…๋‹ˆ๋‹ค.",
		msg_2 = "ํˆ๋งค๋Ÿ‰์ด ํ‰๊ท  ๋ฏธ๋‹ฌ์ž…๋‹ˆ๋‹ค.";
	
	var result =  fourth >= avgSales ? msg_1 : msg_2;
	document.write(result);
</script>
</head>
<body>
</body>
</html>

 

 

 

 

์ œ์–ด๋ฌธ
์กฐ๊ฑด๋ฌธ, ์„ ํƒ๋ฌธ, ๋ฐ˜๋ณต๋ฌธ

โ–ถ ์กฐ๊ฑด๋ฌธ - if๋ฌธ

if๋ฌธ์€ ์กฐ๊ฑด์‹์„ ๋งŒ์กฑ(true)ํ•  ๊ฒฝ์šฐ์—๋งŒ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ์กฐ๊ฑด์‹์€ Boolean( ) ๋‚ด์žฅ ๋ฉ”์„œ๋“œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ทธ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ด๋„ true ๋˜๋Š” false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

์˜ˆ์ œ 1) ๊ฑธ์Œ ์ˆ˜ ์งˆ์˜์‘๋‹ต์ฐฝ

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> if๋ฌธ </title>
<script>
	var walkAmount = prompt("๋‹น์‹ ์˜ ํ•˜๋ฃจ ๊ฑท๋Š” ์–‘์€ ๋ช‡ ๋ณด์ธ๊ฐ€์š”?", "0");

	if(walkAmount >= 10000) {
		document.write("๋งค์šฐ ์ข‹์€ ์Šต๊ด€์„ ์ง€๋‹ˆ๊ณ  ๊ณ„์‹œ๋Š”๊ตฐ์š”!!", "<br>");
	}
	document.write("=========== The End ===========");    
</script>
</head>
<body>
</body>
</html>

10000๋ณด ์ด์ƒ

 

10000๋ณด ๋ฏธ๋งŒ

 

 

์˜ˆ์ œ 2) ํ†ตํ™”๋Ÿ‰ ์งˆ์˜์‘๋‹ต์ฐฝ

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> if๋ฌธ </title>
<script>
	var min = prompt("๋‹น์‹ ์˜ ํ•˜๋ฃจ ํ†ตํ™”๋Ÿ‰์€ ๋ช‡ ๋ถ„์ธ๊ฐ€์š”?", "0");
	if(min >= 60) { document.write("๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํŽธ์ด๋„ค์š”", "<br>"); }
	document.write("===== The End ===="); 
</script>
</head>
<body>
</body>
</html>

 

 

โ–ถ ์กฐ๊ฑด์‹์— ๋…ผ๋ฆฌํ˜• ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ํ˜•์ด ์˜ค๋Š” ๊ฒฝ์šฐ

์กฐ๊ฑด์‹์— ๋…ผ๋ฆฌํ˜• ๋ฐ์ดํ„ฐ(true, false)๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ํ˜•์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋˜์–ด๋„ true ๋˜๋Š” false๋กœ ์ธ์‹๋œ๋‹ค.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> if๋ฌธ </title>
<script>
	var userName = prompt("๋ฐฉ๋ฌธ์ž์˜ ์ด๋ฆ„์€?", "");

	if(userName) {
		document.write(userName+"๋‹˜ ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค! ๋ฐฉ๋ฌธ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.");
	} 
</script>
</head>
<body>
</body>
</html>

 

 

โ–ถ ์กฐ๊ฑด๋ฌธ - else๋ฌธ

์กฐ๊ฑด์‹์„ ๋งŒ์กฑํ• (true) ๊ฒฝ์šฐ์™€ ๋งŒ์กฑํ•˜์ง€ ์•Š์„(false) ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค. ์ฆ‰, ๋‘ ๊ฐ€์ง€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

โ€‹

 

์˜ˆ์ œ 1) ๋ฐฉ๋ฌธ์ž์—๊ฒŒ ์งˆ์˜์‘๋‹ต ์ฐฝ์œผ๋กœ ์ข‹์•„ํ•˜๋Š” ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  if~else๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅ๋œ ๊ฐ’์ด ์ง์ˆ˜์ธ์ง€, ํ™€์ˆ˜์ธ์ง€์— ๋”ฐ๋ผ ์ถœ๋ ฅ๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋‚˜ํƒ€๋‚˜๋„๋ก ํ•˜๋Š” ์˜ˆ์ œ

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> else๋ฌธ </title>
<script>
	var num = prompt("๋‹น์‹ ์ด ์ข‹์•„ํ•˜๋Š” ์ˆซ์ž๋Š”?", "0");

	if(num % 2 == 0) {
		document.write("๋‹น์‹ ์ด ์ข‹์•„ํ•˜๋Š” ์ˆซ์ž๋Š” ์ง์ˆ˜์ž…๋‹ˆ๋‹ค.");
	} else {
		document.write("๋‹น์‹ ์ด ์ข‹์•„ํ•˜๋Š” ์ˆซ์ž๋Š” ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.");
	}
</script>
</head>
<body>
</body>
</html>

 

 

์˜ˆ์ œ 2) confirm( ) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ํ™•์ธ/์ทจ์†Œ ์ฐฝ์„ ๋‚˜ํƒ€๋‚ด์–ด ํšŒ์› ํƒˆํ‡ด ์—ฌ๋ถ€๋ฅผ ๋ฌป๋Š” ์ฐฝ์„ ๋งŒ๋“œ๋Š” ์˜ˆ์ œ

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> else๋ฌธ </title>
<script>
	var result = confirm("์ •๋ง๋กœ ํšŒ์›์„ ํƒˆํ‡ดํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");

	if(result) {
		document.write("ํƒˆํ‡ด ์ฒ˜๋ฆฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!");
	} else {
		document.write("ํƒˆํ‡ด ์ทจ์†Œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!");
	}
</script>
</head>
<body>
</body>
</html>

 

 

โ–ถ ์กฐ๊ฑด๋ฌธ - else if๋ฌธ

๋‘ ๊ฐ€์ง€ ์ด์ƒ์˜ ์กฐ๊ฑด์‹๊ณผ ์ •ํ•ด ๋†“์€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> else if๋ฌธ </title>
<script>
	var mon = prompt("ํ˜„์žฌ๋Š” ๋ช‡ ์›”์ž…๋‹ˆ๊นŒ?", "0");

	if(mon >= 9 && mon <= 11){ 
		document.write("๋…์„œ์˜ ๊ณ„์ ˆ ๊ฐ€์„์ด๋„ค์š”!!");
	}else if(mon >= 6 && mon <= 8){
		document.write("์—ฌํ–‰ ๊ฐ€๊ธฐ ์ข‹์€ ์—ฌ๋ฆ„์ด๋„ค์š”!!");
	}else if(mon >= 3 && mon <= 5){
		document.write("ํ–‡์‚ด ๊ฐ€๋“ํ•œ ๋ด„์ด๋„ค์š”!!");
	}else{
		document.write("์Šคํ‚ค์˜ ๊ณ„์ ˆ ๊ฒจ์šธ์ด๋„ค์š”!!");
	}
</script>
</head>
<body>
</body>
</html>

 

 

โ–ถ ์กฐ๊ฑด๋ฌธ - ์ค‘์ฒฉ if๋ฌธ

์กฐ๊ฑด๋ฌธ ์•ˆ์— ์กฐ๊ฑด๋ฌธ์ด ์žˆ๋Š” ๊ฒƒ.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์ค‘์ฒฉ if๋ฌธ </title>
<script>
	var id = "easy1004";
	var pw = "112233";

	var user_id = prompt("์•„์ด๋””๋Š”?","");
	var user_pw = prompt("๋น„๋ฐ€๋ฒˆํ˜ธ๋Š”?","");

	if(id == user_id) {
		if(pw == user_pw) {
				document.write(user_id+"๋‹˜ ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค!");
		} else {
				alert("๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
				location.reload();
		}
	} else {
		alert("์•„์ด๋””๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
		location.reload();
	}
</script>
</head>
<body>
</body>
</html>

 

 

 

 

์„ ํƒ๋ฌธ

โ–ถ Switch๋ฌธ

์„ ํƒ๋ฌธ์ธ switch๋ฌธ์€ ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๊ฐ’๊ณผ switch๋ฌธ์— ์žˆ๋Š” ๊ฒฝ์šฐ(case)์˜ ๊ฐ’์„ ๊ฒ€์‚ฌํ•˜์—ฌ ๋ณ€์ˆ˜์™€ ๊ฒฝ์šฐ์˜ ๊ฐ’์—์„œ ์ผ์น˜ํ•˜๋Š” ๊ฐ’์ด ์žˆ์„ ๋•Œ ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. if๋ฌธ๊ณผ ์šฉ๋„๋Š” ๋น„์Šทํ•˜๋‚˜ if๋ฌธ์€ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ์— ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๊ณ , switch๋ฌธ์€ ์—ฌ๋Ÿฌ ๊ฒฝ์šฐ์˜ ๊ฐ’ ์ค‘ ์ผ์น˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์•„ ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ฌ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์„ ํƒ๋ฌธ </title>
<script>
	var site = prompt("๋„ค์ด๋ฒ„, ๋‹ค์Œ, ๋„ค์ดํŠธ, ๊ตฌ๊ธ€ ์ค‘ \
	์ฆ๊ฒจ ์‚ฌ์šฉํ•˜๋Š” ํฌํ„ธ ๊ฒ€์ƒ‰ ์‚ฌ์ดํŠธ๋Š”?", "");
	var url;

	switch(site){
		case "๊ตฌ๊ธ€": url = "www.google.com";
		break;
		case "๋‹ค์Œ": url = "www.daum.net";
		break;
		case "๋„ค์ด๋ฒ„": url = "www.naver.com";
		break;
		case "๋„ค์ดํŠธ": url = "www.nate.com";
		break;
		default: alert("๋ณด๊ธฐ ์ค‘์— ์—†๋Š” ์‚ฌ์ดํŠธ์ž…๋‹ˆ๋‹ค.");
	}

	if(url) location.href = "http://" + url;
</script>
</head>
<body>
</body>
</html>

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€