简体   繁体   English

我需要有关此回文密码的帮助

[英]I need help on this palindrome code

Here is the code I wrote on palindrome, no errors, but not working: 这是我在回文上编写的代码,没有错误,但无法正常工作:

function palindrome(str) {
  // Good luck!
  x = 0;
  y = 0;
  for (x = 0; x == str.length; x++){ 
  str2 = str.reverse();
   for(y = 0; y == str2.length; y++){

    var firstChar = str.length[x];
    var lastChar = str2.length[y];
    if (firstChar === lastChar){
  return true;
  }
}
}
}
palindrome("eye");

I will appreciate some directions. 我将感谢您的指导。

 var str="eye"; var strArray=str.split(""); var revStrArray=strArray.reverse(); var revString=revStrArray.join(""); if(revString===str) console.log("palindrome"); 

you are reinventing the wheel Try to make use of the library functions 您正在重新发明轮子尝试利用库函数

var str="eye";

str==str.split("").reverse().join("")

str.split("")->splits it into Array as "e","y","e"
str.split("").reverse()->reverse works on array and makes it as "e","y","e"
join->makes again it as string ,now this will be "eye"

You need not run a for loop for this. 您无需为此运行for循环。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM