TypeScript: Summarise marks

Read a list of marks into a typed array and print a short summary.

What to build

A program that prints the count, total, average and highest of a list of marks.

Requirements

  • Store the marks in a number[].
  • Print four lines: count: N, total: T, average: A (two decimal places) and highest: H.

Input

Line 1: n. Line 2: n whole numbers separated by spaces.

Output

Four lines.

Constraints

1 ≤ n ≤ 100; 0 ≤ each mark ≤ 100.

Example

Input:

5
72 45 88 91 60

Output:

count: 5
total: 356
average: 71.20
highest: 91

Why it works

Typing the array as number[] means every method used on it, from reduce to Math.max, is known to work on numbers, and the average is computed from real numbers rather than joined text.

Examples

Example 1 · Input
5
72 45 88 91 60
Expected output
count: 5
total: 356
average: 71.20
highest: 91
Example 2 · Input
1
100
Expected output
count: 1
total: 100
average: 100.00
highest: 100

Hints

Hint 1

Split the second line and map it to numbers to get a number[].

Hint 2

Math.max(...marks) gives the highest mark.

Solution

Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";
const n = Number(read());
const marks: number[] = read().split(" ").map(Number);
const total = marks.reduce((sum, m) => sum + m, 0);
console.log("count: " + n);
console.log("total: " + total);
console.log("average: " + (total / n).toFixed(2));
console.log("highest: " + Math.max(...marks));

Lesson: Basic types: primitives, arrays, tuples and unknown →