我試圖創建在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));
變量應保持本地和位空白的幫助:'用於(VAR I = 0; I
RobG
謝謝,但程序仍然不會運行。 –
明白了,我通常只是想保證我的所有變量都在適當的範圍內。 –