-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.js
133 lines (112 loc) · 1.84 KB
/
1.js
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
/**
*
* @param {Array} a
* @param {any} element
*/
function push(a, element) {
/**
* + a = [1,2,3,4,5]
* + element = 6
* + ret = [1,2,3,4,5,6]
*
*/
const ret = new Array(a.length + 1);
for (let i = ret.length - 1 - 1; i >= 0; --i) {
ret[i] = a[i];
}
ret[ret.length - 1] = element;
return ret;
}
/**
*
* @param {Array} a
*/
function reverseArray(a) {
/**
* -------0 1 2
* + a = [1,2,3]
*
* ---------0 1 2
* + ret = [3,2,1]
*
* + ret[0] = a[2]
* + ret[1] = a[1]
* + ret[2] = a[0]
*
*
* + i: length -> 0
* + ret[i] = a[length - i - 1]
*/
const length = a.length;
const ret = new Array(length);
for (let i = length - 1; i >= 0; --i) {
ret[i] = a[length - i - 1];
}
return ret;
}
/**
*
* @param {Array} a
*/
function pop(a) {
/**
* -------0 1 2 3
* + a = [1,2,3,4]
* ---------0 1 2
* + ret = [1,2,3]
*
*/
const ret = new Array(a.length - 1);
for (let i = ret.length - 1; i >= 0; --i) {
ret[i] = a[i];
}
return ret;
}
/**
*
* @param {Array} a
* @param {string} factor
*/
function join(a, factor) {
/**
* + a = ['1','0']
* + factor = '-'
* + ret = "1-0"
*
*/
let ret = "";
for (let i = 0; i <= a.length - 1; ++i) {
if (i === a.length - 1) {
ret += a[i];
break;
}
ret += a[i] + factor;
}
return ret;
}
/**
*
* @param {string} pixel
*/
function getPixelNumber(pixel) {
/**
* --0123
* + 10px
* + length: 4
* + traverse to 1:
* + 1 = 4 - 1 - 1 - 1
* + 1 = length - 1 - 1 -1
* + ret: 10
*
*/
let ret = [];
const length = pixel.length;
for (let i = length - 1 - 1 - 1; i >= 0; --i) {
ret = push(ret, pixel[i]);
}
ret = reverseArray(ret);
ret = join(ret, "");
return +ret;
}
const pixel = "12345px";
console.log(getPixelNumber(pixel)); // 10