课程: JavaScript Practice: Object-Oriented Programming
Solution: Create a roster with functions - JavaScript教程
课程: JavaScript Practice: Object-Oriented Programming
Solution: Create a roster with functions
- [Instructor] First, let's create our student object. We'll use JavaScript functions for this. Student takes two parameters, name and grades, which is an array of integers from 0 to 100. Next, let's create the getGrades function. We'll use the prototype dot notation on the student function to create this. It will return the students' grades. We can create the checkIsPassing function in the same way. We'll use the calculateGPA function that was provided to us to get the students' GPA. The calculateGPA function takes a list of the students' grades and uses array.reduce to sum the total of all the grades. It then divides that sum by the total number of grades and floors the decimal value. This gives us the GPA. Once we get the GPA, we'll return true if the GPA is greater than 70. Now we're ready to create our CourseRoster object. CourseRoster takes two parameters: roster, which is a list of student objects, and a teacher, which is a string. Let's define the getRoster function on the CourseRoster prototype. It will iterate through each student in the roster using array.forEach and concatenate their names with a comma. Lastly, we can create the returnGraduatingStudents function. We'll filter the array of students in the roster to return only those who are passing. We'll use student.checkIsPassing to check if the student will graduate. If we run our code, we can see all of our tests are passing.