diff --git a/src/challenge.ts b/src/challenge.ts index fad2280..a92d0fb 100644 --- a/src/challenge.ts +++ b/src/challenge.ts @@ -20,7 +20,7 @@ import { Student } from "./students"; function getStudentsByCourse(students: Student[], course: string): Student[] { // write your code here... - return []; // replace empty array with what you see is fit + return students.filter((student) => student.courses.includes(course)); } /** @@ -36,8 +36,8 @@ function getStudentsByCourse(students: Student[], course: string): Student[] { */ function listAllCourses(students: Student[]): string[] { // write your code here... - - return []; // replace empty array with what you see is fit + const allCourses = students.flatMap((student) => student.courses); + return [...new Set(allCourses)]; } export { getStudentsByCourse, listAllCourses }; diff --git a/src/students.ts b/src/students.ts index ac9d6c0..2e73376 100644 --- a/src/students.ts +++ b/src/students.ts @@ -71,8 +71,7 @@ const students: Student[] = [ */ function getStudentName(student: Student): string { // write your code here... - - return ""; // replace empty string with what you see is fit + return student.name; } /** @@ -87,7 +86,7 @@ function getStudentName(student: Student): string { function getCourse(student: Student, courseIndex: number): string { // write your code here... - return ""; // replace empty string with what you see is fit + return student.courses[courseIndex]; } /** @@ -103,7 +102,7 @@ function getCourse(student: Student, courseIndex: number): string { */ function addCourseToStudent(student: Student, course: string): Student { // write your code here... - + student.courses.push(course); return student; } @@ -117,7 +116,7 @@ function addCourseToStudent(student: Student, course: string): Student { function countCourses(student: Student): number { // write your code here... - return -1; // replace -1 with what you see is fit + return student.courses.length; // replace -1 with what you see is fit } /** @@ -133,7 +132,10 @@ function countCourses(student: Student): number { */ function removeCourseFromStudent(student: Student, course: string): Student { // write your code here... - + const courseIndex = student.courses.indexOf(course); + if (courseIndex !== -1) { + student.courses.splice(courseIndex, 1); + } return student; } @@ -156,7 +158,7 @@ function findStudentById( ): Student | undefined { // write your code here... - return undefined; // replace undefined with what you see is fit + return students.find((student) => student.id == studentId); // replace undefined with what you see is fit } export {