Last active
December 28, 2015 15:19
-
-
Save mikebucks/7521064 to your computer and use it in GitHub Desktop.
A simple little script to detect palindromes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Palindrome Detector</title> | |
</head> | |
<body> | |
<p>This soution splits the value of a text input into an array, reverses it, and then compares the result to the original value. If the two values are identical we have a palindrome. It uses no 3rd party libs and passes JSLint.</p> | |
<hr> | |
<form> | |
<input type="text" id="palindrome"> | |
<button id="submit">Is it a palindrome?</button> | |
</form> | |
<div id="results"></div> | |
<script> | |
(function () { | |
'use strict'; | |
var detectPalindrome = function (string) { | |
var forwardString = string.replace(/\W /g, '').toLowerCase(), | |
backwardString = forwardString.split('').reverse().toString().replace(/,/g, ''); | |
return (forwardString === backwardString) ? true : false; | |
}; | |
document.getElementById('submit').onclick = function (e) { | |
var val = document.getElementById('palindrome').value; | |
document.getElementById('results').innerHTML = detectPalindrome(val); | |
e.preventDefault(); | |
}; | |
}()); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment