Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ const students: Student[] = [
Example:
getStudentName(students[0]); // => "Alice"
******************************/

function getStudentName(student: Student): string {
return student.name;
}
console.log(getStudentName(students[0]));

/******************************
* ✅ Q2) Create a `getCourse` function:
Expand All @@ -82,7 +85,12 @@ const students: Student[] = [
Example:
getCourse(students[4], 2); // => "Music"
******************************/
function getCourse(student: Student, courseIndex: number): string {
let courseIndexfind = student.courses[courseIndex];
return courseIndexfind;
}

console.log(getCourse(students[4], 2));

/******************************
* ✅ Q3) Create `addCourseToStudent` function:
Expand All @@ -98,6 +106,12 @@ const students: Student[] = [
addCourseToStudent(students[7], "Physics"); // => { id: 8, name: "Helen", courses: ["History", "Art", "PE", "Physics"] }
******************************/

function addCourseToStudent(student: Student, course: string): Student {
student.courses.push(course);
return student;
}

console.log(addCourseToStudent(students[7], "Physics"));

/******************************
* ✅ Q4) Create `countCourses` function:
Expand All @@ -108,6 +122,12 @@ const students: Student[] = [
countCourses(students[1]); // => 4
******************************/

function countCourses(student: Student): number {
return student.courses.length;
}

console.log(`The Student has ${countCourses(students[4])} courses!`);

/******************************
* ✅ Q5) Create a `removeCourseFromStudent` function:
- Accepts:
Expand All @@ -120,6 +140,16 @@ const students: Student[] = [
removeCourseFromStudent(students[6], "Science"); // => { id: 7, name: "Grace", courses: ["Math", "English", "Music"] }
******************************/

function removeCourseFromStudent(student: Student, course: string): Student {
// student.courses = student.courses.filter((n) => {
// if (n !== course) return student.courses;
// }); another way
student.courses = student.courses.filter((n) => n !== course);

return student;
}

console.log(removeCourseFromStudent(students[6], "Science"));

/******************************
* ✅ Q6) Create a `findStudentById` function:
Expand All @@ -133,4 +163,13 @@ const students: Student[] = [
- findStudentById(students, 29); // => undefined
******************************/

function findStudentById(
students: Student[],
studentId: number
): Student | undefined {
let finedstudet = students.find((student) => student.id === studentId);
return finedstudet;
}

console.log(findStudentById(students, 10));
console.log(findStudentById(students, 29));