//global variable
counter=0;
var highestAvg=0;
var average = new Array();
var studObj = new Array();
var teachObj = new Array();
//Objects
var student = {
studName:"",
studentId:"",
courses:['a', 'b', 'c', 'd'],
marks:[]}
var teacher = {
profName:"",
course:"",
office:"",
email:""}
for(var i=0; i<30; i++)
{
studObj[i] = Object.create(student);
var check = prompt('Please enter students name(!!! to exit): ','');
if(check !== '!!!')
{
studObj[i].studName = check;
studObj[i].studentId = prompt('Please enter student ID: ','');
var total=0;
for(var j=0; j<4; j++)
{
studObj[i].marks[j] = prompt('Please enter student marks for ' + studObj[i].courses[j] + ' : ','');
if(studObj[i].marks[j] > 100 || studObj[i].marks[j] < 0)
{
alert("Incorrect mark, please enter a correct mark!")
j--;
}
else
{
total += Number(studObj[i].marks[j]);
}
}
average[i] = total/4;
counter++;
}
else
{
break;
}
alert(average[i]);
}
var highestAvg = Number(average[0]);
for(var x=1; x<counter; x++)
{
if((Number(average[x])) > highestAvg)
{
highestAvg = average[x];
var z=x
}
}
alert(highestAvg);
alert('The student with the highest overall average is: \nStudent Name: ' + studObj[z].studName + '\nStudent ID: ' + studObj[z].studentId + '\nCourses: ' + studObj[z].courses + '\nMarks: ' + studObj[z].marks + '\nAverage Mark: ' + average[z]);
我不知道为什么标记数组不会保存到studObj数组中,而是被覆盖。我已经尝试添加另一个数组来保存这些值,但它只能显示最后输入的4个标记。我在这里先向您的帮助表示感谢。Array只显示最后一组输入
TIL有被称为东西['prompt'](https://developer.mozilla.org/en-US/docs/Web/API/Window.prompt)在Javascript中。但我建议你不要过度使用目前的过度使用方式。 – bits
这对我来说很好,铬32.0.1700.107米。刚刚从控制台运行它,所有值都分配到正确的点。这个实现的浏览器和版本是什么?(这有点令人讨厌,你应该看看只是使用一个表单...)另外,而不是计数器,我可以建议使用'array.length'? –
您是否尝试过使用Math.max.apply(Math,average)'然后遍历'average'数组来获得学号?那么就不需要'var counter'。 – PHPglue