-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (38 loc) · 1.35 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
module.exports = Phrase;
// Add `reverse` to all strings.
String.prototype.reverse = function() {
return Array.from(this).reverse().join("");
}
// Defines a Phrase object.
function Phrase(content) {
this.content = content;
// Returns the letters in the content.
// E.g.: new Phrase("Hello, World!").letters() === "Helloworld"
this.letters = function letters() {
// match any letter, case-insensitive
const letterRegex = /[a-z]/gi;
return (this.content.match(letterRegex) || []).join("");
}
// Returns content processed for palindrome testing.
this.processedContent = function processedContent() {
return this.letters().toLowerCase();
}
// Returns true if the phrase is a palindrome, false otherwise.
this.palindrome = function palindrome() {
if (this.processedContent()) {
return this.processedContent() === this.processedContent().reverse();
} else {
return false;
}
}
}
// Defines TranslatedPhrase object.
function TranslatedPhrase(content, translation) {
this.content = content;
this.translation = translation;
// Returns translation processed for palindrome testing.
this.processedContent = function processedContent() {
return this.processor(this.translation);
}
}
TranslatedPhrase.prototype = new Phrase();