-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathindex.js
83 lines (67 loc) · 1.91 KB
/
index.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
export default class Pencil {
constructor(durability = 50, length = 50, eraserDurability) {
this.durability = durability;
this.maxDurability = durability;
this.length = length;
this.eraserDurability = eraserDurability
}
getPencilDurability() {
return this.durability;
}
getPencilLength() {
return this.length;
}
getEraserDurability() {
return this.eraserDurability;
}
updatePencilDurability(character) {
if (character !== ' ') {
character === character.toLowerCase() ? this.durability -= 1 : this.durability -= 2;
}
}
updatePencilLength() {
this.length -= 1;
}
writeOnPaper(paper, textToWrite) {
for (let i = 0; i < textToWrite.length; i++) {
this.updatePencilDurability(textToWrite.charAt(i));
this.durability >= 0 ? paper += textToWrite.charAt(i) : paper += " ";
}
return paper;
}
sharpen() {
if (this.length) {
this.updatePencilLength();
this.durability = this.maxDurability;
}
}
erase(paper, text) {
if (paper.lastIndexOf(text) < 0) {
return;
}
const charactersOnPaper = paper.split('');
const indexOfWord = paper.lastIndexOf(text) + text.length - 1;
for (let i = 0; i < text.length; i++) {
if (charactersOnPaper[indexOfWord - i] !== " ") {
this.eraserDurability -= 1;
}
charactersOnPaper[indexOfWord - i] = " ";
}
return charactersOnPaper.join('');
}
edit(paper, textToAdd) {
if (paper.lastIndexOf(" ") < 0) {
return;
}
const charactersOnPaper = paper.split('');
const indexOfBlankSpace = paper.indexOf(" ") + 1;
for (let i = 0; i < textToAdd.length; i++) {
if (charactersOnPaper[indexOfBlankSpace + i] === " ") {
charactersOnPaper[indexOfBlankSpace + i] = textToAdd.charAt(i);
} else {
charactersOnPaper[indexOfBlankSpace + i] = "@";
}
}
return charactersOnPaper.join('');
}
};