In my Angular 4 project, I am trying to calculate the average of some numbers. Here is a simplified version of my component:
@Component({
selector: 'app-home',
templateUrl: './home.component.html'
})
export class HomeComponent {
students:string[];
constructor(private studentsService: StudentsService) {}
ngOnInit() {
this.studentsService.getStudents().subscribe(students => {
...
})
function calculateAverage(arr) {
var i,
len=arr.length,
average=0;
for (i=0;i<len;i++) {
average += arr[i];
}
return (average/arr.length);
}
}
}
The template in my HTML file looks like this (GPARecord is an array of floats):
<tr *ngFor="let student of students">
...
<td>{{ calculateAverage(student.GPARecord) }}</td>
</tr>
However, I am encountering an error in the console:
ERROR TypeError: _co.calculateAverage is not a function
I would appreciate any help in resolving this issue. Thank you!