Comprehensive JavaScript and TypeScript Interview Questions
1. Array Methods
a. Sort an array of numbers:
```javascript
const arr = [3, 1, 4, 1, 5, 9];
[Link]((a, b) => a - b);
[Link](arr); // [1, 1, 3, 4, 5, 9]
```
b. Flatten a nested array:
```javascript
const nestedArr = [1, [2, [3, 4]], 5];
const flatArr = [Link](2);
[Link](flatArr); // [1, 2, 3, 4, 5]
```
c. Remove falsy values from an array:
```javascript
const mixedArr = [0, "hello", false, 42, undefined, null];
const truthyArr = [Link](Boolean);
[Link](truthyArr); // ["hello", 42]
```
d. Find the frequency of elements in an array:
```javascript
const nums = [1, 2, 2, 3, 3, 3];
const frequency = [Link]((acc, num) => {
acc[num] = (acc[num] || 0) + 1;
return acc;
}, {});
[Link](frequency); // {1: 1, 2: 2, 3: 3}
```
2. String Operations
a. Convert a string to title case:
```javascript
const titleCase = str => [Link]().replace(/\w/g, char => [Link]());
[Link](titleCase("javascript is fun")); // "Javascript Is Fun"
```
b. Find the longest word in a string:
```javascript
const longestWord = str => [Link](' ').reduce((longest, word) => [Link] > [Link] ?
word : longest, "");
[Link](longestWord("Find the longest word in this string")); // "longest"
```
c. Replace all vowels in a string:
```javascript
const replaceVowels = str => [Link](/[aeiou]/gi, '*');
[Link](replaceVowels("javascript")); // "j*v*scr*pt"
```
3. Numbers and Math
a. Generate a random integer within a range:
```javascript
const randomInt = (min, max) => [Link]([Link]() * (max - min + 1)) + min;
[Link](randomInt(1, 10)); // random integer between 1 and 10
```
b. Find the factorial of a number:
```javascript
const factorial = n => (n === 0 ? 1 : n * factorial(n - 1));
[Link](factorial(5)); // 120
```
c. Sum of digits in a number:
```javascript
const sumOfDigits = num => String(num).split('').reduce((sum, digit) => sum + Number(digit), 0);
[Link](sumOfDigits(1234)); // 10
```
4. Objects and Classes
a. Deep cloning an object:
```javascript
const obj = { a: 1, b: { c: 2 } };
const deepClone = [Link]([Link](obj));
[Link](deepClone); // { a: 1, b: { c: 2 } }
```
b. Merge two objects:
```javascript
const obj1 = { name: "John" };
const obj2 = { age: 30 };
const mergedObj = { ...obj1, ...obj2 };
[Link](mergedObj); // { name: "John", age: 30 }
```
c. Create private properties in a class:
```javascript
class Person {
#privateName;
constructor(name) {
this.#privateName = name;
getName() {
return this.#privateName;
const person = new Person("John");
[Link]([Link]()); // "John"
```
5. Promises and Async/Await
a. Handle multiple asynchronous operations with [Link]:
```javascript
const promise1 = [Link](3);
const promise2 = new Promise((resolve, reject) => setTimeout(resolve, 100, "foo"));
[Link]([promise1, promise2]).then(values => [Link](values));
// [3, "foo"]
```
b. Using async/await:
```javascript
async function fetchData() {
const data = await fetch("[Link]
return [Link]();
```
6. TypeScript-Specific Features
a. Working with enums:
```typescript
enum Direction {
Up,
Down,
Left,
Right
[Link]([Link]); // 0
```
b. Using type guards:
```typescript
function isString(value: unknown): value is string {
return typeof value === 'string';
[Link](isString("hello")); // true
```
c. Function overloading:
```typescript
function add(a: number, b: number): number;
function add(a: string, b: string): string;
function add(a: any, b: any): any {
return a + b;
[Link](add(1, 2)); // 3
[Link](add("Hello ", "World")); // "Hello World"
```