[javascript] prompt 예제

2022. 7. 18. 19:24JSP

728x90

✍️1. prompt 함수를 사용해서 등급(A,B,C)을 입력받은 후 변수에 저장,  switch문으로 해당 등급을 alert 함수로 출력하기

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<script>
	
		var grade=prompt('성적 입력(A/B/C) ');
		
		switch(grade){
		
		case 'A':
			alert('A등급 축하합니다.');
			break;
			
		case 'B':
			alert('B등급 화이팅');
			break;
			
		case 'C':
			alert('C등급 분발하세요');
			break;
			
		default:
			alert('잘못된 입력');
			
		}
	</script>
</body>
</html>

✍️2. 이중 for문과 document.write()를 사용해서 웹브라우저에 2~9단까지 구구단 출력하기

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>구구단</title>

	<style>
	
		div{
			display : inline-block;
			height: 30px;
			width:90px;
			color : olive;
			font-size:20px;
		}
	</style>
</head>
<body>

	<script>
	
		for(var i=2;i<=9;i++){
			
			document.write('<h2>'+i+'단</h2>');
			
			for(var j=1;j<=9;j++){
				
				document.write('<div>'+i+'x'+j+'='+ i*j+'</div>');
			}
		}
	
	</script>

</body>
</html>