Show List
TypeScript Coding Questions
- Write a TypeScript function to calculate the sum of two numbers.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy code
function filterPositive(arr: number[]): number[] {
return arr.filter((num) => num >= 0);
}
- Write a TypeScript function to remove duplicates from an array of numbers.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy code
function stringToNumber(str: string): number {
return Number(str);
}
- Write a TypeScript function to check if a string contains a substring.
typescriptCopy code
function containsSubstring(str: string, substring: string): boolean {
return str.includes(substring);
}
- Write a TypeScript function to reverse a string.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy code
function stringToCharArray(str: string): string[] {
return str.split("");
}
- Write a TypeScript function to reverse an array of numbers.
typescriptCopy code
function reverseArray(arr: number[]): number[] {
return arr.reverse();
}
- Write a TypeScript function to check if an array of numbers contains a specific number.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy code
function isEven(num: number): boolean {
return num % 2 === 0;
}
- Write a TypeScript function to check if a string is a valid email address.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy 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.
typescriptCopy code
function arrayToMap(arr: any[], key: string): { [key: string]: any } {
return arr.reduce((map, obj) => {
map[obj[key]] = obj;
return map;
}, {});
}
Leave a Comment