Skip to content
Katabench
Try free

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..100 return "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, otherwise F.

Refactor without changing behavior until every gate passes: one guard, one adjustment expression, one banding helper.

Starter code

C#
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.

C# refactoring exercises →

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