Leave a rating/review
Notes: 34. Challenge: Methods
Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.
It’s time for some practice with methods. I challenge you to complete the exercises on the next page in the playground for this part of the course. Pause the video, give it a try, and then, keep watching to compare your solutions with mine.
For this challenge, you had to revisit some code from Programming in Swift: Fundamentals. This time, your challenge was to add a curveGrades method to Classroom. I saw at least three hints that the curveGrades method was going to need to me mutating:
-
The name
curveGradesitself told me that grades were going to change.curv-ed-Gradesmight have returned something, instead. - I saw “add that amount to all students’ scores”. And…
- “sort the students array”:
That would be a new value for students, and so, a new value for the whole Classroom. So, mutating it was!
mutating func curveGrades() {
}
I also changed three lets to vars, based on what I saw was going to need to change. The first was a student’s grade.
41 var grade…
Next, a Classroom’s students…
46 var students
And finally, the classroom instance I’d be working with.
53 var classroom
Then, back in the method, I used a guard statement, to make sure there was a highestGrade, before proceeding any further.
guard let highestGrade = getHighestGrade() else {
return
}
Then, I got something like an outline of the reassignment I’d need to do, going. I mapped the students to exactly what they were, and assigned that back to the students array.
students =
students.map { student in
return student
}
That wasn’t meaningful in itself, but I’ve found that kind of thing to be a good place to start. Within that closure, I’d be reassigning grades, so I got the curve amount.
75 let curveAmount = 100 - highestGrade
Going with the hint for this challenge, I worked with copies of the students. And when you make a mutable copy of a parameter, you don’t even need a new variable name.
76 var student = student
The last step in the calculation was to add the curveAmount to the copied student’s grade.
77 student.grade += curveAmount
And although that resulted in the correct mutated students, I saw that curveAmount was being calculated in the wrong place. It didn’t need to be calculated for every student.
I’ll show you a little trick here: you can use what’s called the “capture list” of a closure to set things up only once. You just put the values you need inside of square brackets, before any parameters. So I cut and pasted accordingly.
74 students.map { [curveAmount = 100 - highestGrade] student in
var student = student
Then, for the sorting, I used the sorted method, with higher grades being sorted before lower ones.
79 .sorted { $0.grade > $1.grade }
At that point, I was ready to curve the grades!
classroom.curveGrades()
And I had a look at the sorted students to check my work.
84 classroom.students