check for palindrome

Benchmark created by joe on


Preparation HTML

<h1>Palindrome Checker</h1>
                <input type="text" id="textField" placeholder="Enter your word or phrase" value="A man, a plan, a canal, Panama!" style="width:25%"/>
                <input type="button"  id="checkText" value="Is it a Palindrone?" />

Test runner

Ready to run.

Testing in
TestOps/sec
using an array
document.getElementById('checkText').addEventListener('click', function(){
                                var e = document.getElementById('textField').value.replace(/\W/g, '').toLowerCase();
                                var msg = (e == e.split('').reverse().join(''))? 'Palindrome' :'Not!';
                                alert(msg);
                        }, false);

 
ready
testing the string
document.getElementById('checkText').addEventListener('click', function(){
                                var s = document.getElementById('textField').value.replace(/\W/g, '').toLowerCase();
                                var limit = s.length-1;
                                for(i=0; i < limit; i++){
                                        if(s[i] != s[limit-i]){
                                                 alert('Not!');
                                                 break;
                                         }else if(i == Math.ceil(limit/2)){
                                                alert('Palindrome');
                                                break;
                                         }
                                }
                        }, false);
 
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.