你好,我试图让用户输入几个标记,最后给出最高分和最低分,但由于某种原因,它打印两次,第二次打印无限和想法?
var gradeAwarded;
var gradeAwarded;
var repeate = true;
var max;
var A;
var B;
var C;
var F;
var repeat, studentArr = [], markArr = [];
while (repeat !== 'n' && repeat !== 'N'){
studentArr.push(prompt("Enter Student Name: "));
markArr.push(parseInt (prompt("Enter Student mark: ")));
repeat = prompt ("Do you want to enter another student: y/n");
}
Array.prototype.max = function() {
return Math.max.apply(Math, markArr);
};
Array.prototype.min = function() {
return Math.min.apply(Math, markArr);
};
var min = markArr.min();
var max = markArr.max();
if (markArr <0 || markArr >100){
alert("Grate out of bounds");
}else if (markArr >= 83){
gradeAwarded = "A";
A = +1;
}else if (markArr >= 70){
gradeAwarded = "B";
B = +1;
}else if (markArr >= 50){
gradeAwarded = "C";
C = +1;
}else if (markArr >= 0){
gradeAwarded = "F";
F = +1;
}
document.write("The Highest mark was : "+max);
document.write("The Lowest mark was : "+min);
答案 0 :(得分:1)
你使用while循环,所以第二次运行脚本时它永远不会运行,因为repeat =='n'。 markArry被重置,这就是第二次运行脚本时无限制的原因。
你应该使用do while循环。像这样:
do {
studentArr.push(prompt("Enter Student Name: "));
markArr.push(parseInt (prompt("Enter Student mark: ")));
repeat = prompt ("Do you want to enter another student: y/n");
}while(repeat !=='n' || repeat !=='N');
应该有效
答案 1 :(得分:1)
必须是||而不是&amp;&amp;
while (repeat !== 'n' || repeat !== 'N'){
studentArr.push(prompt("Enter Student Name: "));
markArr.push(parseInt (prompt("Enter Student mark: ")));
repeat = prompt ("Do you want to enter another student: y/n");
}
并且你应该清除或将 null 设置为重复变量和