本文介绍了在JS中检查字符串(数组)的回文列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解决我的问题,我将检查一个字符串列表作为回文,如果数组中的任何字符串是回文,那么它应该显示结果为true,结果应该是字符串值而不是布尔值。我尝试过很多次,但没有显示结果;看看我的代码如下:
$ b $ pre $ 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 =; (j = 0; j 如果(str.charAt(j)!= str.charAt((lw-j))

b {
chk =false;
休息;
}
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;
}

如果我将输入值设为 [ dalad radar jaijai rexem] 然后它应该给结果为 [true,true,false,false] ,请帮助我;您还可以查看下面的小提琴: -





谢谢!

解决方案

 函数reverse(s){
return s.split()。reverse()。join();
}

函数checkIsPalindrome(arr){
var result = [];
for(var i = 0; i< arr.length; i ++){
result.push(arr [i] == reverse(arr [i]));
}
返回结果;
}


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!

解决方案

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;
}

这篇关于在JS中检查字符串(数组)的回文列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:24