-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 = "

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();">Enter</button><br /><br />
</body>
zhero1
  • 1

1 Answers1

0

I change some code in isPalindrome and add div to give result.

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>
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62