-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
234 lines (187 loc) · 5.63 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// 1-topshiriq
// enum Color {
// RED = "Red",
// GREEN = "Green",
// BLUE = "Blue",
// }
// function getColorMessage(color: Color): string {
// switch (color) {
// case Color.RED:
// return "Siz qizil rangni tanladingiz!";
// case Color.GREEN:
// return "Siz yashil rangni tanladingiz!";
// case Color.BLUE:
// return "Siz ko'k rangni tanladingiz!";
// default:
// return "Noto'g'ri rang!";
// }
// }
// const selectedColor: Color = Color.GREEN;
// console.log(getColorMessage(selectedColor));
// 2-topshiriq
// const carWithOwner: Car = {
// brand: "Tesla",
// model: "Model S",
// year: 2023,
// ownerName: "John Doe",
// };
// interface Car {
// brand: string;
// model: string;
// year: number;
// ownerName?: string;
// }
// console.log(carWithOwner);
// 3-topshiriq
// interface Person {
// name: string;
// age: number;
// }
// interface Employee {
// company: string;
// }
// type PersonEmployee = Person & Employee;
// const personEmployee: PersonEmployee = {
// name: "Alice",
// age: 30,
// company: "TechCorp",
// };
// interface Admin {
// role: "admin";
// accessLevel: number;
// }
// interface User {
// role: "user";
// permissions: string[];
// }
// type AdminUser = Admin & User;
// type FlexibleAdminUser = {
// role: "admin" | "user";
// } & {
// accessLevel?: number;
// permissions?: string[];
// };
// const flexibleUser: FlexibleAdminUser = {
// role: "admin",
// accessLevel: 5,
// permissions: ["read", "write"],
// };
// console.log("FlexibleAdminUser obyekt:", flexibleUser);
// console.log("PersonEmployee obyekt:", personEmployee);
// 4-topshiriq
// 2x oshirish
// const numbers: number[] = [1, 2, 3, 4, 5];
// function array(arr: number[]): number[] {
// return arr.map((num) => num * 2);
// }
// const Numbers = array(numbers);
// console.log(Numbers);
// lengthsi 5 dan koplarni olish
// const strings: string[] = ["hello", "world", "typescript", "is", "fn"];
// function filter(arr: string[]): string[] {
// return arr.filter((str) => str.length > 5);
// }
// const stringtype = filter(strings);
// console.log("Uzunligi 5 dan katta bo'lgan satrlar:", stringtype);
// 5-topshiriq
// interface User {
// name: string;
// age: number;
// email: string;
// }
// const user: User = {
// name: "John Doe",
// age: 28,
// email: "john.doe@example.com",
// };
// function displayUserInfo(user: User): void {
// console.log(`Name: ${user.name}`);
// console.log(`Age: ${user.age}`);
// console.log(`Email: ${user.email}`);
// }
// displayUserInfo(user);
// 6-topshiriq
// function yigindi(a: number, b: number): number {
// return a + b;
// }
// function ortacha(...numbers: number[]): number {
// if (numbers.length === 0) {
// console.log("Argumentlar yo'q, o'rtacha qiymat: 0");
// return 0;
// }
// const sum = numbers.reduce((acc, curr) => acc + curr, 0);
// return sum / numbers.length;
// }
// const ortachaNatija1 = ortacha(5, 10, 15);
// const yigindiNatija = yigindi(10, 20);
// console.log(yigindiNatija);
// console.log(ortachaNatija1);
// 7-topshiriq
// const userInfo: [string, number, string] = ["Sevgidan kuygan chiyabori", 25, "uyidan|xaydalgan|it@example.com"];
// console.log(`Ism: ${userInfo[0]}, Yosh: ${userInfo[1]}, Email: ${userInfo[2]}`);
// 8-topshiriq
// const mixedArray: any[] = [42, "Hello", { name: "Ali" }, 100, "World", true];
// function numbers1(arr: any[]): number[] {
// return arr.filter(item => typeof item === "number");
// }
// function strings1(arr: any[]): string[] {
// return arr.filter(item => typeof item === "string");
// }
// const numbers = numbers1(mixedArray);
// const strings = strings1(mixedArray);
// console.log("raqamlar:", numbers);
// console.log("stringlar:", strings);
// 9-topshiriq
// function getStringLength(input: unknown): number | null {
// if (typeof input === "string") {
// return input.length;
// }
// return null;
// }
// function toUpperCaseString(input: unknown): string {
// return String(input).toUpperCase();
// }
// console.log("Satr uzunligi:", getStringLength("Hello"));
// console.log("Satr uzunligi:", getStringLength(42));
// console.log("Katta harfda:", toUpperCaseString("hello"));
// console.log("Katta harfda:", toUpperCaseString(12345));
// console.log("Katta harfda:", toUpperCaseString(true));
// console.log("Katta harfda:", toUpperCaseString({ key: "value" }));
// 9-topshiriq
type User = {
name: string;
age: number;
isStudent: boolean;
};
const user1: User = {
name: "Ali",
age: 25,
isStudent: true,
};
const user2: User = {
name: "Samar",
age: 30,
isStudent: false,
};
console.log(user1, user2);
type Name = string;
type Age = number;
type UserDetail = {
name: Name;
age: Age;
};
const userA: UserDetail = {
name: "Ali",
age: 25,
};
const userB: UserDetail = {
name: "Samar",
age: 30,
};
console.log(userA, userB);
type MultiplyNumbers = (a: number, b: number) => number;
type ConcatenateStrings = (a: string, b: string) => string;
const sum: MultiplyNumbers = (a, b) => a * b;
const text: ConcatenateStrings = (a, b) => a + b;
console.log(sum(5, 10));
console.log(text("Hello", " World"));