Tame the Grade Calculator
Easy About 15 min Free, no account needed
Letter grades with bonuses, written as a wall of nested ifs.
The brief
Grade(score, attendancePct, extraCredit) awards a letter:
- inputs outside
0..100return"invalid"; - the adjusted score adds 5 for extra credit and 3 for attendance of 90% or better, clamped at 100;
- bands: 90+
A, 80+B, 70+C, 60+D, otherwiseF.
Refactor without changing behavior until every gate passes: one guard, one adjustment expression, one banding helper.
Starter code
using System;
public class Solution
{
// Letter grade with bonuses. Every combination spelled out by hand.
public string Grade(int score, int attendancePct, bool extraCredit)
{
if (score < 0 || score > 100 || attendancePct < 0 || attendancePct > 100)
{
return "invalid";
}
var adjusted = score;
if (extraCredit)
{
if (attendancePct >= 90)
{
adjusted = adjusted + 5;
adjusted = adjusted + 3;
if (adjusted > 100)
{
adjusted = 100;
}
}
else
{
adjusted = adjusted + 5;
if (adjusted > 100)
{
adjusted = 100;
}
}
}
else
{
if (attendancePct >= 90)
{
adjusted = adjusted + 3;
if (adjusted > 100)
{
adjusted = 100;
}
}
}
if (adjusted >= 90)
{
return "A";
}
else
{
if (adjusted >= 80)
{
return "B";
}
else
{
if (adjusted >= 70)
{
return "C";
}
else
{
if (adjusted >= 60)
{
return "D";
}
return "F";
}
}
}
}
}How it is graded
Behavioral tests have to stay green while the grader measures complexity, nesting, method length, and duplication from your source.
Related guides
What it practices
- Guard clauses
- Flatten nesting with early returns so the happy path reads top to bottom.
- Extract method
- Pull named steps and named conditions out of a long method so each piece does one thing.
Related katas
- Flatten the Cursor Decoder Easy Pro A cursor envelope is validated inside a deep branch pyramid. Extract canonical number parsing and flatten the protocol guards.
- Tame the Chess Move Hard Pro Move legality for six pieces packed into one branchy method.
- Tame the Payroll Run Hard Pro Hourly, salaried and contractor pay rules braided into one method.
- Dedupe the Field Validators Easy Free Three fields, three identical validation blocks.