Javascript“变量”未定义

时间:2018-10-07 22:54:02

标签: javascript

我编写了一个脚本,该脚本从用户获取不同单位的成绩,然后将其添加到数组中,显示成绩以及平均值。

当我运行脚本时,它可以正常运行,但不是平均水平,而是显示“等级”未定义。

var subjects = ["CF:", "DaD:", "PoP:", "N&CS:", "SAD:", "APP:"];
var grades = ["", "", "", "", "", ""];
var result;

grades[0] = prompt("Please enter your marks for CF: ");
grades[1] = prompt("Please enter your marks for DaD: ");
grades[2] = prompt("Please enter your marks for PoP: ");
grades[3] = prompt("Please enter your marks for N&CS: ");
grades[4] = prompt("Please enter your marks for SAD: ");
grades[5] = prompt("Please enter your marks for APP: ");


console.log("Units and grades are: ");

console.log(subjects[0] +"\t"+ grades[0] );
console.log(subjects[1] +"\t"+ grades[1] );
console.log(subjects[2] +"\t"+ grades[2] );
console.log(subjects[3] +"\t"+ grades[3] );
console.log(subjects[4] +"\t"+ grades[4] );
console.log(subjects[5] +"\t"+ grades[5] );

result = grades[0] + grades[1] + grades[2] + grades[3] + grades[4] + 
grade[5];

console.log("Average: " +  "\t" + result / 6);

有人有什么想法吗? 对不好的JavaScript表示歉意,我对此很陌生。

1 个答案:

答案 0 :(得分:6)

在此代码中,

result = grades[0] + grades[1] + grades[2] + grades[3] + grades[4] + grade[5];

等级[5]必须为等级 s [5]。您错过了“ s”分。