试图找到平均值

Trying to find the average

本文关键字:平均值      更新时间:2023-12-15

当我试图划分学生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");

}

您希望学生成绩的总和是数字,但事实上它们被连接为字符串。因此,如果用户键入以下值,例如:学生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?"));