Thuta Learning
ExercisesProgrammingintermediate

Practice Exercises - Set 1 (Basics)

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Work through Practice Exercises - Set 1 (Basics) yourself
  • Practice and lock in skills you've already learned
  • Get comfortable finding bugs, fixing them, and checking your own work

Let's think about this for a second

This lesson gives you practice tasks to directly write out your own code applying what you know about basic types, functions, and interfaces from the tutorial's Basic chapter. Rather than teaching new concepts, the goal is to cement the syntax you've learned in earlier lessons into memory. For each task, open a TypeScript file yourself and write it out, and if a compiler error pops up, read the error message carefully and think through why it happened. Practicing type annotations with your own hands is the best way to learn.

Exercises

Task 1: Write a function celsiusToFahrenheit(celsius: number): number implementing the formula (celsius * 9/5) + 32. Task 2: Create an interface Book with the fields title: string, author: string, year: number, isAvailable: boolean, and create an array containing three Book objects. Task 3: Write a function getAvailableBooks(books: Book[]): string[] that combines filter() and map() to return, as an array, just the titles of the books where isAvailable is true.

Sample code

typescript
// Task 1
function celsiusToFahrenheit(celsius: number): number {
  // TODO: implement
  return 0;
}

// Task 2
interface Book {
  title: string;
  author: string;
  year: number;
  isAvailable: boolean;
}

const books: Book[] = [
  // TODO: add 3 book objects
];

// Task 3
function getAvailableBooks(books: Book[]): string[] {
  // TODO: implement using filter() + map()
  return [];
}

console.log(celsiusToFahrenheit(100));
console.log(getAvailableBooks(books));
You should see
celsiusToFahrenheit(100) returns 212, and getAvailableBooks(books) returns an array of the titles of books where isAvailable is true.

5-minute try it yourself

After finishing Task 3, write one more function, getBooksByAuthor(books: Book[], author: string): Book[], and try filtering books by author name (try it yourself within 5 minutes).

A quick word of caution

Only remove TODO placeholder values (return 0; return [];) after you've actually filled them in — leaving them empty can trigger compiler errors.

Easy traps

  • Leaving off a function's return type annotation without noticing it becomes an implicit any
  • Not realizing you need to call map() after filter(), and assuming filter() alone can give you the array of title strings directly

Now try it yourself

After finishing Task 3, write one more function, getBooksByAuthor(books: Book[], author: string): Book[], and try filtering books by author name (try it yourself within 5 minutes).

You'll know it worked when: celsiusToFahrenheit(100) returns 212, and getAvailableBooks(books) returns an array of the titles of books where isAvailable is true.

Practice Exercises - Set 1 (Basics) | Thuta Learning