Skip to content

Instantly share code, notes, and snippets.

@mikebucks
Last active December 28, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikebucks/7521064 to your computer and use it in GitHub Desktop.
Save mikebucks/7521064 to your computer and use it in GitHub Desktop.
A simple little script to detect palindromes.
<!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