0
0
Typescriptprogramming~10 mins

Declaring functions and classes 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 declare a function named greet that returns a string.

Typescript
function greet() : [1] {
  return "Hello!";
}
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cboolean
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' as return type when the function actually returns a string.
Using 'number' or 'boolean' which do not match the returned value.
2fill in blank
medium

Complete the code to declare a class named Person with a constructor that takes a name parameter.

Typescript
class Person {
  name: string;
  constructor([1]: string) {
    this.name = name;
  }
}
Drag options to blanks, or click blank then click option'
AfullName
Bage
CpersonName
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name that does not match the assignment.
Forgetting to declare the parameter in the constructor.
3fill in blank
hard

Fix the error in the method declaration inside the class Calculator.

Typescript
class Calculator {
  add(a: number, b: number) : [1] {
    return a + b;
  }
}
Drag options to blanks, or click blank then click option'
Avoid
Bstring
Cnumber
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' which means no return value.
Using 'string' or 'boolean' which do not match the returned value.
4fill in blank
hard

Fill both blanks to declare a class Animal with a method makeSound that returns a string.

Typescript
class [1] {
  [2]() : string {
    return "Some sound";
  }
}
Drag options to blanks, or click blank then click option'
AAnimal
BmakeSound
Csound
Dnoise
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class or method names.
Forgetting to specify the return type.
5fill in blank
hard

Fill all three blanks to declare a class Rectangle with a constructor and a method area that returns the area as a number.

Typescript
class [1] {
  width: number;
  height: number;
  constructor([2]: number, [3]: number) {
    this.width = width;
    this.height = height;
  }
  area() : number {
    return this.width * this.height;
  }
}
Drag options to blanks, or click blank then click option'
ARectangle
Bwidth
Cheight
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names that do not match the properties.
Using a class name that does not fit the context.