-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathobjectIteration.js
121 lines (112 loc) · 3.4 KB
/
objectIteration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const students = [
{
id: 1,
name: "Alice",
courses: ["Math", "Science", "History"],
},
{
id: 2,
name: "Bob",
courses: ["History", "English", "Math", "Art"],
},
{
id: 3,
name: "Charlie",
courses: ["Science", "English", "Music"],
},
{
id: 4,
name: "David",
courses: ["Math", "History", "Art", "PE"],
},
{
id: 5,
name: "Eva",
courses: ["Science", "Math", "Music"],
},
{
id: 6,
name: "Frank",
courses: ["English", "Art"],
},
{
id: 7,
name: "Grace",
courses: ["Math", "Science", "English", "Music"],
},
{
id: 8,
name: "Helen",
courses: ["History", "Art", "PE"],
},
{
id: 9,
name: "Ivy",
courses: ["Science", "English", "Art"],
},
{
id: 10,
name: "Jack",
courses: ["Math", "History", "Music"],
},
];
// 1) Write a `getStudentName` function that accepts an argument of `student` object, return the student name
function getStudentName(student) {
// write your code here...
}
// console.log(getStudentName(students[0]))
// 2) Write a `getCourse` function that accepts a `student` object and `courseIndex` return the course at the specified course index in the student's courses array
function getCourse(student, courseIndex) {
// write your code here...
}
// console.log(getCourse(students[4], 2)); // Outputs: Music
// 3) Write a `addCourseToStudent` function that accepts a `student` object and `course` string,
// it will add the course to the student's courses array and return the `student` object
function addCourseToStudent(student, course) {
// write your code here...
}
// console.log(addCourseToStudent(students[7], "Physics"));
// 4) Write a `countCourses` function that accepts a `student` object
// then returns the number of courses the student is enrolled in
function countCourses(student) {
// write your code here...
}
// console.log(countCourses(students[1])); // Outputs: 4
// 5) Write a `removeCourseFromStudent` function that accepts a `student` object and `course` string,
// removes the `course` from the student's courses array,
// then returns the `student` object.
function removeCourseFromStudent(student, course) {
// write your code here...
}
// console.log(removeCourseFromStudent(students[6], "Science"));
// 6) Write a `findStudentById` function that accepts a `studentId` and an array of student objects `students`
// and returns the student object with the matching id.
// It should return undefinded if a student is not found
function findStudentById(students, studentId) {
// write your code here...
}
// console.log(findStudentById(students, 10));
// 🌶️🌶️
// 7) Write a `getStudentsByCourse` function that accepts a `course` string and an array of student objects `students`,
// then returns an array of student objects who are enrolled in the specified course
function getStudentsByCourse(students, course) {
// write your code here...
}
// console.log(getStudentsByCourse(students, "Music"));
// 🌶️🌶️🌶️
// 8) Write a `listAllCourses` function that accepts an array of `students`
// then returns an array of all unique courses names across all students
function listAllCourses(students) {
// write your code here...
}
// console.log(listAllCourses(students));
module.exports = {
getStudentName,
getCourse,
addCourseToStudent,
countCourses,
listAllCourses,
removeCourseFromStudent,
findStudentById,
getStudentsByCourse,
};