30 TypeScript Basic Exercises for Intermediate with Solutions

Master intermediate TypeScript skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in TypeScript, setting a solid foundation for advanced challenges. Start your journey to TypeScript mastery today!

Learning Objectives:

Enhance your TypeScript skills by working with advanced types, generics, enums, and modules. Strengthen your problem-solving ability through real-world coding scenarios and structured logic building.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Write a TypeScript function that takes a string and returns the frequency count of each character.

Required Input:

"typescript"

Expected Output:

{ t: 2, y: 1, p: 2, e: 1, s: 1, c: 1, r: 1, i: 1 }

Code In Typescript

function getFrequencyCount(str: string): { [key: string]: number } { // Your logic here }

Run Code?

Click Run Button to view compiled output

2. Create a generic function to reverse an array of any type.

Required Input:

[1, 2, 3, 4, 5]

Expected Output:

[ 5, 4, 3, 2, 1 ]

Code In Typescript

function reverseArray<T>(arr: T[]): T[] { // Your logic here }

Run Code?

Click Run Button to view compiled output

3. Write a program to implement a class Stack with push, pop, and peek operations.

Required Input:

Perform push(10), push(20), pop(), and peek().

Expected Output:

Top of the stack: 10

Code In Typescript

class Stack<T> { private items: T[] = []; push(item: T): void { // Your logic here } pop(): T

Run Code?

Click Run Button to view compiled output

4. Create an interface for a Book with properties title, author, and year. Implement a function to display book details.

Required Input:

{ title: "TypeScript Basics", author: "John Doe", year: 2024 }

Expected Output:

Title: TypeScript Basics, Author: John Doe, Year: 2024

Code In Typescript

interface Book { title: string; author: string; year: number; } function displayBookDetails(book: Book): void { // Your logic here }

Run Code?

Click Run Button to view compiled output

5. Write a function to remove all duplicates from an array of numbers.

Required Input:

[1, 2, 2, 3, 4, 4, 5]

Expected Output:

[ 1, 2, 3, 4, 5 ]

Code In Typescript

function removeDuplicates(arr: number[]): number[] { // Your logic here }

Run Code?

Click Run Button to view compiled output

6. Write a program to find the longest word in a given string.

Required Input:

"TypeScript is a powerful programming language"

Expected Output:

Longest word: programming

Code In Typescript

function findLongestWord(sentence: string): string { // Your logic here }

Run Code?

Click Run Button to view compiled output

7. Implement a Queue class with methods enqueue, dequeue, and isEmpty.

Required Input:

Perform enqueue(10), enqueue(20), dequeue(), and check if the queue is empty.

Expected Output:

Dequeued: 10
Is Queue Empty: false

Code In Typescript

class Queue<T> { private items: T[] = []; enqueue(item: T): void { // Your logic here } dequeue(): T

Run Code?

Click Run Button to view compiled output

8. Write a function that flattens a nested array (e.g., [1, [2, 3, [4]], 5] → [1, 2, 3, 4, 5]).

Required Input:

[1, [2, 3, [4]], 5]

Expected Output:

[ 1, 2, 3, 4, 5 ]

Code In Typescript

function flattenArray(arr: any[]): any[] { // Your logic here }

Run Code?

Click Run Button to view compiled output

9. Create a function that validates a given email address using regular expressions.

Required Input:

Expected Output:

Valid Email: true

Code In Typescript

function validateEmail(email: string): boolean { // Your logic here }

Run Code?

Click Run Button to view compiled output

10. Write a program to count the number of words in a string.

Required Input:

"TypeScript is fun to learn"

Expected Output:

Word count: 5

Code In Typescript

function countWords(sentence: string): number { // Your logic here }

Run Code?

Click Run Button to view compiled output

ad vertical

1 of 3