2017-02-01 7 views
0

以下のコードでは合計スコアを計算しようとしましたが、合計スコアが10未満の場合はコメントが表示されます。コメント。だから基本的に私は合計スコアとコメントの両方が必要です。Javascriptの両方で合計スコアとコメントを(合計スコアに基づいて)欲しい

私が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; 
} 
+3

いいえ、あなたのコードではありません。これはコードのイメージです。エラーメッセージが表示されているかどうかを確認するためにコンソールを開いたことがありますか?また、あなたのイメージでは、getComment()関数は{...}で囲まれていません。 – Snowmonkey

+0

うわー。今はそれがあなたのコードのさらに荒いイメージです。それでも、getCommentは実際には関数ではありません。だからええ。 – Snowmonkey

+1

質問を編集し、コードサンプルをコードブロックに配置してください:http://stackoverflow.com/editing-help#code。そうすれば、他の人があなたの質問に対処しやすくなります。 –

答えて

0

同じオブジェクトメソッドを試してください。

スニペットが動作しません。htmlにコピーしてexampleを実行してください。

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> 
+0

Heyy Stack Overflow Gurusはありませんか? **このサンプルコードスニペットを使用しない理由は何ですか?**しかし、なぜ通常のページを実行するのですか... – Qh0stM4N

関連する問題