This assignment is designed to assess your understanding of fundamental TypeScript concepts. It includes a series of problems that cover topics such as types, functions, classes, interfaces, and array manipulation.
Create a function formatValue that accepts a value which may be a string, number, or boolean, and returns the following based on the value type:
- If the input is a string β return the string in uppercase
- If the input is a number β return the number multiplied by 10
- If the input is a boolean β return the opposite value (
true β false,false β true)
- You must write the correct type for the function parameter and the return type.
- You must use type checking to handle each case.
console.log(formatValue('hello'));
console.log(formatValue(5));
console.log(formatValue(true));HELLO;
50;
false;Create a function getLength that accepts a value which may be a string or an array, and returns the length of the value.
- If the input is a string β return the number of characters.
- If the input is an array β return the number of elements.
- You must write the correct type for the function parameter and the return type.
- You must use type checking to handle each case (
typeoforArray.isArray).
console.log(getLength('typescript'));
console.log(getLength([10, 20, 30, 40]));10;
4;Create a Person class with name and age properties. Add a method getDetails that returns a string with the person's name and age.
- You must use a constructor to initialize the properties.
- The
getDetailsmethod should return a string in the format:"Name: [name], Age: [age]".
const person1 = new Person('John Doe', 30);
console.log(person1.getDetails());
const person2 = new Person('Alice', 25);
console.log(person2.getDetails());'Name: John Doe, Age: 30';
'Name: Alice, Age: 25';Create a function filterByRating that accepts an array of items, where each item has the following properties:
title(string)rating(number between 0 and 5)
The function should return a new array containing only the items with a rating of 4 or more.
- You must write the correct type for the function parameter and the return type.
- Do not mutate the original array.
const books = [
{ title: 'Book A', rating: 4.5 },
{ title: 'Book B', rating: 3.2 },
{ title: 'Book C', rating: 5.0 },
];
console.log(filterByRating(books));[
{ title: 'Book A', rating: 4.5 },
{ title: 'Book C', rating: 5.0 },
];Create a function filterActiveUsers that accepts an array of user objects. Each user object contains id, name, email, and isActive properties. The function should return a new array containing only the users whose isActive property is true.
- You must write the correct type for the function parameter and the return type.
- Do not mutate the original array.
- Use type checking if necessary.
const users = [
{ id: 1, name: 'Rakib', email: '[email protected]', isActive: true },
{ id: 2, name: 'Asha', email: '[email protected]', isActive: false },
{ id: 3, name: 'Rumi', email: '[email protected]', isActive: true },
];
console.log(filterActiveUsers(users));[
{ id: 1, name: 'Rakib', email: '[email protected]', isActive: true },
{ id: 3, name: 'Rumi', email: '[email protected]', isActive: true },
];Define an interface Book with the following properties:
title(string)author(string)publishedYear(number)isAvailable(boolean)
Then, create a function printBookDetails that accepts an object of type Book and prints its details to the console in the format: "Title: [title], Author: [author], Published: [publishedYear], Available: [Yes/No]".
- You must define the
Bookinterface correctly. - The
printBookDetailsfunction must accept an object that follows to theBookinterface.
const myBook: Book = {
title: 'The Great Gatsby',
author: 'F. Scott Fitzgerald',
publishedYear: 1925,
isAvailable: true,
};
printBookDetails(myBook);Title: The Great Gatsby, Author: F. Scott Fitzgerald, Published: 1925, Available: Yes
Create a function getUniqueValues that accepts two arrays and returns a new array containing only the unique values from both arrays, without any duplicates.
- You must write the correct type for the function parameter and the return type.
- The function should handle arrays of strings or numbers.
- You are not allowed to use any built-in methods to solve this problem.
const array1 = [1, 2, 3, 4, 5];
const array2 = [3, 4, 5, 6, 7];
console.log(getUniqueValues(array1, array2));[1, 2, 3, 4, 5, 6, 7];Create a function calculateTotalPrice that accepts an array of product objects. Each product object contains the following properties:
name(string)price(number)quantity(number)- discount?: optional number from 0β100, representing a percentage discount
The function should return the total price of all products in the array, taking into account the discount for each product (if provided).
If the array is empty, return 0.
- You must write the correct type for the function parameter and the return type.
- Use array methods (
map,reduce, etc.) to calculate the total. - The total price of each product is calculated as:
(price * quantity). - Correctly handle products with and without the
discountproperty.
const products = [
{ name: 'Pen', price: 10, quantity: 2 },
{ name: 'Notebook', price: 25, quantity: 3, discount: 10 },
{ name: 'Bag', price: 50, quantity: 1, discount: 20 },
];
console.log(calculateTotalPrice(products));127.5;- What are some differences between interfaces and types in TypeScript?
- What is the use of the
keyofkeyword in TypeScript? Provide an example. - Explain the difference between
any,unknown, andnevertypes in TypeScript. - What is the use of
enumsin TypeScript? Provide an example of a numeric and string enum. - Provide an example of using union and intersection types in TypeScript.
- Blog: Write a blog post on any 2 of the provided topics and include it in this
README.md. - Coding:
- Place all your solutions in a single TypeScript file (e.g., solution.ts).
- Use the exact function names provided in the problems.
- Your function's output must exactly match the sample output provided. This includes punctuation, spacing, and capitalization. Any deviation will result in marks being deducted. For instance, if the sample output is
'Name: John Doe, Age: 30', your function must return that exact string. A return value like'name: John Doe, age: 30'or'Name: John Doe,Age: 30'will be considered incorrect. - Use meaningful names for all variables, functions, and classes.
- Do not include comments like βProblem 1β in your code.
- Do not include any
console.login your final code.
- Originality: All work must be original. Plagiarism will result in a zero.
- Submission: Submit a single public GitHub repository which will includes your solution.ts file and the updated
README.md.
- 60 Marks: November 18, 2025 β before 11:59 PM
- 50 Marks: November 19, 2025 β before 11:59 PM
- 30 Marks: After November 19, 2025