Repository navigation
Expand file tree
/
Copy pathtask-a05.ts
More file actions
69 lines (61 loc) · 1.36 KB
/
Copy pathtask-a05.ts
File metadata and controls
69 lines (61 loc) · 1.36 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// minimal value 3, 5, 6, 7, 3, 4, 1, 5, 9, 9, 2, 1
function min(n: number): void {
let min = 1;
for (let i = 0; i < n; i++) {
let rnd = Math.floor(Math.random() * 20);
console.log('RND: ', rnd);
if (i == 0) min = rnd;
if (min > rnd) min = rnd;
console.log('min: ', min);
}
}
// maximal value
// abcd -> dcba
function inverseNumber(n: number): number {
let inverse = 0;
let i = 1;
let j = 1;
while (n > 1) {
inverse += Math.floor(n % 10) * i;
console.log('Number: ', n, ' Inverse: ', inverse);
i /= 10;
j *= 10;
n = Math.floor(n / 10);
}
return Math.floor((inverse * j) / 10);
}
function contains2(n: number): boolean {
const two = 2;
while (n > 1) {
if (Math.floor(n % 10) == two) return true;
n = Math.floor(n / 10);
}
return false;
}
// howMany2
// switch first and last: 246789 -> 946782
function firstLast(n: number): number {
let last = (n % 10) * 10;
n = Math.floor(n / 10);
let partial = 0;
let i = 1;
while (n > 1) {
if (n < 10) {
partial += n;
break;
} else {
partial += (n % 10) * 10 * i;
n = Math.floor(n / 10);
i *= 10;
}
}
return last * i + partial;
}
function run(): void {
console.log('Task 05');
// min(6);
//console.log(inverseNumber(48520));
// console.log(contains2(2487));
console.log(firstLast(246789));
}
export default run;