Description:
Create a function that takes a string
and an optional boolean
.
- If the boolean is
true
or not provided, return the string in uppercase. - If the boolean is
false
, return the string in lowercase.
Function Signature:
function formatString(input: string, toUpper?: boolean): string
Example:
formatString("Hello"); // Output: "HELLO"
formatString("Hello", true); // Output: "HELLO"
formatString("Hello", false); // Output: "hello"
Description:
Create a function that filters an array of objects by the rating
property, returning only items with a rating of 4 or more.
Function Signature:
function filterByRating(items: { title: string; rating: number }[]): { title: string; rating: number }[]
Example:
const books = [
{ title: "Book A", rating: 4.5 },
{ title: "Book B", rating: 3.2 },
{ title: "Book C", rating: 5.0 }
];
filterByRating(books);
// Output: [ { title: "Book A", rating: 4.5 }, { title: "Book C", rating: 5.0 } ]
Description: Create a generic function that concatenates multiple arrays of the same type using rest parameters.
Function Signature:
function concatenateArrays<T>(...arrays: T[][]): T[]
Example:
concatenateArrays(["a", "b"], ["c"]); // Output: ["a", "b", "c"]
concatenateArrays([1, 2], [3, 4], [5]); // Output: [1, 2, 3, 4, 5]
Description:
- Create a
Vehicle
class with privatemake
andyear
properties and agetInfo()
method. - Create a
Car
class extendingVehicle
, adding a privatemodel
property and agetModel()
method.
Example:
const myCar = new Car("Toyota", 2020, "Corolla");
myCar.getInfo(); // Output: "Make: Toyota, Year: 2020"
myCar.getModel(); // Output: "Model: Corolla"
Description:
Write a function that takes a string | number
and returns:
- The length if it's a string
- The number multiplied by 2 if it's a number
Function Signature:
function processValue(value: string | number): number
Example:
processValue("hello"); // Output: 5
processValue(10); // Output: 20
Description:
Define an interface Product
and create a function to find the product with the highest price in an array. If the array is empty, return null
.
Interface & Function Signature:
interface Product {
name: string;
price: number;
}
function getMostExpensiveProduct(products: Product[]): Product | null
Example:
const products = [
{ name: "Pen", price: 10 },
{ name: "Notebook", price: 25 },
{ name: "Bag", price: 50 }
];
getMostExpensiveProduct(products);
// Output: { name: "Bag", price: 50 }
Description:
- Define an
enum Day
for the days of the week. - Create a function that returns
"Weekday"
or"Weekend"
based on the input day.
Enum & Function Signature:
enum Day {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
function getDayType(day: Day): string
Example:
getDayType(Day.Monday); // Output: "Weekday"
getDayType(Day.Sunday); // Output: "Weekend"
Description: Create an async function that:
- Returns the square of a number after 1 second
- Rejects if the number is negative
Function Signature:
async function squareAsync(n: number): Promise<number>
Example:
squareAsync(4).then(console.log); // Output after 1s: 16
squareAsync(-3).catch(console.error); // Output: Error: Negative number not allowed
- What are some differences between interfaces and types in TypeScript?
- What is the use of the
keyof
keyword in TypeScript? Provide an example. - Explain the difference between
any
,unknown
, andnever
types in TypeScript. - What is the use of
enums
in TypeScript? Provide an example of a numeric and string enum. - What is type inference in TypeScript? Why is it helpful?
- How does TypeScript help in improving code quality and project maintainability?
- Provide an example of using union and intersection types in TypeScript.
โ Instructions:
-
Choose any 2 topics from the list and write a blog post about them.
-
Present your blog through the
README.md
file in your GitHub repository. -
All solutions must be in a single TypeScript file.
-
Do not include comments like โProblem 1โ, โProblem 2โ, etc., in your code.
-
Use meaningful names for all variables, functions, and classes.
-
Ensure the code is original.
โ ๏ธ Plagiarism (copying from friends or online sources) will result in a zero. -
Submit a GitHub public repository link containing your:
- Solutions file
README.md
with the blog content
- ๐ Submit: GitHub Public Repository Link
- ๐ Ensure: The blog content is inside the
README.md
file - ๐ Organize: All code and documentation should be in one repo
- 60 Marks: May 08, 2025 โ before 11:59 PM
- 50 Marks: May 09, 2025 โ before 11:59 PM
- 30 Marks: After May 09, 2025