0
0
Typescriptprogramming~30 mins

Declaring functions and classes in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Declaring functions and classes
📖 Scenario: You are building a simple program to manage a small library. You want to keep track of books and be able to describe them with a function.
🎯 Goal: Create a Book class with properties, a function to describe a book, and print the description.
📋 What You'll Learn
Create a Book class with title and author properties
Create a function called describeBook that takes a Book object and returns a string description
Create an instance of Book with exact values
Print the description returned by describeBook
💡 Why This Matters
🌍 Real World
Managing books in a library or bookstore software requires classes to represent books and functions to describe or manipulate them.
💼 Career
Understanding how to declare classes and functions is fundamental for software development jobs, especially when working with object-oriented programming languages like TypeScript.
Progress0 / 4 steps
1
Create the Book class
Create a Book class with a constructor that takes title and author as strings and sets them as public properties.
Typescript
Need a hint?

Use class Book { constructor(public title: string, public author: string) {} } to create the class.

2
Create the describeBook function
Create a function called describeBook that takes a parameter book of type Book and returns a string in the format: "Title: [title], Author: [author]".
Typescript
Need a hint?

Use a function with a template string to return the description.

3
Create a Book instance
Create a variable called myBook and assign it a new Book instance with title set to "The Great Gatsby" and author set to "F. Scott Fitzgerald".
Typescript
Need a hint?

Use const myBook = new Book("The Great Gatsby", "F. Scott Fitzgerald") to create the instance.

4
Print the book description
Use console.log to print the result of calling describeBook with myBook.
Typescript
Need a hint?

Use console.log(describeBook(myBook)) to print the description.