Search⌘ K

Challenge 2: Letter Grade to GPA

Explore how to create a Perl subroutine named gpaPoint that translates letter grades into their corresponding GPA values. Understand how to use conditionals to map grades like A+, B-, and F to decimal GPA points, and handle invalid inputs gracefully. This lesson enhances your skills in writing concise, functional Perl code for real-world text processing tasks.

Problem statement

There are many school systems that give standardized GPA points instead of letter grades and vice versa. To bring all these institutions on the same grade scale, we have hired you! Your job is to take a letter grade and change it to the required GPA point.

Write a method called gpaPoint that takes in a string value and returns the correct decimal GPA point, with respect to the scale given below:

  • A+: 4
  • A: 4
  • A-: 3.7
  • B+: 3.3
  • B: 3
  • B-: 2.8
  • C+: 2.5
  • C: 2
  • C-: 1.8
  • D: 1.5
  • F: 0

If any other grade is given, simply return a -1.

Hint: Revise conditionals and see what helps keep your code short and brief.

Input

A string grade.

Output

A floating-point value corresponding to the letter grade.

Sample input

B-

Sample output

2.8

Coding exercise

Write your code in the code widget below. If you don’t get it right, don’t worry; the solution is also given.

Perl
sub gpaPoint{
# Write your code here
return -9;
}