2015-10-02 73 views
0

我試圖創建在JavaScript中檔次的任意值等級計算器。代碼運行時,代碼的任何部分都不會被執行。在我的代碼的語法中是否存在明顯的錯誤? 謝謝, 本級計算器的JavaScript未運行

var numberofgrades = prompt("How many grades do you want to calculate?"); 
var countingvalue = 1; 
var grades = [0]; 
var tempgrade = 0; 
var averagetotal = 0; 
for(i=0;i<numberofgrades;i++;){ 
if(countingvalue!=numberofgrades){ 
    var tempgrade= prompt("What is your "+countingvalue"th grade?") 
    grades.push(tempgrade); 
    countingvalue++; 
} 
else if(countingvalue==numberofgrades){ 
    for(var j=1;j=<numberofgrades;j++){ 
     averagetotal = grades[j]+averagetotal; 
    } 
} 
    alert("Your average grade is: "+(averagetotal/numberofgrades)); 

最新編輯的代碼 -

var numberofgrades = prompt("How many grades do you want to calculate?"); 
var countingvalue = 1; 
var grades = [0]; 
var tempgrade = 0; 
var averagetotal = 0; 
for(i=0;i<numberofgrades;i++){ 
if(countingvalue!=numberofgrades){ 
    var tempgrade= prompt("What is your "+countingvalue+"th grade?") 
    grades.push(tempgrade); 
    countingvalue++; 
} 
else if(countingvalue==numberofgrades){ 
    for(var j=1;j<=numberofgrades;j++){ 
     averagetotal = grades[j]+averagetotal; 
    } 
    } 
} 
alert("Your average grade is: "+(averagetotal/numberofgrades)); 

我的語法只是固定的這個問題,並正在運行。我有問題添加兩個數字,而不是我的程序連接值。感謝您的幫助, 本

var averagetotal = (grades[j]+averagetotal); 

這是整個代碼。

var numberofgrades = prompt("How many grades do you want to calculate?"); 
var countingvalue = 0; 
var grades = []; 
var tempgrade = 0; 
var averagetotal = 0; 
for(var i=0;i<=numberofgrades;i++){ 
if(countingvalue!=numberofgrades){ 
    if(countingvalue==1){ 
    var tempgrade= prompt("What is your "+(countingvalue+1)+"st grade?"); 
    grades.push(tempgrade); 
    countingvalue++; 
} 
    else if(countingvalue!=1){ 
    var tempgrade= prompt("What is your "+(countingvalue+1)+"th grade?"); 
    grades.push(tempgrade); 
    countingvalue++; 
    } 
} 
else if(countingvalue==numberofgrades) 
{ 
    for(var j=0;j<numberofgrades;j++){ 
     var averagetotal = (grades[j]+averagetotal); // problem line 
     alert(j+" "+averagetotal); //checking values 
    } 
} 


} 
alert(grades[0]); //just checking values 
alert(grades[1]); //checking values 
alert(averagetotal); 
alert("Your average grade is: "+(averagetotal/numberofgrades)); 

回答

1

您有語法錯誤。取而代之的

for(i=0;i<numberofgrades;i++;) 

應該

for(i=0;i<numberofgrades;i++) 

代替

var tempgrade= prompt("What is your "+countingvalue"th grade?") 

也應該是

var tempgrade= prompt("What is your "+countingvalue+"th grade?") 

for(var j=1;j=<numberofgrades;j++){ 

,而不是

應該

for(var j=1;j<=numberofgrades;j++){ 

終於有似乎是你的括號中的問題 - 你缺少}

+0

變量應保持本地和位空白的幫助:'用於(VAR I = 0; I RobG

+0

謝謝,但程序仍然不會運行。 –

+0

明白了,我通常只是想保證我的所有變量都在適當的範圍內。 –

1

試試這個代碼

<script> 
var numberofgrades = prompt("How many grades do you want to calculate?"); 
var countingvalue = 1; 
var grades = [0]; 
var tempgrade = 0; 
var averagetotal = 0; 
for(var i=0;i<numberofgrades;i++){ 
if(countingvalue!=numberofgrades) 
{ 
    var tempgrade= prompt("What is your "+countingvalue+"th grade?"); 
    grades.push(tempgrade); 
    countingvalue++; 
} 
else if(countingvalue==numberofgrades) 
{ 
    for(var j=1;j<numberofgrades;j++){ 
     averagetotal = grades[j]+averagetotal; 
    } 
} 

    alert("Your average grade is: "+(averagetotal/numberofgrades)); 
} 

</script> 
+0

不,我用firefox和chrome試了一下。我發佈整頁代碼。 –

+0

嘗試在Chrome上打開新標籤 –

+0

https:// jsfiddle。net/73nkothp /檢查這個 –