Show List

TypeScript Coding Questions


  • Write a TypeScript function to calculate the sum of two numbers.
typescript
Copy code
function add(a: number, b: number): number { return a + b; }
  • Write a TypeScript function to find the maximum number in an array of numbers.
typescript
Copy code
function findMax(arr: number[]): number { let max = arr[0]; for (let i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } return max; }
  • Write a TypeScript class that represents a rectangle with a given width and height.
typescript
Copy code
class Rectangle { private width: number; private height: number; constructor(width: number, height: number) { this.width = width; this.height = height; } getArea(): number { return this.width * this.height; } }
  • Write a TypeScript function to capitalize the first letter of a string.
typescript
Copy code
function capitalize(str: string): string { return str.charAt(0).toUpperCase() + str.slice(1); }
  • Write a TypeScript function to filter an array of numbers to remove any negative numbers.
typescript
Copy code
function filterPositive(arr: number[]): number[] { return arr.filter((num) => num >= 0); }
  • Write a TypeScript function to remove duplicates from an array of numbers.
typescript
Copy code
function removeDuplicates(arr: number[]): number[] { return Array.from(new Set(arr)); }
  • Write a TypeScript function to find the second largest number in an array of numbers.
typescript
Copy code
function findSecondLargest(arr: number[]): number { arr.sort((a, b) => b - a); return arr[1]; }
  • Write a TypeScript function to calculate the factorial of a number.
typescript
Copy code
function factorial(num: number): number { if (num <= 1) { return 1; } return num * factorial(num - 1); }
  • Write a TypeScript function to convert a string to a number.
typescript
Copy code
function stringToNumber(str: string): number { return Number(str); }
  • Write a TypeScript function to check if a string contains a substring.
typescript
Copy code
function containsSubstring(str: string, substring: string): boolean { return str.includes(substring); }
  • Write a TypeScript function to reverse a string.
typescript
Copy code
function reverseString(str: string): string { return str.split("").reverse().join(""); }
  • Write a TypeScript function to check if an array of numbers is sorted in ascending order.
typescript
Copy code
function isSortedAscending(arr: number[]): boolean { for (let i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) { return false; } } return true; }
  • Write a TypeScript function to check if a number is prime.
typescript
Copy code
function isPrime(num: number): boolean { if (num <= 1) { return false; } for (let i = 2; i <= Math.sqrt(num); i++) { if (num % i === 0) { return false; } } return true; }
  • Write a TypeScript function to find the index of the first occurrence of a number in an array of numbers.
typescript
Copy code
function findIndexOf(arr: number[], num: number): number { return arr.indexOf(num); }
  • Write a TypeScript function to find the average of an array of numbers.
typescript
Copy code
function findAverage(arr: number[]): number { const sum = arr.reduce((total, num) => total + num, 0); return sum / arr.length; }
  • Write a TypeScript function to convert a string to an array of characters.
typescript
Copy code
function stringToCharArray(str: string): string[] { return str.split(""); }
  • Write a TypeScript function to reverse an array of numbers.
typescript
Copy code
function reverseArray(arr: number[]): number[] { return arr.reverse(); }
  • Write a TypeScript function to check if an array of numbers contains a specific number.
typescript
Copy code
function containsNumber(arr: number[], num: number): boolean { return arr.includes(num); }

  • Write a TypeScript function to convert an array of strings to a single string, separated by a given delimiter.
typescript
Copy code
function joinStrings(arr: string[], delimiter: string): string { return arr.join(delimiter); }
  • Write a TypeScript function to calculate the sum of an array of numbers using recursion.
typescript
Copy code
function sum(arr: number[]): number { if (arr.length === 0) { return 0; } const [first, ...rest] = arr; return first + sum(rest); }
  • Write a TypeScript function to find the intersection of two arrays of numbers.
typescript
Copy code
function findIntersection(arr1: number[], arr2: number[]): number[] { return arr1.filter((num) => arr2.includes(num)); }
  • Write a TypeScript function to check if a number is even.
typescript
Copy code
function isEven(num: number): boolean { return num % 2 === 0; }
  • Write a TypeScript function to check if a string is a valid email address.
typescript
Copy code
function isValidEmail(email: string): boolean { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }
  • Write a TypeScript function to check if an array of numbers is sorted in descending order.
typescript
Copy code
function isSortedDescending(arr: number[]): boolean { for (let i = 0; i < arr.length - 1; i++) { if (arr[i] < arr[i + 1]) { return false; } } return true; }
  • Write a TypeScript function to find the difference between two arrays of numbers.
typescript
Copy code
function findDifference(arr1: number[], arr2: number[]): number[] { return arr1.filter((num) => !arr2.includes(num)); }
  • Write a TypeScript function to check if a string is a valid URL.
typescript
Copy code
function isValidUrl(url: string): boolean { const urlRegex = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/; return urlRegex.test(url); }
  • Write a TypeScript function to calculate the factorial of a number using recursion.
typescript
Copy code
function factorialRecursive(num: number): number { if (num <= 1) { return 1; } return num * factorialRecursive(num - 1); }
  • Write a TypeScript function to remove an item from an array by index.
typescript
Copy code
function removeByIndex(arr: any[], index: number): any[] { return [...arr.slice(0, index), ...arr.slice(index + 1)]; }
  • Write a TypeScript function to convert an array of objects to a single object, using a specified property as the key.
typescript
Copy code
function arrayToMap(arr: any[], key: string): { [key: string]: any } { return arr.reduce((map, obj) => { map[obj[key]] = obj; return map; }, {}); }

    Leave a Comment


  • captcha text