Course of Raku / Addendum 🆕 / Flow and functions / Control flow and logic / Exercises / Letter grades
Solution: Letter grades
Here is a possible solution to the task.
Code
for 95, 82, 71, 58 -> $score {
my $grade = do given $score {
when * >= 90 { 'A' }
when * >= 80 { 'B' }
when * >= 70 { 'C' }
when * >= 60 { 'D' }
default { 'F' }
}
say "$score -> $grade";
}🦋 You can find the source code in the file letter-grades.raku.
Output
95 -> A
82 -> B
71 -> C
58 -> FComments
do giventurns the wholegiven/wheninto an expression whose value is the matching block — here a grade letter — which is stored in$grade.Each
when * >= Nis a Whatever comparison against the topic. The first one that succeeds wins, so the boundaries only need a lower bound each.
Course navigation
← Letter grades | Sign and parity →