0% found this document useful (0 votes)
29 views3 pages

Exercise-10 MST Programs (1)

The document outlines exercises in TypeScript covering various programming concepts such as Rest Parameters, Interfaces, Duck Typing, and Function Types. Each section includes the aim, source code, and expected outputs for implementing logic related to product management and arithmetic operations. The exercises demonstrate practical applications of TypeScript features through code examples and console outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Exercise-10 MST Programs (1)

The document outlines exercises in TypeScript covering various programming concepts such as Rest Parameters, Interfaces, Duck Typing, and Function Types. Each section includes the aim, source code, and expected outputs for implementing logic related to product management and arithmetic operations. The exercises demonstrate practical applications of TypeScript features through code examples and console outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercise -10 MST Programs

10. a
Course Name: Typescript
Module Name: Rest Parameter
AIM: Implement business logic for adding multiple Product values into a cart variable
which is type of string array.

SOURCE CODE: restp.ts


const cart: string[] = []; // declaring a empty string array
// arrow function logic to push values into cart array
const pushtoCart = (item: string) => { cart.push(item); };
// logic to add items into cart
function addtoCart(...productName: string[]): string[] {
for (const item of productName) {
pushtoCart(item);
}
return cart;
}
// to populate value on console
addtoCart(' Opo A53', ' Apple iPhone 5s','Samsung Galaxy S21','OnePlus 9 Pro');
console.log('Cart Items are :' +cart );

10.a OUTPUT:

10.b
Course Name: Typescript
Module Name: Creating an Interface
AIM: Declare an interface named - Product with two properties like productId and
productName with a number and string data type and need to implement logic to
populate the Product details.

SOURCE CODE: interface1.ts


interface Product { // declaring an interface
productId: number ;
productName: string ;
}
// logic to display the Product details with interface object as parameter
function getProductDetails(productobj: Product): string {
return 'The Product Id is : ' + productobj.productId+"\n"+'The Product Name is : ' +
productobj.productName;
}
// declaring a variable having interface properties
const prodObject = {productId: 180, productName: 'Mobile'};
// declaring variable and invoking Product details function
const productDetails: string = getProductDetails(prodObject);
// line to populate the created product on console
console.log("Product Details:");
console.log(productDetails);

10.b OUTPUT:

10.c
Course Name: Typescript
Module Name: Duck Typing
AIM: Declare an interface named - Product with two properties like productId and
productName with the number and string data type and need to implement logic to
populate the Product details.

SOURCE CODE: duck.ts


interface Product { // declaring an interface
productId: number;
productName: string;
}
// logic to display the Product details with interface object as parameter
function getProductDetails(productobj: Product): string {
return 'Product Id : ' +productobj.productId+"\n"+'Product Name : ' +
productobj.productName;
}
// declaring a variable along with interface properties
//Adding an additional property productCategory to demonstrate Duck typing
const prodObject = {productId: 180, productName: 'Mobile', productCategory: 'Gadget'};
// declaring variable and invoking Product details function
const productDetails: string = getProductDetails(prodObject);
// line to populate the created product variable on console
console.log(productDetails);
10.c OUTPUT:

10.d
Course Name: Typescript
Module Name: Function Types
AIM: Declare an interface with function type and access its value.

SOURCE CODE: funtypes.ts


// Define an interface with a function types
interface Arithmetic {
(a: number, b: number): number;
}
// Define a function that implements the interface
let add: Arithmetic = (a : number, b : number) =>{
return a + b;
};
let sub: Arithmetic = (a : number, b : number) => {
return a - b;
};
let mul: Arithmetic = (a : number, b : number) => {
return a * b;
};
// Call the functions and display the results
var result;
result = add(4, 2);
console.log("ADDITION :"+result);
result = sub(4, 2);
console.log("SUBTRACTION :"+result);
result = mul(4, 2);
console.log("MULTIPLICATION :"+result);
10.d OUTPUT:

You might also like