2016-06-18 47 views
-1

1.我在代碼的第一條評論中包含了我想要組合的兩條線。如果你能指出我需要做什麼,這真的會有所幫助。我幾天前剛開始使用JavaScript。如果你能推薦任何書籍,請告訴我。即時通訊正在閱讀murach系列書籍及其真正的幫助。如何結合這些特定的Javascript語句?

/*The two statements i want to combine are 
    entry = parseInt(entry); 
    var score1 = entry; */ 

    <!DOCTYPE html> 
    <html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Average Test Scores</title> 
     <script> 
      var entry; 
      var average; 
      var total = 0; 

      //get 3 scores from user and add them together 
      entry = prompt("Enter test score"); 
      entry = parseInt(entry); 
      var score1 = entry; 
      total = total + score1; 

      entry = prompt("Enter test score"); 
      entry = parseInt(entry); 
      var score2; 
      total = total + score2; 

      entry = prompt("Enter test score"); 
      entry = parseInt(entry); 
      var score3 = entry; 
      total = total + score3; 

      //calculate the average 
      average = parseInt(total/3); 
     </script> 
    </head> 
    <body> 
     <script> 
      document.write("<h1>The Test Scores App</h1>"); 
      document.write("Score 1 = " + score1 + "<br>" + 
       "Score 2 = " + score2 + "<br>" + 
       "Score 3 = " + score3 + "<br><br>" + 
       "Average score = " + average + "<br><br>"); 
     </script> 
     Thanks for using the Test Scores application! 
    </body> 
    </html> 
+0

'變種score1 = parseInt函數(條目); * /' – Rayon

+0

目前還不清楚你的問題是什麼。通過合併這些線,你的意思是什麼? 'var score1 = parseInt(entry);'?爲什麼所有額外的代碼,而不僅僅是這兩行?我明白你有更具體的問題,你沒有直接問。 –

+0

這是本書的練習。它告訴我以某種方式結合這兩條線。所以我只是發佈整個文件,以便大家可以看到。 –

回答

0

Addition assignmentaddition assignment操作者增加了right operand的值給變量,並將結果賦給該變量。

Unary plus (+)unary plus operator先於其操作數,並計算其操作數,而是試圖將其轉換成number,如果它是不是已經。

簡化版本:

var total = 0; 
 
var score1 = +prompt("Enter test score"); //Cast it to Number 
 
total += score1; //Add it to total 
 
var score2 = +prompt("Enter test score"); 
 
total += score2; 
 
var score3 = +prompt("Enter test score"); 
 
total += score3; 
 
var average = parseInt(total/3); 
 
document.write("<h1>The Test Scores App</h1>"); 
 
document.write("Score 1 = " + score1 + "<br>" + 
 
    "Score 2 = " + score2 + "<br>" + 
 
    "Score 3 = " + score3 + "<br><br>" + 
 
    "Average score = " + average + "<br><br>");