我一直试图找到一种方法来正确定义数组作为构造函数值之一。假设我们有一个学生,我们需要有一个成绩阵列,然后使用我们需要的阵列来获得学生成绩的平均分。可悲的是,我只发现了一些用其他编程语言解决这个问题的线程。这是我认为可行的:
function student(name, surname, number, grades) {
this.name = name;
this.surname = surname;
this.number = number;
this.grades = [];
this.average = function(grades) {
var sum = 0;
for(var i = 0; i < grades.length; i++) {
sum + = grades[i];}
var average = sum / grades.length;
return average;
}
}
然后
var student1 = new student("Peter","Cat",14444,[2,3,4]);
console.log(student1);
不幸的是,它显示我的成绩数组为空白,我无法查看我的平均功能是否正常工作。我应该更改哪些部分,以便在grade数组中实际有一些值?
谢谢。
答案 0 :(得分:2)
您可以通过使用ES6解决相同的问题,在您的代码中,您正在使用函数内部的空数组初始化this.grade=[]
,因此仅对空数组进行平均值的进一步处理。为了良好作法,应为函数参数分配一个默认值,这样,如果我们错误地不将数组作为参数传递,则函数参数将使用默认值。附加代码段,以便在ES6中轻松理解。
class std{
constructor(name, surname, number, grades = []) {
this.name = name;
this.surname = surname;
this.number = number;
this.grades = grades;
}
average() {
if(this.grades.length !== 0){
return this.grades.reduce((previous, current) => previous + current, 0) /
this.grades.length
} else { return "no grades mentioned"; }
}
}
var student1 = new std("Peter", "Cat", 14444, [1, 3, 4]);
console.log("Average: ", student1.average());
//add new student
var student2 = new std("Prasanna", "sasne", 14444);
console.log(student2);
//student2.grades.push(7)
//console.log("New Average: ", student2.average() )
答案 1 :(得分:1)
你有几件事搞砸了。如果您要将等级数组作为参数传递,那么您需要使用以下内容设置成绩:
this.grades = grades;
同样在平均功能中,你需要引用this.grades
成绩而不仅仅是成绩。这将允许您稍后添加更多成绩并仍然获得正确的平均值。您还可以考虑通过使用以下内容定义构造函数来使成绩可选:
function student(name, surname, number, grades =[])
然后,如果你没有传入一个值,那么一个空数组将等着你。
最后你可能会有:
function student(name, surname, number, grades = []) {
this.name = name;
this.surname = surname;
this.number = number;
this.grades = grades;
this.average = function() {
return this.grades.reduce((a, c) => a + c, 0) / this.grades.length
}
}
var student1 = new student("Peter", "Cat", 14444, [2, 3, 4]);
console.log("Average: ", student1.average())
// add another grade:
student1.grades.push(6)
console.log("New Average: ", student1.average() )
&#13;
答案 2 :(得分:1)
您已将grades
传递到student()
函数,因此您无需将其传递给student.average
函数(因为内部函数将会已经可以访问外部函数参数)。因此,您也不需要设置this.grades = []
。
此外,sum + = grades[i]
应为sum += grades[i]
。
简单地修复此错误,然后省略将grades
传递到内部函数中将正确显示平均值,如下所示:
function student(name, surname, number, grades) {
this.name = name;
this.surname = surname;
this.number = number;
this.average = function() {
var sum = 0;
for (var i = 0; i < grades.length; i++) {
sum += grades[i];
}
var average = sum / grades.length;
return average;
}
}
var student1 = new student("Peter", "Cat", 14444, [2, 3, 4]);
console.log(student1.average());
&#13;
答案 3 :(得分:0)
您对变量的初始化是一个空数组。
this.grades = [];
应该是
this.grades = grades;
但是,我建议你学习一些javascript ES6 / ECMAScript 2015并使用类。 this source
class Student {
constructor(grades) {
//... Other code
this.grades = grades;
}
}
答案 4 :(得分:0)
你可以使用这一行:
<块引用>function student(name, surname, number, grades){ }
而不是这个:
<块引用>function student(name, surname, number, grades = [ ]){ }
function student(name, surname, number, grades) {
this.name = name;
this.surname = surname;
this.number = number;
this.grades = grades;
this.average = function() {
return this.grades.reduce((a, c) => a + c, 0) / this.grades.length
}
}
var student1 = new student("Peter", "Cat", 14444, [2, 3, 4]);
console.log("Average: ", student1.average())
// add another grade:
student1.grades.push(6)
console.log("New Average: ", student1.average() )
答案 5 :(得分:0)
我们可以使用对象方法来存储平均值:
function Student(name, surname, number, grades = [])
{
this.name = name;
this.surname = surname;
this.number = number;
this.grades = grades;
//Method to calculate the Average
this.average = function()
{
if(this.grades.length !==0)
{
//Return the Average Calculation
return this.grades.reduce((previous, current) => previous + current, 0) / this.grades.length
}
else
{
//This will return if grades not provided
return "No Grades Mentioned"
}
}
}
//With Grades
var student1 = new Student("Srinivasan", "Raja", 1635, [22,43,67,89,90]);
console.log(student1, "Average: "+ student1.average());
//Without Grades
var student1 = new Student("Denny", "Lawrence", 1635);
console.log(student1, "Average: "+ student1.average());