-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.4.js
64 lines (52 loc) · 975 Bytes
/
4.4.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
"use strict";
function Stack() {
this.dataStore = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek;
this.length = length;
this.clear = clear;
}
function push(element) {
this.dataStore[this.top++] = element;
}
function pop(element) {
return this.dataStore[--this.top];
}
function peek() {
return this.dataStore[this.top-1];
}
function length() {
return this.top;
}
function clear() {
this.top = 0;
}
function isPalindrome(word) {
var s = new Stack();
for(var i = 0; i < word.length; ++i) {
s.push(word[i]);
}
var rword = "";
while(s.length() > 0) {
rword += s.pop();
}
if(word == rword) {
return true;
} else {
return false;
}
}
var word = 'hello';
if(isPalindrome(word)) {
console.log(word + '是回文');
} else {
console.log(word + '不是回文');
}
word = 'rececer';
if(isPalindrome(word)) {
console.log(word + '是回文');
} else {
console.log(word + '不是回文');
}