JSFiddle - React, Tailwind, and code Playground
by JInks Peng
JavaScript
function Stack() {
this.dataStore = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek;
this.length = length;
this.clear =clear;
}
function push(elem) {
this.dataStore[this.top++] = elem;
}
function pop() {
var data = this.dataStore[--this.top];
this.dataStore.length = this.top;
return data;
}
function peek() {
return this.dataStore[this.top--];
}
function length() {
return this.top;
}
function clear() {
this.top = 0;
}
function isPalindrome(word) {
var s = new Stack(),rword = "";
for(var i = 0; i < word.length; i++) {
s.push(word[i]);
}
while(s.length() > 0) {
rword += s.pop();
}
if(word === rword) {
return true;
} else {
return false;
}
}
function checkWord(word) {
if(isPalindrome(word)) {
console.log(word + " is a Palindrome");
} else {
console.log(word + " is not s Palindrome");
}
}
var word = "hello";
checkWord(word);
var word = "racecar";
checkWord(word);