dadadodo
dadadodo

Reputation: 347

javascript program to check if a string is palindromes not returning false

I wrote this bit of code a a part of an exercise to check weather or not a string is palindromes. They program is working correctly in terms of checking the string but it does not return false when the string is not palindromes. What am I doing wrong? thanks

//convert the string to array
var stringArr = [ ];
var bool;
function palindrome(str) {
  // make lowercase
  var lowerCase = str.toLowerCase();
  //remove numbers, special characters, and white spaces
  var noNumbers = lowerCase.replace(/[0-9]/g, '');
  var noSpecials = noNumbers.replace(/\W+/g, " ");
  var finalString = noSpecials.replace(/\s/g, '');
  stringArr = finalString.split("");

    if (stringArr.sort(frontToBack)==stringArr.sort(backToFront)) {
       bool = true;  
    }
    else {
       bool= false;

    }
  return bool;
}
function frontToBack (a,b) {return a-b;}
function backToFront (a,b) {return b-a;}

palindrome("eye");

Upvotes: 0

Views: 417

Answers (4)

Sanjay Rai
Sanjay Rai

Reputation: 27

       function pal()
        {
            var x=document.getElementById("a").value;

            //input String 

            var y="";

           //blank String 

            for (i=x.length-1;i>=0;i--)
          //string run from backward

                {

                    y=y+x[i];
       //store string last to first one by one in blank string 

                }

            if(x==y)

          //compare blank and original string equal or not 

                {

                    console.log("Palindrome");

                }

            else

                {

                    console.log("Not Palindrome ");

                }

        }
        

Upvotes: 0

Redu
Redu

Reputation: 26191

You might do as follows;

var isPalindrome = s => { var t = s.toLowerCase()
                                   .replace(/\s+/g,"");
                          return [].slice.call(t)
                                   .reverse()
                                   .every((b,i) => b === t[i]);
                        };

console.log(isPalindrome("Was it a car or a cat I saw"));
console.log(isPalindrome("This is not a palindrome"));

Upvotes: 0

Carl
Carl

Reputation: 1816

if (stringArr.sort(frontToBack)==stringArr.sort(backToFront)) { is your problem.

In JavaScript, the sort method updates the value of the variable you are sorting. So in your comparison, once both sort's have run, both end up with the same value (since the second sort, effectively overrides the first).

For example.

var a = [1,7,3];
a.sort();
console.log(a); // will print 1,3,7

Edit: had a quick test, I think eavidan's suggestion is probably the best one.

Edit2: Just put together a quick version of a hopefully working palindrome function :)

 function palindrome(str) { return str.split("").reverse().join("") == str;}

Upvotes: 1

eavidan
eavidan

Reputation: 5564

It is because string subtraction yields NaN, which means both sorted arrays are the same as the original.

Even if you did convert to ASCII coding, you sort the entire string, then for instance the string abba would be sorted front to back as aabb and back to front as bbaa. (edit: and also what Carl wrote about sort changing the original array. Still - sort is not the way to go here)

What you should do is just reverse the string (using reverse on the array) and compare.

Upvotes: 0

Related Questions