Skip to content

Commit c6cf4af

Browse files
authored
Update 2296-design-a-text-editor.js
1 parent 573bf16 commit c6cf4af

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

2296-design-a-text-editor.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ const TextEditor = function () {
88
* @return {void}
99
*/
1010
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)
1412
}
1513

1614
/**
@@ -19,7 +17,7 @@ TextEditor.prototype.addText = function (text) {
1917
*/
2018
TextEditor.prototype.deleteText = function (k) {
2119
let res = 0
22-
while(this.stk1.length && k) {
20+
while (this.stk1.length && k) {
2321
k--
2422
res++
2523
this.stk1.pop()
@@ -33,19 +31,13 @@ TextEditor.prototype.deleteText = function (k) {
3331
*/
3432
TextEditor.prototype.cursorLeft = function (k) {
3533
let res = ''
36-
while(this.stk1.length && k) {
34+
while (this.stk1.length && k) {
3735
const tmp = this.stk1.pop()
3836
this.stk2.push(tmp)
3937
k--
4038
}
4139

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()
4941
}
5042

5143
/**
@@ -55,16 +47,24 @@ TextEditor.prototype.cursorLeft = function (k) {
5547
TextEditor.prototype.cursorRight = function (k) {
5648
let res = ''
5749

58-
while(this.stk2.length && k) {
50+
while (this.stk2.length && k) {
5951
const tmp = this.stk2.pop()
6052
this.stk1.push(tmp)
6153
k--
6254
}
6355

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+
}
6758

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+
}
6868
return res
6969
}
7070

0 commit comments

Comments
 (0)