成绩计算器JavaScript未运行

时间:2015-10-02 21:57:19

标签: 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));

2 个答案:

答案 0 :(得分: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++){

最后您的括号似乎有问题 - 您最后缺少}

答案 1 :(得分: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>