ProgramGuru

Student Marks Manager

Student Marks Manager — purpose and prerequisites

A marks manager collects assessments, calculates totals and averages, and assigns grades. Separate calculation from display so the grading policy can change without affecting record storage. Explicitly handle students with no marks.

What you’ll learn

  • Explain the purpose of student marks manager.
  • Identify the typed contract, runtime responsibilities and common pitfalls.
  • Run the example and extend its behavior with a practical exercise.

How the idea works

The average of 88, 92, and 90 is 90, so the first student receives A. An empty marks array is reported explicitly instead of dividing by zero.

A score may be a number but still be out of the allowed 0–100 range; validate before saving it.

Visualize the flow

Student Marks Manager — learning flowStudent records → Average + grade → Report cardStudent Marks Manager1Student records2Average + grade3Report cardProject core logic is executable; integrations may need setup.
A three-step view of the responsibilities and the observable result.

Run the checked TypeScript example

This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.

TypeScript source

type Student = { name: string; marks: number[] };
function report(student: Student): string {
  if (!student.marks.length) return `${student.name}: no marks`;
  const avg = student.marks.reduce((a, b) => a + b, 0) / student.marks.length;
  const grade = avg >= 90 ? "A" : avg >= 75 ? "B" : avg >= 50 ? "C" : "D";
  return `${student.name}: ${avg.toFixed(1)} (${grade})`;
}
console.log(report({ name: "Maya", marks: [88, 92, 90] }));
console.log(report({ name: "Sam", marks: [] }));

Verified output

Maya: 90.0 (A)
Sam: no marks

Compile: npx tsc --strict --target es2022 --module commonjs student-marks-manager.ts; run: node student-marks-manager.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.

Trace the execution

The average of 88, 92, and 90 is 90, so the first student receives A. An empty marks array is reported explicitly instead of dividing by zero.

Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.

Common mistake and correction

A score may be a number but still be out of the allowed 0–100 range; validate before saving it.

Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.

Your turn: extend the example

Add multiple subjects, rank students by average, and handle tied scores consistently.

Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.

Check your understanding

Which statement correctly describes student marks manager in the example?
Choose one answer. You can try again.

Answer and explanation

The average of 88, 92, and 90 is 90, so the first student receives A.

The average of 88, 92, and 90 is 90, so the first student receives A. An empty marks array is reported explicitly instead of dividing by zero.

Key idea to remember

The average of 88, 92, and 90 is 90, so the first student receives A. An empty marks array is reported explicitly instead of dividing by zero.

Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Contact Management System →