CodeBlanks Logo Icon CodeBlanks

Summary - Arrays

Write a notes app by combining array types and methods.

Summary - Arrays

In this section you learned to specify array types as number[] and Array<number>, use the push, pop and map methods, define immutable arrays with readonly, and create nested arrays like number[][]. Now you will build a notes app combining all of these.

let notlar: readonly number[][] = [
    [80, 90],
    [70, 85]
];
// notlar.push([100]); // Error: readonly

Practice Notes app

Define an array named notlar of type readonly number[][]: first row [80, 90], second row [70, 85]. Then use map to compute the sum of each row's two grades, store them in a number[] array called toplamlar, and print it.

Starter code

index.ts
// readonly iç içe not dizisi tanımla, her satırın toplamını map ile hesapla ve yazdır

Sample solution

index.ts
let notlar: readonly number[][] = [
    [80, 90],
    [70, 85]
];
let toplamlar: number[] = notlar.map(satir => satir[0] + satir[1]);
console.log(toplamlar);

Expected output

[ 170, 155 ]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is a valid TypeScript array type notation?

  • number{}
  • string()
  • number[] Correct answer
  • Array

Explanation: `number[]` and `Array<number>` are valid array type notations in TypeScript; curly braces or parentheses forms are invalid.

2 Define a readonly string array

Question
let sabit: ____ = ["pzt", "sal"];
Answer
let sabit: readonly string[] = ["pzt", "sal"];

Explanation: `readonly string[]` defines an immutable string array; `string[]` or `Array<string>` does not add readonly protection.

3 Array concepts summary

Term Meaning
number[] Mutable number array
readonly number[] Immutable number array
number[][] Nested number array
Array<string> Alternative notation for a string array

All lessons in Beginner