Skip to content

Instantly share code, notes, and snippets.

@nishabe
Last active January 7, 2021 15:09
Show Gist options
  • Save nishabe/a64ae19650ff3c9ed16f82af0acd9219 to your computer and use it in GitHub Desktop.
Save nishabe/a64ae19650ff3c9ed16f82af0acd9219 to your computer and use it in GitHub Desktop.
student.service.ts
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