forked from bethrobson/Head-First-JavaScript-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.html
51 lines (44 loc) · 900 Bytes
/
palindrome.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Palindrome</title>
<script>
String.prototype.palindrome = function() {
var len = this.length-1;
for (var i = 0; i <= len; i++) {
if (this.charAt(i) !== this.charAt(len-i)) {
return false;
}
if (i === (len-i)) {
return true;
}
}
return true;
};
//
// SUPER advanced way
//
String.prototype.palindromeAdv = function() {
var r = this.split("").reverse().join("");
return (r === this.valueOf());
}
var phrases = ["eve",
"kayak",
"mom",
"wow",
"noon",
"Not a palindrome"];
for (var i = 0; i < phrases.length; i++) {
var phrase = phrases[i];
if (phrase.palindrome()) {
console.log("'" + phrase + "' is a palindrome");
} else {
console.log("'" + phrase + "' is NOT a palindrome");
}
}
</script>
</head>
<body>
</body>
</html>