21. Write a program to read and parse a JSON object and display specific properties.
Required Input:
{ "name": "Alice", "age": 25, "city": "New York" }
Expected Output:
Name: Alice
Age: 25
Code In Typescript
const jsonString: string = '{"name": "Alice", "age": 25, "city": "New York"}';
function displayJsonProperties(jsonStr: string): void {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
22. Create a function to merge two objects. If a property exists in both, the second object's value overrides the first.
Required Input:
Object 1: { a: 1, b: 2, c: 3 }
Object 2: { b: 4, d: 5 }
Expected Output:
{ a: 1, b: 4, c: 3, d: 5 }
Code In Typescript
// Predefined structure
function mergeObjects(obj1: any, obj2: any): any {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
23. Write a TypeScript class to represent a simple bank account with deposit, withdraw, and check balance methods.
Required Input:
Initial Balance: 1000
Operations: deposit(500), withdraw(200), checkBalance()
Expected Output:
Balance: 1300
Code In Typescript
// Predefined structure
class BankAccount {
private balance: number;
constructor(initialBalance: number) {
// Your logic here
}
deposit(amount: number): void {
// Your logic here
}
withdraw(amount: number): void {
// Your logic here
}
checkBalance(): number {
// Your logic here
}
}
Run Code?
Click Run Button to view compiled output
24. Create a function that returns the first non-repeating character in a string.
Required Input:
"swiss"
Expected Output:
First non-repeating character: w
Code In Typescript
// Predefined structure
function firstNonRepeatingChar(str: string): string
Run Code?
Click Run Button to view compiled output
25. Write a function to find the factorial of a number using recursion.
Required Input:
5
Expected Output:
Factorial: 120
Code In Typescript
// Predefined structure
function factorial(n: number): number {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
26. Implement a function to check if a string is an anagram of another string.
Required Input:
String 1: "listen"
String 2: "silent"
Expected Output:
Is anagram: true
Code In Typescript
// Predefined structure
function isAnagram(str1: string, str2: string): boolean {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
27. Write a program to calculate the sum of numbers in a 2D matrix.
Required Input:
[ [1, 2, 3],
[4, 5, 6],
[7, 8, 9] ]
Expected Output:
Sum: 45
Code In Typescript
// Predefined structure
function sumMatrix(matrix: number[][]): number {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
28. Write a program to group array elements based on their length.
Required Input:
["apple", "cat", "dog", "banana", "bat"]
Expected Output:
{ '3': [ 'cat', 'dog', 'bat' ], '5': [ 'apple' ], '6': [ 'banana' ] }
Code In Typescript
// Predefined structure
function groupByLength(arr: string[]): { [key: number]: string[] } {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
29. Write a program that implements the "debounce" function to control how often a function is executed.
Required Input:
Simulate a button click every 500ms, with a debounce time of 2000ms.
Expected Output:
Button clicked!
Code In Typescript
// Predefined structure
function debounce(func: () => void, delay: number): () => void {
// Your logic here
}
// Simulate calls
Run Code?
Click Run Button to view compiled output