Javascript 试图找到平均值

Javascript 试图找到平均值,javascript,Javascript,我需要知道当我尝试划分学生1、学生2和学生3时的原因。我认为这可能是一个循环错误,因为它给我的数字是数千,但我不知道怎么会发生这种情况 function averageOfThreeScores() { var student1; var student2; var student3; var end; do { student1 = prompt("What are the scorces of students 1?");

我需要知道当我尝试划分学生1、学生2和学生3时的原因。我认为这可能是一个循环错误,因为它给我的数字是数千,但我不知道怎么会发生这种情况

function averageOfThreeScores() {
    var student1;
    var student2;
    var student3;
    var end;
    do {
        student1 = prompt("What are the scorces of students 1?");
        student2 = prompt("What are the scorces of students 2?");
        student3 = prompt("What are the scorces of students 3?");
        end = prompt("Would you like to end, type yes to end.");
        var average = (student1 + student2 + student3) / 3;
    if (average <= 59) {
        document.write(average + " Your score is F <br/>");

    } else if (average <= 69) {
        document.write(average + " Your score is D <br/>");

    } else if (average <= 79) {
        document.write(average + " Your score is C <br/>");

    } else if (average <= 95) {
        document.write(average + "That's a great score <br/>");

    } else if (average <= 100) {
        document.write(average + "God like </br>");

    } else {
        document.write(average + " End <br/>");
    }

}
while (end != "yes");
三核函数平均值(){
var学生1;
var学生2;
var学生3;
var端;
做{
student1=提示(“student1的分数是多少?”);
student2=提示(“student2的分数是多少?”);
student3=提示(“student3的分数是多少?”);
end=prompt(“是否结束,键入yes结束”);
var平均值=(student1+student2+student3)/3;

如果(average您希望学生成绩的总和是数字,但事实上它们是作为字符串连接的。因此,如果用户键入以下值,例如: 学生1:13 学生2:36 学生3:50

平均值为44550,因为总和为(串联字符串):133650

若要解决此问题,只需在获得类型时将其转换为数字即可

student1 = parseInt(prompt("What are the scorces of students 1?"));
student2 = parseInt(prompt("What are the scorces of students 2?"));
student3 = parseInt(prompt("What are the scorces of students 3?"));

您至少需要
(student1+student2+student3)/3;
而不是
student1+student2+student3/3;
对于运算顺序,乘法优先于加法。average是一个数字。请尝试average.toString()@matthew Gunn我试过了,但它仍然给我高达2200的分数,以此类推,我正在寻找它,将三个学生的分数除以3,以求得平均值。@lamirap我不知道你的意思。你能给我发一个关于使用to.String方法的mozilla参考链接吗?你的意思是什么“除法不仅仅发生在乘法中”?乘法在哪里?while条件也很奇怪,因为您将数字分配给student1、student2和student3,然后在while条件中使布尔“and”运算等于字符串?