-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (38 loc) · 1.25 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;
// Adds '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 content processed for aplindrome testing.
this.processedContent = function processedContent() {
return this.letters().toLowerCase();
}
//Returns the letters in the content.
//for example:
// new Phrase("Hello, World!").letters() === "Helloworld"
this.letters = function letters() {
const lettersRegEx = /[a-z]/i;
return Array.from(this.content).filter(c => c.match(lettersRegEx)).join("");
}
// Returns true if the phrase is a palindorme, false otherwise.
this.palindrome = function palindrome() {
if (this.letters()) {
return this.processedContent() === this.processedContent().reverse();
} else {
return false;
}
}
}
// Defines a TranslatedPhrase object.
function TranslatedPhrase(content, translation) {
this.content = content;
this.translation = translation;
// Returns translation processed for palindrome testing.
this.processedContent = function processedContent() {
return this.translation.toLowerCase();
}
}
TranslatedPhrase.prototype = new Phrase();