TypeScript Tutorial

TypeScript Project: Student Manager

Store students in a typed array, print the roster, and find the top score.

What you will build

A student manager keeps a roster in memory: each person has a name and a score. You print every row, then walk the same list once more to find the highest score and the student who earned it. The container is a typed array. The row type is an interface.

Names and scores are hardcoded so the program runs without readLine(). Open/typescript/try with Try it in TypeScript. That is the TypeScript editor.

A Student interface

An interface names the fields that belong together. name is a string.score is a number. One object is one student. The roster will hold several objects.

Example

interface Student {
  name: string;
  score: number;
}

const a: Student = {
  name: "Ada",
  score: 91,
};

console.log(a.name + " " + a.score);

Output is Ada 91. The two values travel as one record instead of two parallel variables.

A typed array of students

The type is Student[]. push adds one student at the end. length is how many rows you have. A for...of loop visits each object in order.

Example

interface Student {
  name: string;
  score: number;
}

const roster: Student[] = [];
roster.push({ name: "Ada", score: 91 });
roster.push({ name: "Lin", score: 84 });
roster.push({ name: "Omar", score: 97 });

console.log("Count: " + roster.length);
for (const s of roster) {
  console.log(s.name + " " + s.score);
}

Run this at /typescript/try with Try it in TypeScript. Add a fourth student withpush and compile again. The loop picks up the new row without a new index variable.

Print a roster

A dedicated print function keeps the top of the file short. Pass the array so the function can read every field. Print a header first so the columns are obvious.

Example

interface Student {
  name: string;
  score: number;
}

function printRoster(roster: Student[]): void {
  console.log("Name   Score");
  for (const s of roster) {
    console.log(s.name + "   " + s.score);
  }
}

const roster: Student[] = [];
roster.push({ name: "Ada", score: 91 });
roster.push({ name: "Lin", score: 84 });
roster.push({ name: "Omar", score: 97 });
printRoster(roster);

Complete program: top score

Start from the first student as the current best. Walk the rest of the array. When a score is higher, keep that student instead. If the roster is empty, print a message and skip the search. Ties keep the first name that reached that score.

Example

interface Student {
  name: string;
  score: number;
}

function printRoster(roster: Student[]): void {
  console.log("Name   Score");
  for (const s of roster) {
    console.log(s.name + "   " + s.score);
  }
}

function printTop(roster: Student[]): void {
  if (roster.length === 0) {
    console.log("No students");
    return;
  }
  let top: Student = roster[0];
  for (let i = 1; i < roster.length; i++) {
    if (roster[i].score > top.score) {
      top = roster[i];
    }
  }
  console.log("Top: " + top.name + " with " + top.score);
}

const roster: Student[] = [];
roster.push({ name: "Ada", score: 91 });
roster.push({ name: "Lin", score: 84 });
roster.push({ name: "Omar", score: 97 });
roster.push({ name: "Nia", score: 88 });
printRoster(roster);
printTop(roster);

Omar has 97, the highest value in this demo. Change Lin to 99 and the top line should name Lin. The roster print stays in the same order you pushed.

Common mistakes

  • Parallel arrays, one of names and one of scores. An interface keeps the pair aligned. If you delete a name from one list and forget the matching score, the rows no longer match.
  • Using any[] for the roster. Then tsc will not catch a missing score field. AnnotateStudent[].
  • Reading roster[0] when the array is empty. Check length === 0 first.
  • Using >= in the top-score loop when you want the first winner of a tie. >keeps the earlier name.

Practice

  1. Change Omar’s score from 97 to 90 and confirm Ada becomes the top student.
  2. Add a fifth student with push and confirm the roster and the top-score line both update.
  3. Print the lowest score as well, with the student name, and compute the class average as a number.

Next project: an Account class that refuses a withdrawal when the balance would go negative.

FAQ: TypeScript Project: Student Manager

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript students project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript students project in this TypeScript TypeScript lesson (TypeScript Project: Student Manager).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.