Last active
January 7, 2021 15:09
-
-
Save nishabe/a64ae19650ff3c9ed16f82af0acd9219 to your computer and use it in GitHub Desktop.
student.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HttpException, Injectable } from '@nestjs/common'; | |
import { ApiService } from '../api/api.service'; | |
export interface Student { | |
name: string; | |
grades: number[]; | |
} | |
@Injectable() | |
export class StudentService { | |
constructor(private apiService: ApiService) {} | |
public async getGpa(firstName: string, lastName: string): Promise<number> { | |
const student: Student = await this.apiService.getStudent( | |
firstName, | |
lastName, | |
); | |
if (!student || !student.grades) { | |
throw new HttpException('Cannot find student or student grades', 404); | |
} | |
let gpa = 0; | |
for (const grade of student.grades) { | |
gpa += grade / student.grades.length; | |
} | |
return gpa; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment