Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a typed array of numbers.
Typescript
let numbers: [1] = [1, 2, 3, 4];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string[] instead of number[] causes type errors.
Using any[] loses the benefit of type safety.
✗ Incorrect
Typed arrays specify the type of elements they hold. Here, number[] means an array of numbers.
2fill in blank
mediumComplete the code to declare a typed array of strings.
Typescript
const fruits: [1] = ['apple', 'banana', 'cherry'];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using number[] for string elements causes errors.
Using any[] removes type checking benefits.
✗ Incorrect
The array holds strings, so the type string[] is correct.
3fill in blank
hardFix the error in the typed array declaration.
Typescript
let flags: [1] = [true, false, true]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string[] or number[] causes type mismatch errors.
Using any[] works but loses type safety.
✗ Incorrect
The array contains boolean values, so the type must be boolean[].
4fill in blank
hardFill both blanks to declare a typed array and add a new element.
Typescript
let scores: [1] = [10, 20, 30]; scores.[2](40);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string[] for numbers causes errors.
Using pop removes elements instead of adding.
✗ Incorrect
The array holds numbers, so number[] is correct. To add an element, use the push method.
5fill in blank
hardFill all three blanks to declare a typed array, add an element, and access an element.
Typescript
let names: [1] = ['Alice', 'Bob']; names.[2]('Carol'); const first: [3] = names[0];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using number[] for string arrays causes errors.
Using pop instead of push removes elements.
Using number type for accessed element causes type mismatch.
✗ Incorrect
The array holds strings, so string[] is correct. Use push to add an element. Accessing an element returns a string.