0

function palindrome(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;
  }
}

console.log(palindrome('test1'))
console.log(palindrome('manam'))

/* im trying to check if word is a palindrome using stack. But when I run my code it's always return false , even when word is a palindrome*/

CRice
  • 29,968
  • 4
  • 57
  • 70
  • 2
    How is `Stack` defined? That's not a built in javascript type, so where did it come from? Your code works as is if you change to `var s = []` and `s.length` without parentheses. – CRice Feb 27 '18 at 21:48

3 Answers3

1

use an array for stack . this code works :

function palindrome(word) {
  var s = [];
  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;
  }
}

console.log(palindrome('manam'))

demo note : you also used length() in your code instead of length

CRice
  • 29,968
  • 4
  • 57
  • 70
Zoha
  • 546
  • 7
  • 13
1

I added one simple Stack implementation, then the function=palindrome should work as you expected.

And I added one function = palindrome1 which is the simple optimization (only loop for half length of the string) for palindrome judgement.

function Stack()
{
 this.stac=new Array();
 this.pop=function(){
  return this.stac.pop();
 };
 this.push=function(item){
  this.stac.push(item);
 };
 this.length=function(){
  return this.stac.length;
 };
}
function palindrome(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;
    }
}

console.log(palindrome('test1'))
console.log(palindrome('manam'))

function palindrome1(word){
   var maxIndex = word ? word.length:0;
   if(!maxIndex || maxIndex==0) {
    return false;
   }
   var index = 0;
   var left = "";
   var right = "";
   while(index<(maxIndex/2)) {
    left += word[index];
    right += word[maxIndex-index-1];
    index++;
   }
   return left==right;
}
console.log(palindrome1('test1'))
console.log(palindrome1('manam'))
Sphinx
  • 10,519
  • 2
  • 27
  • 45
  • The code is secondary, the first priority should be the algorithm. Trying to optimize the algorithm before coding. – Sphinx Feb 27 '18 at 22:16
  • Thanks for that advice. I was doing some online courses and now I started to read JS data structure and algorithm book so hopefully it will help me to understand and do better code... hopefully... –  Feb 27 '18 at 22:34
1

In addition to what's already been mentioned, you can simplify the palindrome checker to be a much smaller function (es6+). Since a palindrome is equal to its own reversal, we can just check that (and we can use the built in methods to do the reverse rather than implementing it ourselves with a stack).

const palindrome = str => str === str.split("").reverse().join("");

console.log(palindrome("racecar"))
console.log(palindrome("palindrome"))
CRice
  • 29,968
  • 4
  • 57
  • 70
  • This is much more elegant solution than mine. beautiful usage for built-in features. – Sphinx Feb 27 '18 at 22:20
  • 2
    In ES6 you can use the spread operator to get each character of a string into an array, so `str => str === [...str].reverse().join('')` – Julien Zakaib Feb 27 '18 at 22:55