-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnion.ts
More file actions
48 lines (46 loc) · 1.16 KB
/
Copy pathUnion.ts
File metadata and controls
48 lines (46 loc) · 1.16 KB
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
function add(a: number | string, b: number | string) {
try {
if (typeof a === "number" && typeof b === "number") {
return a + b;
}
if (typeof a === "string" && typeof b === "string") {
return a.concat(b);
}
throw new Error("Parameters must be numbers or strings");
} catch (e) {
console.log(e);
}
}
// function disp(array?: number[] | string[]) {
// if (typeof array === "number") {
// console.log(typeof array);
// }
// if (typeof array === "string") {
// console.log(typeof array);
// }
// }
let x: number | string;
x = add(12, 34);
let y: number | string;
y = add("Gokul", "R");
console.log(x+"\n");
console.log(y+"\n");
let z = add(78, "gokul");
console.log(z+"\n");
let arr: number[] | string[];
arr = [60, 24, 15, 74, 35, 84, 12, 19, 34, 40];
let l = arr.length;
// console.log(disp(arr));
console.log(typeof arr);
console.log(l);
for (let i = 0; i < l; i++) {
console.log(arr[i]);
}
console.log("\n");
arr = ["Gokul", "Sweatha", "Sachin", "Pavi", "Kishore", "Srinath", "Rikhitha"];
// console.log(disp(arr));
console.log(typeof arr);
let q = arr.length;
for (let i = 0; i < q; i++) {
console.log(arr[i]);
}