@@ -8,9 +8,7 @@ const TextEditor = function () {
8
8
* @return {void }
9
9
*/
10
10
TextEditor . prototype . addText = function ( text ) {
11
- for ( const ch of text ) {
12
- this . stk1 . push ( ch )
13
- }
11
+ for ( const ch of text ) this . stk1 . push ( ch )
14
12
}
15
13
16
14
/**
@@ -19,7 +17,7 @@ TextEditor.prototype.addText = function (text) {
19
17
*/
20
18
TextEditor . prototype . deleteText = function ( k ) {
21
19
let res = 0
22
- while ( this . stk1 . length && k ) {
20
+ while ( this . stk1 . length && k ) {
23
21
k --
24
22
res ++
25
23
this . stk1 . pop ( )
@@ -33,19 +31,13 @@ TextEditor.prototype.deleteText = function (k) {
33
31
*/
34
32
TextEditor . prototype . cursorLeft = function ( k ) {
35
33
let res = ''
36
- while ( this . stk1 . length && k ) {
34
+ while ( this . stk1 . length && k ) {
37
35
const tmp = this . stk1 . pop ( )
38
36
this . stk2 . push ( tmp )
39
37
k --
40
38
}
41
39
42
-
43
- for ( let len = this . stk1 . length , size = Math . min ( 10 , this . stk1 . length ) , i = 0 ; i < size ; i ++ ) {
44
- res = this . stk1 [ len - 1 - i ] + res
45
- }
46
-
47
-
48
- return res
40
+ return this . slice ( )
49
41
}
50
42
51
43
/**
@@ -55,16 +47,24 @@ TextEditor.prototype.cursorLeft = function (k) {
55
47
TextEditor . prototype . cursorRight = function ( k ) {
56
48
let res = ''
57
49
58
- while ( this . stk2 . length && k ) {
50
+ while ( this . stk2 . length && k ) {
59
51
const tmp = this . stk2 . pop ( )
60
52
this . stk1 . push ( tmp )
61
53
k --
62
54
}
63
55
64
- for ( let len = this . stk1 . length , size = Math . min ( 10 , this . stk1 . length ) , i = 0 ; i < size ; i ++ ) {
65
- res = this . stk1 [ len - 1 - i ] + res
66
- }
56
+ return this . slice ( )
57
+ }
67
58
59
+ TextEditor . prototype . slice = function ( ) {
60
+ let res = ''
61
+ for (
62
+ let len = this . stk1 . length , size = Math . min ( 10 , this . stk1 . length ) , i = 0 ;
63
+ i < size ;
64
+ i ++
65
+ ) {
66
+ res = this . stk1 [ len - i - 1 ] + res
67
+ }
68
68
return res
69
69
}
70
70
0 commit comments