https://github.com/himanshuteotia/typescript_with_nodejs
Typescript tutorial with simple projects like google map implementation and sorting algorithim
https://github.com/himanshuteotia/typescript_with_nodejs
Last synced: 4 months ago
JSON representation
Typescript tutorial with simple projects like google map implementation and sorting algorithim
- Host: GitHub
- URL: https://github.com/himanshuteotia/typescript_with_nodejs
- Owner: himanshuteotia
- Created: 2020-04-10T05:45:48.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-27T09:38:44.000Z (almost 5 years ago)
- Last Synced: 2024-12-27T08:27:42.388Z (5 months ago)
- Language: TypeScript
- Homepage:
- Size: 2.16 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Typescript
A junior to senior developer:
```you write interfaces & abstract classes I will write classes for them ```
Abstract class :
1. We can not create objects directly
2. Only used as a parent class
3. An abstract class can have reference methods those we can define in child class```
// here abstract class is saying provide me these three functions { compare, swap, length }
// I will do the sort for youabstract class SortArray {
abstract compare(leftIndex: number, rightIndex: number) : boolean;
abstract swap(leftIndex: number, rightIndex: number) : void;
abstract length:number;
sort() : void {
const { length } = this;
for(let i =0; i < length; i++) {
for( let j =0; j < length-1; j++ ) {
if(this.compare(j, j+1)) {
this.swap(j,j+1)
}
}
}
}
}
class NumbersCollection extends SortArray {
constructor(public data: number[]) {
super()
}
get length():number{
return this.data.length;
}
compare(leftIndex: number, rightIndex: number):boolean {
return this.data[leftIndex] > this.data[rightIndex];
}
swap(leftIndex: number, rightIndex: number) {
const leftHand = this.data[leftIndex];
this.data[leftIndex] = this.data[rightIndex];
this.data[rightIndex] = leftHand;
}
}const arr = new NumbersCollection([2,4,8,2,4,8,2,4,8])
arr.sort()
console.log(arr.data);
// [2, 2, 2, 4, 4, 4, 8, 8, 8]
```