배열 - 이름, 숫자

조수경·2021년 11월 18일
0

HTML

목록 보기
57/96
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel = "stylesheet" href = "../css/mystyle.css" type="text/css">

<script>
function proc1(){
	names = new Array();
	
	while(true){
		//입력
		input = prompt("이름을 입력하세요");
			
		//비교 - 입력없이 확인버튼 눌렀거나 취소 버튼 누를때
		if(input == null || input.trim().length < 1)break;	
			
		//배열에 저장
		names.push(input);
	}
	//배열을 출력
	document.getElementById('result1').innerHTML = names;
}


function proc2(){
	
	items = new Array();
	
	str = "";
	
	while(true){
		input = prompt("수를 입력해주세요");
	
		str += input + " "
		
		//배열저장 여부 비교
		idx = items.indexOf(input); //배열에서 요소를 찾아 위치를 리턴한다.
		if(idx < 0) items.push(input); //배열 끝에 요소를 추가한다.
			
		//계속할지 그만할지 판단하기
		if(items.length >= 5) break;

	}
	
	res = "입력한 수 : " + str + "<br>"
	res += "배열저장 한 수 : " + items + "<br>"
	
	document.getElementById('result2').innerHTML = res;
}
</script>

</head>
<body>

<div class = "box">
사람 이름을 계속 입력 받아 배열에 저장하고 그 저장된 이름을 출력하는 프로그램을 작성하시오.<br>
(단, 입력은 prompt 명령을 이용하고, 입력의 마지막은 공백문자를 입력하거나 "취소" 버튼을 눌렀을 때로 한다. <br>
"취소" 버튼은 null 이 입력될 때이다.)<br>
   <br>
  <button type = "button" onclick="proc1()">확인</button>
  <div id = "result1"></div>
</div>

<div class = "box">
수를 입력받는다. (입력시 중복 될수도 안될수도 있음)<br>
서로 중복되지 않은 정수 5개를 배열에 저장한다.<br>
출력하는 프로그램을 작성하시오<br>
   <br>
  <button type = "button" onclick="proc2()">확인</button>
  <div id = "result2"></div>
</div>

</body>
</html>

profile
신입 개발자 입니다!!!

0개의 댓글