平均数字和显示分组平均值

时间:2018-01-23 17:45:19

标签: javascript

我正在尝试编写一个代码,提示用户添加4个数字并将其取平均值,具体取决于该答案是100 +,75-100,50-75,50-它会检查它是否在指定的数字之间区域并分别在提示框中显示答案。 编辑无论如何,代码都不会提供低于75-100组的任何内容。例如,如果我输入四个20,它表示它介于75和100之间(一个+等级),这对我来说毫无意义。如果有人有想法,请帮助。

<!DOCTYPE html>
<html>
<head>
    <title> Looping assignment
    </title>
</head>

<script>
    var grades = new Array()

        for (i = 0; i < 4; i++) {
            grades[i] = parseFloat(prompt("Enter your grades:"))  
        }

    var total = (grades[0] + grades[1] + grades[2] + grades[3])
    var average = (total / 4)

        if ( average >= 100)
            alert("How is the average higher than 100!  A+ grade/n " + average);
        if ( average > 75 && average <99)
            alert("The average is between 75 and 100.  B grade  /n " + average);
        if ( average > 50 && average <75)
             alert("The average is between 75 and 50.  C grade /n " + average);
</script>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

您需要使用&&运算符代替;

&#13;
&#13;
var grades = new Array()

for (i = 0; i < 4; i++) {
  grades[i] = parseFloat(prompt("Enter your grades:"));
}

var total = (grades[0] + grades[1] + grades[2] + grades[3])
var average = (total / 4)

if (average >= 100)
  alert("How is the average higher than 100!  A+ grade/n " + average);
if (average > 75 && average <=99)
  alert("The average is between 75 and 100.  B grade  /n " + average);
if (average > 50 && average <=75)
  alert("The average is between 75 and 50.  C grade /n " + average);
&#13;
&#13;
&#13;