0

Please solve my issue, I am going to check a list of strings as palindrome, if any of the string from the array is palindrome then it should display a result true, the result should be in string value not in Boolean value. I had tried so many times but it is not displaying the result; See my code below:-

function checkPry()
{
    var status = new Array();
    var wordList1 = document.getElementById("tk").value;
    var wordArray = new Array();
    wordArray = wordList1.split(" "); 
    var alength = wordArray.length; 
    for(var i=0; i <= alength; i++)
    {
        var str = wordArray[i];
        var chrlength = str.length;
        var lw = chrlength - 1;
        var chk = "";
        for(j=0; j<=chrlength; j++)
        {
            if(str.charAt(j) != str.charAt((lw - j)))
            {
                chk = "false";
                break;
            }
            else
            {
                chk = "true";
            }
        }
        if (chk == "true")
        {
            status[i] = "true";
        }
        else if (chk == "false")
        {
            status[i] = "false"
        }
    }
    var displayStr = status.toString();
    document.getElementById("show").innerHTML = displayStr;
}

like If I am giving the input value as [dalad radar jaijai rexem] then it should be give result as [true,true,false,false], Please help me in that; you can also check the fiddle below:--

http://jsfiddle.net/yePQ4/1/

Thanks!

0

2 Answers 2

5

You just need to check if something reversed is the same

function reverse(s){
     return s.split("").reverse().join("");
}

function checkIsPalindrome(arr){
   var result=[];
   for(var i=0;i<arr.length;i++){
       result.push(arr[i]==reverse(arr[i]));
   }
   return result;
}
Sign up to request clarification or add additional context in comments.

5 Comments

can you provide a fiddle for that, it's hard to understand
thank you so much... it's very optimized code
well the code is really good, but the issue is I am just beginner, and it's hard to understand it. can you provide me some instructions on my code, that where is the issue, why my logic is not working, see my code please. it will be a great help for me.
Performance isn't as good as it is here stackoverflow.com/questions/22111507/how-to-write-palindrome
2

Just for fun, here's an alternate version of @scrblnrd3's answer that uses a regex to reverse the string instead of split/join.

function reverse(text) {
    return text.replace(/./g, function(c, i, s) { return s[s.length - 1 - i]; });
}

function checkIsPalindrome(arr) {
    var result = [];
    for (var i = 0; i < arr.length; i++) {
        result.push(arr[i] == reverse(arr[i]));
    }
    return result;
}

2 Comments

can you create a fiddle so I can understand how's it will be use, I am just a beginner in JS
jsfiddle.net/8pLFC/1 :p Please dont accept this answer though, scrblnrd3 deserves credit for it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.