This is my first "program" or whatever you want to call it using JavaScript.
I need to modify the avg, total, and grade by using 2 functions: calcAVG and calcGrade. I cannot get the calcAVG function to work correctly. I can't figure out why the function won't modify the avg or total value. In the program, I can't modify the top 5 properties of the object (it's for an assignment).
Also the calcAVG function works when called in the console.log. So do I have some kind of syntax error?
var student = {
fullName : 'Amanda Corbin',
scores : [90,50,88,56,89],
avg : 0,
total : 0,
grade : '',
calcAvg : function(scores,avg,total) {
for (var i = 0; i < this.scores.length; i++) {
this.total += this.scores[i];
}
this.avg = ( this.total / this.scores.length );
return (this.avg);
}
calcGrade : function(avg,grade) {
if (this.avg >=90){
console.log("grade is A");
this.grade = 'A';
} else if (this.avg >= 80 && this.avg <90){
console.log("grade is B");
this.grade = 'B';
} else if (this.avg >= 70 && this.avg <80){
console.log("grade is C");
this.grade = 'C';
} else if (this.avg >= 60 && this.avg <70){
console.log("grade is D");
this.grade = 'D';
} else if (this.avg <60){
console.log("grade is F");
this.grade = 'F';
}
return (this.grade);
}
console.log(student);
console.log(student.calcAvg());
console.log(student.calcGrade());

calcAvgfunction 2. no closing}to closestudent. There's also no need for the function parameters; you're accessing the fields viathis. And you can remove the parens around the values you're returning.