File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Title: Design a Text Editor
3
+ * Description: Design a text editor with a cursor that can do the following: Add text to where the cursor is. Delete text from where the cursor is (simulating the backspace key). Move the cursor either left or right.
4
+ * Author: Hasibul Islam
5
+ * Date: 06/04/2023
6
+ */
7
+
8
+ var TextEditor = function ( ) {
9
+ this . leftText = "" ;
10
+ this . rightText = "" ;
11
+ } ;
12
+
13
+ /**
14
+ * @param {string } text
15
+ * @return {void }
16
+ */
17
+ TextEditor . prototype . addText = function ( text ) {
18
+ this . leftText += text ;
19
+ } ;
20
+
21
+ /**
22
+ * @param {number } k
23
+ * @return {number }
24
+ */
25
+ TextEditor . prototype . deleteText = function ( k ) {
26
+ if ( k > this . leftText . length ) k = this . leftText . length ;
27
+ this . leftText = this . leftText . substr ( 0 , this . leftText . length - k ) ;
28
+ return k ;
29
+ } ;
30
+
31
+ /**
32
+ * @param {number } k
33
+ * @return {string }
34
+ */
35
+ TextEditor . prototype . cursorLeft = function ( k ) {
36
+ if ( this . leftText . length < k ) k = this . leftText . length ;
37
+ let temp = this . leftText . substr ( this . leftText . length - k ) ;
38
+ this . leftText = this . leftText . substr ( 0 , this . leftText . length - k ) ;
39
+ this . rightText = temp + this . rightText ;
40
+ return this . leftText . substr (
41
+ this . leftText . length - Math . min ( this . leftText . length , 10 )
42
+ ) ;
43
+ } ;
44
+
45
+ /**
46
+ * @param {number } k
47
+ * @return {string }
48
+ */
49
+ TextEditor . prototype . cursorRight = function ( k ) {
50
+ if ( this . rightText . length < k ) k = this . rightText . length ;
51
+ let temp = this . rightText . substr ( 0 , k ) ;
52
+ this . rightText = this . rightText . substr ( k ) ;
53
+ this . leftText = this . leftText + temp ;
54
+ return this . leftText . substr (
55
+ this . leftText . length - Math . min ( this . leftText . length , 10 )
56
+ ) ;
57
+ } ;
You can’t perform that action at this time.
0 commit comments