"use strict"; function round2(x) { return Math.round(x * 100) / 100; } // Standard 4.0-scale grade points (common US university registrar convention). const GRADE_POINTS = { "A": 4.0, "A-": 3.7, "B+": 3.3, "B": 3.0, "B-": 2.7, "C+": 2.3, "C": 2.0, "C-": 1.7, "D+": 1.3, "D": 1.0, "D-": 0.7, "F": 0.0 }; module.exports = { slug: "gpa-weighted", title: "Credit-weighted GPA", deck_category: "other", description: "Computes a credit-weighted grade point average from a list of letter grades and their credit hours on the standard 4.0 scale.", inputs: [ { name: "grades", type: "array", required: true, description: "Letter grades, one per course.", enum: Object.keys(GRADE_POINTS) }, { name: "credits", type: "array", required: true, description: "Credit hours for each course, same length and order as grades." } ], outputs: [ { name: "gpa", description: "Credit-weighted grade point average on a 4.0 scale." } ], steps: [ "Convert each letter grade to grade points using the standard 4.0-scale table: A=4.0, A-=3.7, B+=3.3, B=3.0, B-=2.7, C+=2.3, C=2.0, C-=1.7, D+=1.3, D=1.0, D-=0.7, F=0.0.", "Multiply each course's grade points by its credit hours and sum the results.", "Divide by the sum of all credit hours.", "Round to 2 decimals." ], constants: [ { name: "grade_point_scale_4_0", value: JSON.stringify(GRADE_POINTS), unit: "grade points", source: 0 } ], sources: [ { kind: "url", value: "https://registrar.missouri.edu/grades-calculators/grading-system/", title: "Grading System", publisher: "University of Missouri, Office of the University Registrar", retrieved_utc: "2026-09-30T22:25:22Z", note: "Confirmed table: A+/A 4.00, A- 3.7, B+ 3.3, B 3.00, B- 2.7, C+ 2.3, C 2.00, C- 1.7, D+ 1.3, D 1.00, D- 0.7, F 0; matches the plus/minus values used here exactly (A+ not used by this card)." } ], disclaimer: null, questionText(inputs) { return `What is the GPA for grades ${JSON.stringify(inputs.grades)} with credits ${JSON.stringify(inputs.credits)}?`; }, run(inputs) { const { grades, credits } = inputs || {}; if (!Array.isArray(grades) || !Array.isArray(credits) || grades.length === 0 || grades.length !== credits.length) { throw new Error("grades and credits must be non-empty arrays of the same length"); } let totalPoints = 0; let totalCredits = 0; for (let i = 0; i < grades.length; i++) { const g = grades[i]; const c = credits[i]; if (!(g in GRADE_POINTS)) throw new Error(`unknown grade: ${g}`); if (typeof c !== "number" || c <= 0) throw new Error("each credit value must be a positive number"); totalPoints += GRADE_POINTS[g] * c; totalCredits += c; } const gpa = round2(totalPoints / totalCredits); return { answer: `${gpa.toFixed(2)}`, values: { gpa } }; }, examples: [ { name: "worked example", inputs: { grades: ["A", "B+", "B"], credits: [3, 4, 3] }, expected: { answer: "3.42" }, check: "By hand: 4.0*3=12, 3.3*4=13.2, 3.0*3=9; sum=34.2; total credits=10; gpa=34.2/10=3.42." }, { name: "test vector: mixed grades", inputs: { grades: ["A-", "C", "B-"], credits: [3, 3, 3] }, expected: { answer: "2.80" }, check: "By hand: 3.7*3=11.1, 2.0*3=6, 2.7*3=8.1; sum=25.2; total credits=9; gpa=25.2/9=2.80." } ] };