Solution Review: Letter Grade to GPA
In this lesson, we'll look at the explanation of the last challenge.
We'll cover the following
Let’s look at the solution first before jumping into the explanation:
sub gpaPoint{$grade = @_[0];if($grade eq "A+"){return 4;}elsif($grade eq "A"){ return 4;}elsif($grade eq "A-"){return 3.7;}elsif($grade eq "B+"){ return 3.3;}elsif($grade eq "B"){ return 3;}elsif($grade eq "B-"){ return 2.8;}elsif($grade eq "C+"){ return 2.5;}elsif($grade eq "C"){ return 2;}elsif($grade eq "C-"){ return 1.8;}elsif($grade eq "D"){ return 1.5;}elsif($grade eq "F"){ return 0;}else{return -1;}}print gpaPoint("B+"); # change value to check for multiple grades
Explanation
We can get the passed parameter by using @_[0]
and by using if elsif else
statement, we can compare the passed parameter and return the GPA
accordingly.