我想在Javascript中获得总分和评论(基于总分)

时间:2017-02-01 16:37:02

标签: javascript

在下面的代码中,我尝试计算总分,并根据总分小于10,它会显示一些评论,否则其他评论。所以基本上我需要得分和评论。

尝试时,如果我使用calculateTotal函数并删除getComment函数,则可以正常工作,反之亦然。但是,当我提出这两个功能时,什么都没有。这是我的代码。

function calculateTotal()
{
    //Here we get the total Score by calling our function
    //Each function returns a number so by calling them we add the values they return together
    var Scoring = getScore() + getScore1() + getScore2() + getScore3() + getScore4();

    //display the result
    var divobj = document.getElementById('totalScore');
    divobj.style.display='block';
    divobj.innerHTML = "Total Score for the selected answer "+Scoring;
    getComment(Scoring);
}

getComment(Scoring)
if (totalScore == 10) {
    document.getElementById("displaycomment").innerHTML = "Excellent Result"+Scoring;
}
else {
    document.getElementById("displaycomment").innerHTML = "See the guidebook"+Scoring;
}

1 个答案:

答案 0 :(得分:0)

尝试相同的objet方法。

代码片段无法复制到html并运行示例。

var a = 2;

function getScore() {
    return a;
}

function getScore1() {
    return a;
}

function getScore2() {
    return a;
}

function getScore3() {
    return a;
}

function getScore4() {
    return a;
}

var calcClass = new function() {
    this.Scoring = function() {
        return getScore() + getScore1() + getScore2() + getScore3() + getScore4();
    }

    this.divobj = document.getElementById('totalScore');
    this.divobj.style.display = 'block';
    this.divobj.innerHTML = "Total Score for the selected answer " + this.Scoring();

    this.getComment = function() {
        if (this.Scoring() == 10) {
            document.getElementById("displaycomment").innerHTML = "Excellent Result " + this.Scoring();
        } else {
            document.getElementById("displaycomment").innerHTML = "See the guidebook " + this.Scoring();
        }
    }
}
alert("total " + calcClass.Scoring());
calcClass.getComment();

//try to console see and check result
//a = 3;
//calcClass.getComment();
//alert("total " + calcClass.Scoring());




<div class="scoregroup">total score <div id="totalScore"></div></div>
<div class="scoregroup">display comment <div id="displaycomment"></div>      </div>