diff --git a/src/challenge.ts b/src/challenge.ts index fad2280..23dc7af 100644 --- a/src/challenge.ts +++ b/src/challenge.ts @@ -19,8 +19,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)); // replace empty array with what you see is fit } /** @@ -36,8 +35,15 @@ 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 coursesArray: string[] = []; + students.forEach((student) => + student.courses.forEach((course) => { + if (!coursesArray.includes(course)) { + coursesArray.push(course); + } + }) + ); + return coursesArray; // replace empty array with what you see is fit } export { getStudentsByCourse, listAllCourses }; diff --git a/src/students.ts b/src/students.ts index ac9d6c0..8703423 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; // replace empty string with what you see is fit } /** @@ -86,8 +85,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]; // replace empty string with what you see is fit } /** @@ -103,7 +101,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; } @@ -116,8 +114,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 } /** @@ -132,9 +129,10 @@ function countCourses(student: Student): number { * // => { id: 7, name: "Grace", courses: ["Math", "English", "Music"] } */ function removeCourseFromStudent(student: Student, course: string): Student { - // write your code here... - - return student; + return { + ...student, + courses: student.courses.filter((subject) => subject !== course), + }; } /** @@ -155,8 +153,7 @@ function findStudentById( studentId: number ): 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 {