Skip to content

Instantly share code, notes, and snippets.

@btownrippleman
Created January 4, 2021 06:38
Show Gist options
  • Select an option

  • Save btownrippleman/2365b62bde24da2fb2e08057f7c488da to your computer and use it in GitHub Desktop.

Select an option

Save btownrippleman/2365b62bde24da2fb2e08057f7c488da to your computer and use it in GitHub Desktop.
this is html that checks if the text entered is a palindrome, it excludes anything that is neither a letter nor a number.
<!DOCTYPE=html>
<p>enter text and press button to check if it is a palindrome</p>
<input id="palcheck">
</input>
<button id="myBtn" onclick="getStatement()">Button</button>
<h1 id="output"></h1>
<script type="text/javascript">
/**
* @param {string} s
* @return {boolean}
*/
function getStatement(){
txt = (document.getElementById("palcheck").value);
if (isPalindrome(txt)){
document.getElementById("output").innerHTML = "the text above is a palindrome pal"
}
else{
document.getElementById("output").innerHTML = "the text above is not a palindrome pal"
}
}
var isPalindrome = function(s) {
n = s.split('').filter(char => /[a-zA-Z0-9]/.test(char)).map(a=>a.toLowerCase())
return JSON.stringify( n) === JSON.stringify([...n.reverse()])
};
// test = "A0 man, a plan, a canal: Panam0a"
// console.log(isPalindrome(test))
</script>
</>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment