Skip to content

Commit 6cea1b9

Browse files
author
hasibulislam999
committed
Design a Text Editor problem solved
1 parent 5eca86d commit 6cea1b9

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
};

0 commit comments

Comments
 (0)