首页 > 解决方案 > 我的 HTML 和 JS 回文脚本有什么问题或缺少什么?

问题描述

函数 isPalindrome (userEntry) { str = str.toLowerCase; str = str.replace(/[^az]/g, ""); str = (i = 0 || i 你的话不是回文

"; } } document.getElementById("输出").innerHTML = "

你的话是回文

"; }

        <h2>Palindrome detection</h2>
        <code>Detect if a string is a palindrome</code><br /><br />

        Enter a word with 10 or less characters <input type="text" id="userEntry"><br />
        <button type="button" onclick="isPalindrome();">Enter</button><br /><br />

</body>

标签: palindrome

解决方案


我更改了 isPalindrome 中的一些代码并添加 div 以给出结果。

function isPalindrome (userEntry) { 
var re = /[\W_]/g;
  var lowRegStr = userEntry.toLowerCase().replace(re, '');
  var reverseStr = lowRegStr.split('').reverse().join(''); 
  if( reverseStr === lowRegStr){
  document.getElementById("output").innerHTML = "Your word is a palindrome"; 
  }
  }
  
 <h2>Palindrome detection</h2>
        <code>Detect if a string is a palindrome</code><br /><br />

        Enter a word with 10 or less characters <input type="text" id="userEntry"><br />
        <button type="button" onclick="isPalindrome(document.getElementById('userEntry').value)">Enter</button><br /><br />
   <div id="output"></div>


推荐阅读