0% found this document useful (0 votes)
7 views5 pages

Angular Basics 2

Uploaded by

vishalwdv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Angular Basics 2

Uploaded by

vishalwdv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Array is a homogeneous collection

Array elements are accessed via index starts from


0-(size-1)
a[10]
a[0] , a[1],a[2]...............a[9]

syntax:-
let arrname:datatype[]=[values]
eg:
let numlist1:number[]=[10,20,30];
let fruits:string[]=["mango","apple","grapes"];
_________________________________________________

**for-of loop
used to iterate over the values of any collection like any array in a forward
direction
for(let tempvar:collection){...}
eg:
let numlist1:number[]=[10,20,30];
for(let n of numlist1){
console.log(n);
}
output:- 10 20 30
_________________________________________________

**for-in loop
used to iterate over the index any collection like any array
let numlist1:number[]=[10,20,30];
for(let n1 in numlist1){
console.log(n1s);
}

output:-0 1 2

Lab1)create an array holding car brands & print it in a sorted manner using for-of
loop.Also add more elements using push method [array & for-of]

Lab2)create a array holding team scores [use push method to add score of 10
members].Print the highest & lowest & average score of the team

_________________________________________________

**Functions
------------
A function is a block of code which performs some task.It may or may not return the
value. If no return type, void to be written.

in C: int add(int n1,int n2)


{body}
:void Fun1(string str)
{body}

in TS: function add(n1:number,n2:number) : number


{body}
syntax:-
function <functionName>(parameters) : returnType{
//body
}

function Add(n1:number,n2:number): number


{//body}
eg:- //function defination
function greet():void{
console.log("Hello");
}

//function call
greet();
greet();

default parameters in the function


-----------------------------------
function myfunc(num1:number=10,num2:number=20)
{....}

myfunc(34,45);
myfunc(25);
myfunc();

optional parameters in the function using ?:


--------------------------------------------
function myfunc(num1:number,num2:number,num3?:number){....}

myfunc(10,20,30)
myfunc(10,20)

passing array to a function


----------------------------
function myfunc(arr:number[]){...}

let a:number[]=[10,20,30];
myfunc(a)

myfunc([77,88,99,33])

Lab 3)
write a function printMarksheet receiving parameters rollno,name,marks-array
which needs to print marksheet details as follows
rollno=..
student name=..
total=..
percentage=..
**write 2 more functions calTotal & calPercentage & invoke them from printMarksheet

Lab 4)
write a function printBookInfo receiving
bookcode,title,author,price,discount[optional]
output:-
Book Code = ...
Book Title= ...
Book Author= ...
Book Price= ...
NO Discount / Discounted Price=..

Lab5)
write a function productInfo receiving
proCode,proName,price with default parameters

productInfo()
productInfo(1023,"Speakers",4500);

_________________________________________________

In one ts file we write some functionality[function,class,interface,variable] & use


it in another file ---by using import statement

**importing Modules
import {..} from 'path of the ts file w/o extension'
eg:-
import { greet } from "./myfunc";

myfunc.ts
----------
export function greet(){
console.log("Welcome to Typescript");
}

callfunc.ts
------------
import { greet } from './myfunc'

//caller code
greet();
__________________________________________________________
Lab6)
create 2 files
myfunc.ts
----------
write 4 functions add,subtract,multiply,divide
[pass default parameter]

caller.ts
----------
call all the above 4 functions
_________________________________________________________

**Typescript as an object oriented language


-------------------------------------------
Object oriented approach is a programming paradigm in which everything is modelled
as an object

we implement OOPs by writing classes & creating objects

Writing a class in Typescript


-TS filename & classname name can be anything
- Data members are created without let keyword.

-constructor is defined using constructor keyword , not classname

-we can have only one ctor in typescript class[default/parameterized]

-using this is mandatory inside a TS class

-when we define a function inside a class,we dont use the keyword function
function globalfunc():void{....}

class MyClass{
localfunc():void{....}

}
___________________________________________________________

Lab7)Player.ts
write a class Player having data members
playerId,playerName,country,numMatches

write ctor & DisplayInfo member function

TestPlayer.ts
create the array of Player objects & print it using for-of loop

__________________________________________________________
Inheritance
-----------

2 major advantages of inheritance are:


1. Reusability
2. Extensibility

is a property by the virtue of which an object is derived from other object


or an object acquires features of the other object

class A{....}
class B extends A{....}

Here A is a super/parent/base class


Here B is a subclass/child/derived class

eg:-
Every derivd class has 'is-a' relationship with its base class.

class Employee{empId,empName,jdate}

class Manager extends Employee{basicSal,incentives}


class WageEmployee extends Employee{hours,rate}

we can create an array of super type which holds subclass objects[generic


reference]

let earr:Employee[]=[];
earr.push(new Manager(....));
earr.push(new WageEmployee(....));
**super keyword is available to evry subclass to access any member of super class.

Lab3) Inheritance topic


Batsman.ts
------------
Write a class Batsman extends Player-->numRuns
ctor,DisplayInfo

Bowler.ts
------------
Write a class Bowler extends Player-->numWickets
ctor,DisplayInfo

caller.ts
---------
create an array of Player type---team holding 2 Batsman object & 2 Bowler objects
& display all players using for-of loop

eg:
let p1:Batsman=new Batsman(........);
let p2: Bowler=new Bowler(.........);
let team:Player[]=[p1,p2];
here p1 is a Batsman object
& p2 is a Bowler object
_________________________________________________________

You might also like