I need to convert string to float to summarize all numbers and get the average.
I have tried Number(), parseFloat none of them are giving me the expected output.
For example instead of return 2 it is returning ‘11’
I am colleting data from this API: https://www.hatchways.io/api/assessment/students obs(to retrieve the data, I created a service: export class StudentsService {
studentsUrl: string = "https://www.hatchways.io/api/assessment/students";
constructor(private http: HttpClient) {}
//casting observable into Students array
getAllStudents(): Observable<{students: Students[]}> {
//return this.http.get(this.studentsUrl);
return this.http.get<{students: Students[]}>(${this.studentsUrl}
);
}
}
getAVG() {
for(let i = 0; i < this.students.length; i++) {
//console.log('Estudante número: '+ i);
for(let z = 0; z < 8; z++) {
//console.log('Notas index: ' + z);
this.grades[i] += Number(this.students[i].grades[z]);
console.log('nota: '+ this.students[i].grades[z]);
}
var num = parseFloat(this.grades[0]);
console.log('#######sum das notas######: ' + num);
}
}
I need to sum all grades in the array to calculate the average and display it