Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
Tags
- Spring
- 생성자
- JavaScript
- Graphic
- 전화걸기
- 메소드
- Menu
- layout
- oracle
- 클래스
- 국제화
- HTML
- Eclips
- 메서드
- struts2
- OGNL
- 에러페이지
- 안드로이드
- mybatis
- 오버로딩
- AWT
- 예외처리
- 배열
- Java
- 이클립스
- 어노테이션
- paint
- Android
- 기본
- JSP
Archives
- Today
- Total
note
sort() / slice()메서드 사용 / 학생 성적 정렬 본문
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
function Student(name,korean,math,english,science){
//속성
this.이름 = name;
this.국어 = korean;
this.수학 = math;
this.영어 = english;
this.과학 = science;
}
Student.prototype.getSum = function (){
return this.국어+this.수학+this.영어+this.과학;
};
Student.prototype.getAverage = function (){
return this.getSum() / 4;
};
Student.prototype.toString = function(){
return this.이름 + '\t' +this.getSum() + '\t'+this.getAverage();
};
//학생 정보 배열을 만듭니다.
var students= [];
students.push(new Student('윤인성', 87, 98, 88, 95));
students.push(new Student('연하진', 92, 98, 96, 98));
students.push(new Student('구지연', 76, 96, 94, 90));
students.push(new Student('나선주', 98, 92, 96, 92));
students.push(new Student('윤아린', 95, 98, 98, 98));
students.push(new Student('윤명월', 64, 88, 92, 92));
students.push(new Student('김미화', 82, 86, 98, 88));
students.push(new Student('김연화', 88, 74, 78, 92));
students.push(new Student('박아현', 97, 92, 88, 95));
students.push(new Student('서준서', 45, 52, 72, 78));
//정렬하고 1등부터 3등까지만 배열에 남겨둡니다.
students.sort(function (left,right){
return right.getSum() - left.getSum();
});
students = students.slice(0,3);
//출력합니다.
var output = '이름\t총점\t평균\n';
for(var i in students){
output += students[i].toString()+'\n';
}
alert(output);
</script>
</head>
<body>
</body>
</html>
slice() 메서드를 사용하여 1~3등 까지만 남겨놓았습니다
'JavaScript > 기본' 카테고리의 다른 글
| 메서드의 함수화 / Math.max (0) | 2012.03.15 |
|---|---|
| 날짜 간격을 구하는 방법 (0) | 2012.03.15 |
| sort()메서드의 정렬 방식 지정 (0) | 2012.03.15 |
| Array 객체의 메서드를 사용할때 주의할점 / sort()메서드 사용 (0) | 2012.03.15 |
| String 객체의 메서드 사용 / toUpperCase() (0) | 2012.03.15 |