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 missingscorefield. AnnotateStudent[]. - Reading
roster[0]when the array is empty. Checklength === 0first. - Using
>=in the top-score loop when you want the first winner of a tie.>keeps the earlier name.
Practice
- Change Omar’s score from 97 to 90 and confirm Ada becomes the top student.
- Add a fifth student with
pushand confirm the roster and the top-score line both update. - 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.