我的HTML和JS回文脚本有什么问题或缺失?

问题描述 投票:-1回答:1
function isPalindrome (userEntry) { str = str.toLowerCase; str = str.replace(/[^a-z]/g, ""); str = (i = 0 || i Your word is not a palindrome"; } } document.getElementById("output").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
1个回答
0
投票

我在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>
© www.soinside.com 2019 - 2024. All rights reserved.