CHALLENGE 1: Generic Array Reverse =================================== SOLUTION: function reverse(arr: T[]): T[] { return arr.reverse(); } // Or, without using the built-in method: function reverse(arr: T[]): T[] { const result: T[] = []; for (let i = arr.length - 1; i >= 0; i--) { result.push(arr[i]); } return result; } // Testing with different types: // Strings const stringArr = ["hello", "world", "foo"]; const reversedStrings = reverse(stringArr); console.log(reversedStrings); // ["foo", "world", "hello"] // TypeScript knows reversedStrings is string[] // Numbers const numberArr = [1, 2, 3, 4, 5]; const reversedNumbers = reverse(numberArr); console.log(reversedNumbers); // [5, 4, 3, 2, 1] // TypeScript knows reversedNumbers is number[] // Objects interface User { id: number; name: string; } const users: User[] = [ { id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Charlie" } ]; const reversedUsers = reverse(users); console.log(reversedUsers); // [Charlie, Bob, Alice] // TypeScript knows reversedUsers is User[] EXPLANATION: - "function reverse(arr: T[]): T[]" — accepts an array of any type, returns same type - The acts as a placeholder that TypeScript fills in based on the argument - When you call reverse(["a", "b"]), T becomes 'string' - When you call reverse([1, 2, 3]), T becomes 'number' - The return type is always the same as the input type WHY THIS MATTERS: ✅ Works with ANY type (strings, numbers, objects, etc.) ✅ Type-safe: TypeScript knows what type is being returned ✅ No duplicated code: one function for all types ✅ Better IDE support: autocompletion knows the return type ALTERNATIVE: Using the spread operator function reverse(arr: T[]): T[] { return [...arr].reverse(); } This creates a copy before reversing, leaving the original unchanged.