0
0
Typescriptprogramming~10 mins

Type annotation on function parameters in Typescript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a type annotation for the parameter name as a string.

Typescript
function greet([1]) {
  return `Hello, ${name}!`;
}
Drag options to blanks, or click blank then click option'
Aname: string
Bstring name
Cname number
Dname = string
Attempts:
3 left
💡 Hint
Common Mistakes
Using space instead of colon between parameter and type.
Putting the type before the parameter name.
Assigning a value instead of annotating the type.
2fill in blank
medium

Complete the code to add a type annotation for the parameter age as a number.

Typescript
function setAge([1]) {
  console.log(`Age is ${age}`);
}
Drag options to blanks, or click blank then click option'
Anumber age
Bage string
Cage = number
Dage: number
Attempts:
3 left
💡 Hint
Common Mistakes
Using space instead of colon.
Putting the type before the parameter name.
Assigning a value instead of annotating the type.
3fill in blank
hard

Fix the error in the function parameter type annotation for isActive which should be a boolean.

Typescript
function setActive([1]) {
  console.log(`Active status: ${isActive}`);
}
Drag options to blanks, or click blank then click option'
AisActive = boolean
BisActive: boolean
Cboolean isActive
DisActive string
Attempts:
3 left
💡 Hint
Common Mistakes
Using space instead of colon.
Putting the type before the parameter name.
Assigning a value instead of annotating the type.
4fill in blank
hard

Fill both blanks to add type annotations for parameters title as string and pages as number.

Typescript
function bookInfo([1], [2]) {
  return `${title} has ${pages} pages.`;
}
Drag options to blanks, or click blank then click option'
Atitle: string
Bpages: string
Cpages: number
Dtitle number
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up types for parameters.
Using space instead of colon.
Putting type before parameter name.
5fill in blank
hard

Fill all three blanks to add type annotations for parameters firstName and lastName as strings, and age as number.

Typescript
function personInfo([1], [2], [3]) {
  return `${firstName} ${lastName} is ${age} years old.`;
}
Drag options to blanks, or click blank then click option'
AfirstName: string
BlastName: string
Cage: number
Dage string
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong types for parameters.
Forgetting colon between parameter and type.
Putting type before parameter name.