Yahoo 知識+ 將於 2021 年 5 月 4 日 (美國東岸時間) 停止服務,而 Yahoo 知識+ 網站現已轉為僅限瀏覽模式。其他 Yahoo 資產或服務,或你的 Yahoo 帳戶將不會有任何變更。你可以在此服務中心網頁進一步了解 Yahoo 知識+ 停止服務的事宜,以及了解如何下載你的資料。

請教Javascript程式,想修改一下@@

你好,我打算用javascript設計一個等級計算器,可是不太會,已經有大大賜教了謝謝~不過想修改一下,可是不會,有大大能教我一下嗎?

https://tw.knowledge.yahoo.com/question/question?q...

==============

<script type="text/javascript">

function calculate() {

var total = 0;

total += get_score("page1", "result1");

total += get_score("page2", "result2");

total += get_score("page3", "result3");

var total_grade = get_grade(total / 3);

document.getElementById("overall").innerHTML = total_grade;

}

function get_score(score_from, result_to) {

var score = parseInt(document.getElementById(score_from).value);

document.getElementById(result_to).innerHTML = get_grade(score);

return score

}

function get_grade(score) {

var result = "D";

if (score >= 90) {

result = "A";

} else if (score >= 80) {

result = "B";

}

return result;

}

</script>

===================

想修改的部份:

例如說卷一、卷二、卷三不同分數給相同等級才對。

卷一90分A級

卷二70分A級

卷三60分A級

只是很大概教一下就可以了,謝謝。初學者,想很久都沒頭緒。

1 個解答

評分
  • ?
    Lv 7
    6 年前
    最愛解答

    <script type="text/javascript">

    var GradeStrategy = function() {

    this.strategy = "";

    };

    GradeStrategy.prototype = {

    setStrategy: function(strategy) {

    this.strategy = strategy;

    },

    getGrade: function(score) {

    return this.strategy.getGrade(score);

    }

    };

    var GradeStrategy1 = function() {

    this.getGrade = function(score) {

    if (score >= 90) return "A";

    if (score >= 80) return "B";

    return "F";

    }

    };

    var GradeStrategy2 = function() {

    this.getGrade = function(score) {

    if (score >= 70) return "A";

    if (score >= 60) return "B";

    return "F";

    }

    };

    var GradeStrategy3 = function() {

    this.getGrade = function(score) {

    if (score >= 60) return "A";

    if (score >= 50) return "B";

    return "F";

    }

    };

    var OverallStrategy = function() {

    this.getGrade = function(score) {

    if (score >= 90) return "A";

    if (score >= 80) return "B";

    if (score >= 70) return "C";

    if (score >= 60) return "D";

    if (score >= 50) return "E";

    return "F";

    }

    };

    function calculate() {

    var strategy = new GradeStrategy();

    var total = 0;

    strategy.setStrategy(new GradeStrategy1());

    total += get_score("page1", "result1", strategy);

    strategy.setStrategy(new GradeStrategy2());

    total += get_score("page2", "result2", strategy);

    strategy.setStrategy(new GradeStrategy3());

    total += get_score("page3", "result3", strategy);

    strategy.setStrategy(new OverallStrategy());

    var total_grade = strategy.getGrade(total / 3);

    document.getElementById("overall").innerHTML = total_grade;

    }

    function get_score(score_from, result_to, strategy) {

    var score = parseInt(document.getElementById(score_from).value);

    document.getElementById(result_to).innerHTML = strategy.getGrade(score);

    return score;

    }

    </script>

還有問題嗎?立即提問即可得到解答。