JavaScript/기본

객체 (최소, 최대 평균)

dev_jiwon 2022. 6. 29.

객체 안에 메소드로 값을 한번에 받아오기

<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var Score = {
            in_score: 0,
            out_max_score: 0,
            out_min_score: 0,
            out_avg_score: 0,

            save_score: function (score) {
                this.in_score = score;
            },

            find_scoreMax: function () {
                for (var i = 0; i < this.in_score.length; i++) {
                    if (this.out_max_score < this.in_score[i])
                        this.out_max_score = this.in_score[i];
                }

                return this.out_max_score;
            },

            find_scoreMin: function () {
                this.out_min_score = this.in_score[0];
                for (var i = 0; i < this.in_score.length; i++) {
                    if (this.out_min_score > this.in_score[i])
                        this.out_min_score = this.in_score[i];
                }

                return this.out_min_score;
            },

            find_scoreAvg: function () {
                var sum = 0;
                for (var i = 0; i < this.in_score.length; i++) {
                    sum += this.in_score[i];
                }

                this.out_avg_score = sum / this.in_score.length;
                return this.out_avg_score;
            },

            function_result: function (score) {
                this.save_score(score);
                this.find_scoreMin();
                this.find_scoreMax();
                this.find_scoreAvg();

                document.write("min =" + this.out_min_score + "<br>");
                document.write("max =" + this.out_max_score + "<br>");
                document.write("avg =" + this.out_avg_score + "<br>");
                return [this.out_min_score, this.out_max_score, this.out_avg_score];
            }
        };

        var input = [10, 99, 33, 70, 20, 30, 50, 66];
        var s1 = Score.function_result(input);
    </script>
</body>

</html>

시나리오

성적 리스트를 받아서 세가지를 처리한 다음에 결과를 리턴한다.
입력: 성적리스트를 받는다. --> 파라미터
처리: 세가지 함수를 차례로 호출한다. --> 리턴이 온다.
리턴: 받은 세가지 결과를 리턴한다. 순서: 최소값, 최대값, 평균값

 

 

최소값

<script>
    var ScoreMin = {
        in_score: 0,
        out_min_score: 100,

        find_score: function (ele) {
            this.in_score = ele;
            for (var i = 0; i < this.in_score.length; i++) {
                if (this.out_min_score > this.in_score[i])
                    this.out_min_score = this.in_score[i];
            }
            return this.out_min_score;
        }
    };

    var o2 = ScoreMin.find_score([10, 30, 40]);
    document.write('min=' + o2 + '</br>');

</script>

 

 

최대값

<script>
    var ScoreMax = {
        in_score: 0,
        out_max_score: 0,

        find_score: function (ele) {
            this.in_score = ele;
            for (var i = 0; i < this.in_score.length; i++) {
                if (this.out_max_score < this.in_score[i])
                    this.out_max_score = this.in_score[i];
            }
            return this.out_max_score;
        }
    };

    var o3 = ScoreMax.find_score([10, 30, 40]);
    document.write('max=' + o3 + '</br>');

</script>

 

 

평균값

<script>
    var ScoreAvg = {
        in_score: 0,
        out_avg_score: 0,

        find_score: function (ele) {
            this.in_score = ele;
            var sum = 0;
            for (var i = 0; i < this.in_score.length; i++) {
                sum += this.in_score[i];
            }
            this.out_avg_score = sum / this.in_score.length;
            return this.out_avg_score;
        }
    };

    var o4 = ScoreAvg.find_score([10, 23, 40]);
    document.write('avg=' + o4)

</script>

 

 

 

 

 

 

객체 안에 메소드

<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var Score = {
            in_score: 0,
            out_max_score: 0,
            out_min_score: 100,
            out_avg_score: 0,

            find_scoreMax: function (ele) {
                this.in_score = ele;
                for (var i = 0; i < this.in_score.length; i++) {
                    if (this.out_max_score < this.in_score[i])
                        this.out_max_score = this.in_score[i];
                }
                return this.out_max_score;
            },

            find_scoreMin: function (ele) {
                this.in_score = ele;
                for (var i = 0; i < this.in_score.length; i++) {
                    if (this.out_min_score > this.in_score[i])
                        this.out_min_score = this.in_score[i];
                }
                return this.out_min_score;
            },

            find_scoreAvg: function (ele) {
                this.in_score = ele;
                var sum = 0;
                for (var i = 0; i < this.in_score.length; i++) {
                    sum += this.in_score[i];
                }
                this.out_avg_score = sum / this.in_score.length;
                return this.out_avg_score;
            }
        };

        var o1 = Score.find_scoreMax([10, 20, 30, 40, 50]);
        document.write("최대값= " + o1 +'</br>');

        var o2 = Score.find_scoreMin([10, 20, 30, 40, 50]);
        document.write("최소값= " + o2+'</br>');

        var o3 = Score.find_scoreAvg([10, 20, 30, 40, 50]);
        document.write("평균값= " + o3);
    </script>
</body>

</html>

'JavaScript > 기본' 카테고리의 다른 글

내장객체  (0) 2022.06.30
문서 객체 모델 (DOM)  (0) 2022.06.29
객체 (생성자)  (0) 2022.06.24
객체  (0) 2022.06.24
자바스크립트 참고 사이트  (0) 2022.06.23

댓글